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    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class TransactionInterceptor implements MethodInterceptor {
030    
031            @Override
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                    this.platformTransactionManager = platformTransactionManager;
059            }
060    
061            public void setTransactionAttributeSource(
062                    TransactionAttributeSource transactionAttributeSource) {
063    
064                    this.transactionAttributeSource = transactionAttributeSource;
065            }
066    
067            public void setTransactionExecutor(
068                    TransactionExecutor transactionExecutor) {
069    
070                    this.transactionExecutor = transactionExecutor;
071            }
072    
073            /**
074             * @deprecated As of 6.1.0, replaced by {@link
075             *             #setPlatformTransactionManager(PlatformTransactionManager)}
076             */
077            public void setTransactionManager(
078                    PlatformTransactionManager platformTransactionManager) {
079    
080                    setPlatformTransactionManager(platformTransactionManager);
081            }
082    
083            protected PlatformTransactionManager platformTransactionManager;
084            protected TransactionAttributeSource transactionAttributeSource;
085            protected TransactionExecutor transactionExecutor;
086    
087    }