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