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