001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
032     * @author Shuyang Zhou
033     */
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                    // Backwards compatibility
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            @Override
122            @SuppressWarnings("rawtypes")
123            protected Object[] getAdvicesAndAdvisorsForBean(
124                    Class beanClass, String beanName, TargetSource targetSource) {
125    
126                    Object[] advices = DO_NOT_PROXY;
127    
128                    if (_beanMatcher.match(beanClass, beanName)) {
129                            advices = super.getAdvicesAndAdvisorsForBean(
130                                    beanClass, beanName, targetSource);
131    
132                            if (advices == DO_NOT_PROXY) {
133                                    advices = PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
134                            }
135                    }
136    
137                    return advices;
138            }
139    
140            private BeanMatcher _beanMatcher;
141            private MethodInterceptor _methodInterceptor;
142            private final ServiceBeanAopCacheManager _serviceBeanAopCacheManager;
143    
144    }