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