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 void setMethodInterceptor(MethodInterceptor methodInterceptor) {
036                    _methodInterceptor = methodInterceptor;
037            }
038    
039            public void setServiceBeanAopCacheManager(
040                    ServiceBeanAopCacheManager serviceBeanAopCacheManager) {
041    
042                    _serviceBeanAopCacheManager = serviceBeanAopCacheManager;
043    
044                    _serviceBeanAopCacheManager.registerAnnotationChainableMethodAdvice(
045                            Skip.class, null);
046            }
047    
048            @Override
049            protected void customizeProxyFactory(ProxyFactory proxyFactory) {
050                    proxyFactory.setAopProxyFactory(
051                            new AopProxyFactory() {
052    
053                                    public AopProxy createAopProxy(AdvisedSupport advisedSupport)
054                                            throws AopConfigException {
055    
056                                            return new ServiceBeanAopProxy(
057                                                    advisedSupport, _methodInterceptor,
058                                                    _serviceBeanAopCacheManager);
059                                    }
060    
061                            }
062                    );
063            }
064    
065            @Override
066            @SuppressWarnings("rawtypes")
067            protected Object[] getAdvicesAndAdvisorsForBean(
068                    Class beanClass, String beanName, TargetSource targetSource) {
069    
070                    Object[] advices = DO_NOT_PROXY;
071    
072                    if (beanName.endsWith(_SERVICE_SUFFIX)) {
073                            advices = super.getAdvicesAndAdvisorsForBean(
074                                    beanClass, beanName, targetSource);
075    
076                            if (advices == DO_NOT_PROXY) {
077                                    advices = PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
078                            }
079                    }
080    
081                    return advices;
082            }
083    
084            private static final String _SERVICE_SUFFIX = "Service";
085    
086            private MethodInterceptor _methodInterceptor;
087            private ServiceBeanAopCacheManager _serviceBeanAopCacheManager;
088    
089    }