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 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    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class ServiceBeanAutoProxyCreator
033            extends AbstractAdvisorAutoProxyCreator {
034    
035            public ServiceBeanAutoProxyCreator() {
036                    _serviceBeanAopCacheManager = new ServiceBeanAopCacheManager();
037    
038                    _serviceBeanAopCacheManager.registerAnnotationChainableMethodAdvice(
039                            Skip.class, null);
040            }
041    
042            public void afterPropertiesSet() {
043                    ServiceBeanAopCacheManagerUtil.registerServiceBeanAopCacheManager(
044                            _serviceBeanAopCacheManager);
045    
046                    // Backwards compatibility
047    
048                    if (_beanMatcher == null) {
049                            _beanMatcher = new ServiceBeanMatcher();
050                    }
051            }
052    
053            public void destroy() {
054                    ServiceBeanAopCacheManagerUtil.unregisterServiceBeanAopCacheManager(
055                            _serviceBeanAopCacheManager);
056            }
057    
058            public void setBeanMatcher(BeanMatcher beanMatcher) {
059                    _beanMatcher = beanMatcher;
060            }
061    
062            public void setMethodInterceptor(MethodInterceptor methodInterceptor) {
063                    _methodInterceptor = methodInterceptor;
064            }
065    
066            @Override
067            protected void customizeProxyFactory(ProxyFactory proxyFactory) {
068                    proxyFactory.setAopProxyFactory(
069                            new AopProxyFactory() {
070    
071                                    @Override
072                                    public AopProxy createAopProxy(AdvisedSupport advisedSupport)
073                                            throws AopConfigException {
074    
075                                            return new ServiceBeanAopProxy(
076                                                    advisedSupport, _methodInterceptor,
077                                                    _serviceBeanAopCacheManager);
078                                    }
079    
080                            }
081                    );
082            }
083    
084            @Override
085            @SuppressWarnings("rawtypes")
086            protected Object[] getAdvicesAndAdvisorsForBean(
087                    Class beanClass, String beanName, TargetSource targetSource) {
088    
089                    Object[] advices = DO_NOT_PROXY;
090    
091                    if (_beanMatcher.match(beanClass, beanName)) {
092                            advices = super.getAdvicesAndAdvisorsForBean(
093                                    beanClass, beanName, targetSource);
094    
095                            if (advices == DO_NOT_PROXY) {
096                                    advices = PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
097                            }
098                    }
099    
100                    return advices;
101            }
102    
103            private BeanMatcher _beanMatcher;
104            private MethodInterceptor _methodInterceptor;
105            private ServiceBeanAopCacheManager _serviceBeanAopCacheManager;
106    
107    }