001    /**
002     * Copyright (c) 2000-2010 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 com.liferay.portal.kernel.util.StringPool;
018    
019    import java.lang.reflect.Method;
020    
021    import org.aopalliance.intercept.MethodInterceptor;
022    import org.aopalliance.intercept.MethodInvocation;
023    
024    import org.springframework.transaction.interceptor.TransactionAspectSupport;
025    import org.springframework.transaction.interceptor.TransactionAttribute;
026    import org.springframework.transaction.interceptor.TransactionAttributeSource;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class TransactionInterceptor
032            extends TransactionAspectSupport implements MethodInterceptor {
033    
034            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
035                    Method method = methodInvocation.getMethod();
036    
037                    Class<?> targetClass = null;
038    
039                    Object thisObject = methodInvocation.getThis();
040    
041                    if (thisObject != null) {
042                            targetClass = thisObject.getClass();
043                    }
044    
045                    TransactionAttributeSource transactionAttributeSource =
046                            getTransactionAttributeSource();
047    
048                    TransactionAttribute transactionAttribute =
049                            transactionAttributeSource.getTransactionAttribute(
050                                    method, targetClass);
051    
052                    Class<?> declaringClass = method.getDeclaringClass();
053    
054                    String joinPointIdentification =
055                            declaringClass.getName().concat(StringPool.PERIOD).concat(
056                                    method.getName());
057    
058                    TransactionInfo transactionInfo = createTransactionIfNecessary(
059                            getTransactionManager(), transactionAttribute,
060                            joinPointIdentification);
061    
062                    Object returnValue = null;
063    
064                    try {
065                            returnValue = methodInvocation.proceed();
066                    }
067                    catch (Throwable throwable) {
068                            completeTransactionAfterThrowing(transactionInfo, throwable);
069    
070                            throw throwable;
071                    }
072                    finally {
073                            cleanupTransactionInfo(transactionInfo);
074                    }
075    
076                    commitTransactionAfterReturning(transactionInfo);
077    
078                    return returnValue;
079            }
080    
081    }