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 =
056 listableBeanFactory.getBeansOfType(
057 ChainableMethodAdviceInjector.class);
058
059 for (ChainableMethodAdviceInjector chainableMethodAdviceInjector :
060 chainableMethodAdviceInjectors.values()) {
061
062 chainableMethodAdviceInjector.inject();
063 }
064
065 if (!listableBeanFactory.containsBean(
066 ChainableMethodAdviceInjectorCollector.BEAN_NAME)) {
067
068 return;
069 }
070
071 ChainableMethodAdviceInjectorCollector
072 chainableMethodAdviceInjectorCollector =
073 (ChainableMethodAdviceInjectorCollector)
074 listableBeanFactory.getBean(
075 ChainableMethodAdviceInjectorCollector.BEAN_NAME);
076
077 List<String> beanNames =
078 chainableMethodAdviceInjectorCollector.getBeanNames();
079
080 for (String beanName : beanNames) {
081 Object bean = listableBeanFactory.getBean(beanName);
082
083 if (bean instanceof ChainableMethodAdviceInjector) {
084 ChainableMethodAdviceInjector chainableMethodAdviceInjector =
085 (ChainableMethodAdviceInjector)bean;
086
087 chainableMethodAdviceInjector.inject();
088 }
089 }
090 }
091
092 public void destroy() {
093 ServiceBeanAopCacheManagerUtil.unregisterServiceBeanAopCacheManager(
094 _serviceBeanAopCacheManager);
095 }
096
097 public void setBeanMatcher(BeanMatcher beanMatcher) {
098 _beanMatcher = beanMatcher;
099 }
100
101 public void setMethodInterceptor(MethodInterceptor methodInterceptor) {
102 _methodInterceptor = methodInterceptor;
103 }
104
105 @Override
106 protected void customizeProxyFactory(ProxyFactory proxyFactory) {
107 proxyFactory.setAopProxyFactory(
108 new AopProxyFactory() {
109
110 @Override
111 public AopProxy createAopProxy(AdvisedSupport advisedSupport)
112 throws AopConfigException {
113
114 return new ServiceBeanAopProxy(
115 advisedSupport, _methodInterceptor,
116 _serviceBeanAopCacheManager);
117 }
118
119 }
120 );
121 }
122
123 @Override
124 @SuppressWarnings("rawtypes")
125 protected Object[] getAdvicesAndAdvisorsForBean(
126 Class beanClass, String beanName, TargetSource targetSource) {
127
128 Object[] advices = DO_NOT_PROXY;
129
130 if (_beanMatcher.match(beanClass, beanName)) {
131 advices = super.getAdvicesAndAdvisorsForBean(
132 beanClass, beanName, targetSource);
133
134 if (advices == DO_NOT_PROXY) {
135 advices = PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
136 }
137 }
138
139 return advices;
140 }
141
142 private BeanMatcher _beanMatcher;
143 private MethodInterceptor _methodInterceptor;
144 private ServiceBeanAopCacheManager _serviceBeanAopCacheManager;
145
146 }