001    /**
002     * Copyright (c) 2000-2013 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 com.liferay.portal.kernel.spring.aop.Skip;
018    
019    import java.util.Map;
020    
021    import org.aopalliance.intercept.MethodInterceptor;
022    
023    import org.springframework.aop.TargetSource;
024    import org.springframework.aop.framework.AdvisedSupport;
025    import org.springframework.aop.framework.AopConfigException;
026    import org.springframework.aop.framework.AopProxy;
027    import org.springframework.aop.framework.AopProxyFactory;
028    import org.springframework.aop.framework.ProxyFactory;
029    import org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator;
030    import org.springframework.beans.factory.ListableBeanFactory;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class ServiceBeanAutoProxyCreator
036            extends AbstractAdvisorAutoProxyCreator {
037    
038            public ServiceBeanAutoProxyCreator() {
039                    _serviceBeanAopCacheManager = new ServiceBeanAopCacheManager();
040    
041                    _serviceBeanAopCacheManager.registerAnnotationChainableMethodAdvice(
042                            Skip.class, null);
043            }
044    
045            public void afterPropertiesSet() {
046                    ServiceBeanAopCacheManagerUtil.registerServiceBeanAopCacheManager(
047                            _serviceBeanAopCacheManager);
048    
049                    // Backwards compatibility
050    
051                    if (_beanMatcher == null) {
052                            _beanMatcher = new ServiceBeanMatcher();
053                    }
054    
055                    ListableBeanFactory listableBeanFactory =
056                            (ListableBeanFactory)getBeanFactory();
057    
058                    Map<String, ChainableMethodAdviceInjector>
059                            chainableMethodAdviceInjectors =
060                                    listableBeanFactory.getBeansOfType(
061                                            ChainableMethodAdviceInjector.class);
062    
063                    for (ChainableMethodAdviceInjector chainableMethodAdviceInjector :
064                                    chainableMethodAdviceInjectors.values()) {
065    
066                            chainableMethodAdviceInjector.inject();
067                    }
068            }
069    
070            public void destroy() {
071                    ServiceBeanAopCacheManagerUtil.unregisterServiceBeanAopCacheManager(
072                            _serviceBeanAopCacheManager);
073            }
074    
075            public void setBeanMatcher(BeanMatcher beanMatcher) {
076                    _beanMatcher = beanMatcher;
077            }
078    
079            public void setMethodInterceptor(MethodInterceptor methodInterceptor) {
080                    _methodInterceptor = methodInterceptor;
081            }
082    
083            @Override
084            protected void customizeProxyFactory(ProxyFactory proxyFactory) {
085                    proxyFactory.setAopProxyFactory(
086                            new AopProxyFactory() {
087    
088                                    @Override
089                                    public AopProxy createAopProxy(AdvisedSupport advisedSupport)
090                                            throws AopConfigException {
091    
092                                            return new ServiceBeanAopProxy(
093                                                    advisedSupport, _methodInterceptor,
094                                                    _serviceBeanAopCacheManager);
095                                    }
096    
097                            }
098                    );
099            }
100    
101            @Override
102            @SuppressWarnings("rawtypes")
103            protected Object[] getAdvicesAndAdvisorsForBean(
104                    Class beanClass, String beanName, TargetSource targetSource) {
105    
106                    Object[] advices = DO_NOT_PROXY;
107    
108                    if (_beanMatcher.match(beanClass, beanName)) {
109                            advices = super.getAdvicesAndAdvisorsForBean(
110                                    beanClass, beanName, targetSource);
111    
112                            if (advices == DO_NOT_PROXY) {
113                                    advices = PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
114                            }
115                    }
116    
117                    return advices;
118            }
119    
120            private BeanMatcher _beanMatcher;
121            private MethodInterceptor _methodInterceptor;
122            private ServiceBeanAopCacheManager _serviceBeanAopCacheManager;
123    
124    }