001
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
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 }