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.transaction;
016    
017    import java.lang.reflect.Method;
018    
019    import org.aopalliance.intercept.MethodInterceptor;
020    import org.aopalliance.intercept.MethodInvocation;
021    
022    import org.springframework.transaction.PlatformTransactionManager;
023    import org.springframework.transaction.interceptor.TransactionAttribute;
024    import org.springframework.transaction.interceptor.TransactionAttributeSource;
025    import org.springframework.transaction.support.CallbackPreferringPlatformTransactionManager;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class TransactionInterceptor implements MethodInterceptor {
031    
032            @Override
033            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
034                    Method method = methodInvocation.getMethod();
035    
036                    Class<?> targetClass = null;
037    
038                    Object targetBean = methodInvocation.getThis();
039    
040                    if (targetBean != null) {
041                            targetClass = targetBean.getClass();
042                    }
043    
044                    TransactionAttribute transactionAttribute =
045                            transactionAttributeSource.getTransactionAttribute(
046                                    method, targetClass);
047    
048                    if (transactionAttribute == null) {
049                            return methodInvocation.proceed();
050                    }
051    
052                    return transactionExecutor.execute(
053                            platformTransactionManager, transactionAttribute, methodInvocation);
054            }
055    
056            public void setPlatformTransactionManager(
057                    PlatformTransactionManager platformTransactionManager) {
058    
059                    if (platformTransactionManager instanceof
060                                    CallbackPreferringPlatformTransactionManager) {
061    
062                            transactionExecutor = new CallbackPreferringTransactionExecutor();
063                    }
064                    else {
065                            transactionExecutor = new DefaultTransactionExecutor();
066                    }
067    
068                    this.platformTransactionManager = platformTransactionManager;
069            }
070    
071            public void setTransactionAttributeSource(
072                    TransactionAttributeSource transactionAttributeSource) {
073    
074                    this.transactionAttributeSource = transactionAttributeSource;
075            }
076    
077            /**
078             * @deprecated {@link
079             *             #setPlatformTransactionManager(PlatformTransactionManager)}
080             */
081            public void setTransactionManager(
082                    PlatformTransactionManager platformTransactionManager) {
083    
084                    setPlatformTransactionManager(platformTransactionManager);
085            }
086    
087            protected PlatformTransactionManager platformTransactionManager;
088            protected TransactionAttributeSource transactionAttributeSource;
089            protected TransactionExecutor transactionExecutor;
090    
091    }