001    /**
002     * Copyright (c) 2000-2012 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    
047            public void destroy() {
048                    ServiceBeanAopCacheManagerUtil.unregisterServiceBeanAopCacheManager(
049                            _serviceBeanAopCacheManager);
050            }
051    
052            public void setMethodInterceptor(MethodInterceptor methodInterceptor) {
053                    _methodInterceptor = methodInterceptor;
054            }
055    
056            @Override
057            protected void customizeProxyFactory(ProxyFactory proxyFactory) {
058                    proxyFactory.setAopProxyFactory(
059                            new AopProxyFactory() {
060    
061                                    public AopProxy createAopProxy(AdvisedSupport advisedSupport)
062                                            throws AopConfigException {
063    
064                                            return new ServiceBeanAopProxy(
065                                                    advisedSupport, _methodInterceptor,
066                                                    _serviceBeanAopCacheManager);
067                                    }
068    
069                            }
070                    );
071            }
072    
073            @Override
074            @SuppressWarnings("rawtypes")
075            protected Object[] getAdvicesAndAdvisorsForBean(
076                    Class beanClass, String beanName, TargetSource targetSource) {
077    
078                    Object[] advices = DO_NOT_PROXY;
079    
080                    if (beanName.endsWith(_SERVICE_SUFFIX)) {
081                            advices = super.getAdvicesAndAdvisorsForBean(
082                                    beanClass, beanName, targetSource);
083    
084                            if (advices == DO_NOT_PROXY) {
085                                    advices = PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
086                            }
087                    }
088    
089                    return advices;
090            }
091    
092            private static final String _SERVICE_SUFFIX = "Service";
093    
094            private MethodInterceptor _methodInterceptor;
095            private ServiceBeanAopCacheManager _serviceBeanAopCacheManager;
096    
097    }