001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.spring.transaction;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.lang.reflect.Method;
022    
023    import org.aopalliance.intercept.MethodInterceptor;
024    import org.aopalliance.intercept.MethodInvocation;
025    
026    import org.springframework.transaction.interceptor.TransactionAspectSupport;
027    import org.springframework.transaction.interceptor.TransactionAttribute;
028    import org.springframework.transaction.interceptor.TransactionAttributeSource;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class TransactionInterceptor
034            extends TransactionAspectSupport implements MethodInterceptor {
035    
036            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
037                    Method method = methodInvocation.getMethod();
038    
039                    Class<?> targetClass = null;
040    
041                    Object thisObject = methodInvocation.getThis();
042    
043                    if (thisObject != null) {
044                            targetClass = thisObject.getClass();
045                    }
046    
047                    TransactionAttributeSource transactionAttributeSource =
048                            getTransactionAttributeSource();
049    
050                    TransactionAttribute transactionAttribute =
051                            transactionAttributeSource.getTransactionAttribute(
052                                    method, targetClass);
053    
054                    if (transactionAttribute == null) {
055                            return methodInvocation.proceed();
056                    }
057    
058                    Class<?> declaringClass = method.getDeclaringClass();
059    
060                    String joinPointIdentification = StringPool.BLANK;
061    
062                    if (_log.isDebugEnabled()) {
063                            joinPointIdentification =
064                                    declaringClass.getName().concat(StringPool.PERIOD).concat(
065                                            method.getName());
066                    }
067    
068                    TransactionInfo transactionInfo = createTransactionIfNecessary(
069                            getTransactionManager(), transactionAttribute,
070                            joinPointIdentification);
071    
072                    Object returnValue = null;
073    
074                    try {
075                            returnValue = methodInvocation.proceed();
076                    }
077                    catch (Throwable throwable) {
078                            completeTransactionAfterThrowing(transactionInfo, throwable);
079    
080                            throw throwable;
081                    }
082                    finally {
083                            cleanupTransactionInfo(transactionInfo);
084                    }
085    
086                    commitTransactionAfterReturning(transactionInfo);
087    
088                    return returnValue;
089            }
090    
091            private static Log _log = LogFactoryUtil.getLog(
092                    TransactionInterceptor.class);
093    
094    }