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            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
033                    Method method = methodInvocation.getMethod();
034    
035                    Class<?> targetClass = null;
036    
037                    Object targetBean = methodInvocation.getThis();
038    
039                    if (targetBean != null) {
040                            targetClass = targetBean.getClass();
041                    }
042    
043                    TransactionAttribute transactionAttribute =
044                            transactionAttributeSource.getTransactionAttribute(
045                                    method, targetClass);
046    
047                    if (transactionAttribute == null) {
048                            return methodInvocation.proceed();
049                    }
050    
051                    return transactionExecutor.execute(
052                            platformTransactionManager, transactionAttribute, methodInvocation);
053            }
054    
055            public void setPlatformTransactionManager(
056                    PlatformTransactionManager platformTransactionManager) {
057    
058                    if (platformTransactionManager instanceof
059                                    CallbackPreferringPlatformTransactionManager) {
060    
061                            transactionExecutor = new CallbackPreferringTransactionExecutor();
062                    }
063                    else {
064                            transactionExecutor = new DefaultTransactionExecutor();
065                    }
066    
067                    this.platformTransactionManager = platformTransactionManager;
068            }
069    
070            public void setTransactionAttributeSource(
071                    TransactionAttributeSource transactionAttributeSource) {
072    
073                    this.transactionAttributeSource = transactionAttributeSource;
074            }
075    
076            /**
077             * @deprecated As of 6.1.0, replaced by {@link
078             *             #setPlatformTransactionManager(PlatformTransactionManager)}
079             */
080            public void setTransactionManager(
081                    PlatformTransactionManager platformTransactionManager) {
082    
083                    setPlatformTransactionManager(platformTransactionManager);
084            }
085    
086            protected PlatformTransactionManager platformTransactionManager;
087            protected TransactionAttributeSource transactionAttributeSource;
088            protected TransactionExecutor transactionExecutor;
089    
090    }