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 org.aopalliance.intercept.MethodInterceptor;
018    
019    import org.springframework.aop.TargetSource;
020    import org.springframework.aop.framework.AdvisedSupport;
021    import org.springframework.aop.framework.AopConfigException;
022    import org.springframework.aop.framework.AopProxy;
023    import org.springframework.aop.framework.AopProxyFactory;
024    import org.springframework.aop.framework.ProxyFactory;
025    import org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class ServiceBeanAutoProxyCreator
031            extends AbstractAdvisorAutoProxyCreator {
032    
033            public void afterPropertiesSet() {
034    
035                    // Backwards compatibility
036    
037                    if (_beanMatcher == null) {
038                            _beanMatcher = new ServiceBeanMatcher();
039                    }
040            }
041    
042            public void setBeanMatcher(BeanMatcher beanMatcher) {
043                    _beanMatcher = beanMatcher;
044            }
045    
046            public void setMethodInterceptor(MethodInterceptor methodInterceptor) {
047                    _methodInterceptor = methodInterceptor;
048            }
049    
050            @Override
051            protected void customizeProxyFactory(ProxyFactory proxyFactory) {
052                    proxyFactory.setAopProxyFactory(
053                            new AopProxyFactory() {
054    
055                                    @Override
056                                    public AopProxy createAopProxy(AdvisedSupport advisedSupport)
057                                            throws AopConfigException {
058    
059                                            return new ServiceBeanAopProxy(
060                                                    advisedSupport, _methodInterceptor);
061                                    }
062    
063                            }
064                    );
065            }
066    
067            @Override
068            @SuppressWarnings("rawtypes")
069            protected Object[] getAdvicesAndAdvisorsForBean(
070                    Class beanClass, String beanName, TargetSource targetSource) {
071    
072                    Object[] advices = DO_NOT_PROXY;
073    
074                    if (_beanMatcher.match(beanClass, beanName)) {
075                            advices = super.getAdvicesAndAdvisorsForBean(
076                                    beanClass, beanName, targetSource);
077    
078                            if (advices == DO_NOT_PROXY) {
079                                    advices = PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
080                            }
081                    }
082    
083                    return advices;
084            }
085    
086            private BeanMatcher _beanMatcher;
087            private MethodInterceptor _methodInterceptor;
088    
089    }