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                    TransactionAttributeAdapter transactionAttributeAdapter =
056                            new TransactionAttributeAdapter(transactionAttribute);
057    
058                    return transactionExecutor.execute(
059                            platformTransactionManager, transactionAttributeAdapter,
060                            methodInvocation);
061            }
062    
063            public void setPlatformTransactionManager(
064                    PlatformTransactionManager platformTransactionManager) {
065    
066                    this.platformTransactionManager = platformTransactionManager;
067            }
068    
069            public void setTransactionAttributeSource(
070                    TransactionAttributeSource transactionAttributeSource) {
071    
072                    this.transactionAttributeSource = transactionAttributeSource;
073            }
074    
075            public void setTransactionExecutor(
076                    TransactionExecutor transactionExecutor) {
077    
078                    this.transactionExecutor = transactionExecutor;
079            }
080    
081            protected PlatformTransactionManager platformTransactionManager;
082            protected TransactionAttributeSource transactionAttributeSource;
083            protected TransactionExecutor transactionExecutor;
084    
085    }