001
014
015 package com.liferay.portal.spring.aop;
016
017 import java.util.List;
018 import java.util.Map;
019
020 import org.aopalliance.intercept.MethodInterceptor;
021
022 import org.springframework.aop.TargetSource;
023 import org.springframework.aop.framework.AdvisedSupport;
024 import org.springframework.aop.framework.AopConfigException;
025 import org.springframework.aop.framework.AopProxy;
026 import org.springframework.aop.framework.AopProxyFactory;
027 import org.springframework.aop.framework.ProxyFactory;
028 import org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator;
029 import org.springframework.beans.factory.ListableBeanFactory;
030
031
034 public class ServiceBeanAutoProxyCreator
035 extends AbstractAdvisorAutoProxyCreator {
036
037 public ServiceBeanAutoProxyCreator() {
038 _serviceBeanAopCacheManager = new ServiceBeanAopCacheManager();
039 }
040
041 public void afterPropertiesSet() {
042 ServiceBeanAopCacheManagerUtil.registerServiceBeanAopCacheManager(
043 _serviceBeanAopCacheManager);
044
045
046
047 if (_beanMatcher == null) {
048 _beanMatcher = new ServiceBeanMatcher();
049 }
050
051 ListableBeanFactory listableBeanFactory =
052 (ListableBeanFactory)getBeanFactory();
053
054 Map<String, ChainableMethodAdviceInjector>
055 chainableMethodAdviceInjectors = listableBeanFactory.getBeansOfType(
056 ChainableMethodAdviceInjector.class);
057
058 for (ChainableMethodAdviceInjector chainableMethodAdviceInjector :
059 chainableMethodAdviceInjectors.values()) {
060
061 chainableMethodAdviceInjector.inject();
062 }
063
064 if (!listableBeanFactory.containsBean(
065 ChainableMethodAdviceInjectorCollector.BEAN_NAME)) {
066
067 return;
068 }
069
070 ChainableMethodAdviceInjectorCollector
071 chainableMethodAdviceInjectorCollector =
072 (ChainableMethodAdviceInjectorCollector)
073 listableBeanFactory.getBean(
074 ChainableMethodAdviceInjectorCollector.BEAN_NAME);
075
076 List<String> beanNames =
077 chainableMethodAdviceInjectorCollector.getBeanNames();
078
079 for (String beanName : beanNames) {
080 Object bean = listableBeanFactory.getBean(beanName);
081
082 if (bean instanceof ChainableMethodAdviceInjector) {
083 ChainableMethodAdviceInjector chainableMethodAdviceInjector =
084 (ChainableMethodAdviceInjector)bean;
085
086 chainableMethodAdviceInjector.inject();
087 }
088 }
089 }
090
091 public void destroy() {
092 ServiceBeanAopCacheManagerUtil.unregisterServiceBeanAopCacheManager(
093 _serviceBeanAopCacheManager);
094 }
095
096 public void setBeanMatcher(BeanMatcher beanMatcher) {
097 _beanMatcher = beanMatcher;
098 }
099
100 public void setMethodInterceptor(MethodInterceptor methodInterceptor) {
101 _methodInterceptor = methodInterceptor;
102 }
103
104 @Override
105 protected void customizeProxyFactory(ProxyFactory proxyFactory) {
106 proxyFactory.setAopProxyFactory(
107 new AopProxyFactory() {
108
109 @Override
110 public AopProxy createAopProxy(AdvisedSupport advisedSupport)
111 throws AopConfigException {
112
113 return new ServiceBeanAopProxy(
114 advisedSupport, _methodInterceptor,
115 _serviceBeanAopCacheManager);
116 }
117
118 }
119 );
120 }
121
122 @Override
123 @SuppressWarnings("rawtypes")
124 protected Object[] getAdvicesAndAdvisorsForBean(
125 Class beanClass, String beanName, TargetSource targetSource) {
126
127 Object[] advices = DO_NOT_PROXY;
128
129 if (_beanMatcher.match(beanClass, beanName)) {
130 advices = super.getAdvicesAndAdvisorsForBean(
131 beanClass, beanName, targetSource);
132
133 if (advices == DO_NOT_PROXY) {
134 advices = PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
135 }
136 }
137
138 return advices;
139 }
140
141 private BeanMatcher _beanMatcher;
142 private MethodInterceptor _methodInterceptor;
143 private final ServiceBeanAopCacheManager _serviceBeanAopCacheManager;
144
145 }