1
14
15 package com.liferay.portal.spring.transaction;
16
17 import com.liferay.portal.kernel.util.StringPool;
18
19 import java.lang.reflect.Method;
20
21 import org.aopalliance.intercept.MethodInterceptor;
22 import org.aopalliance.intercept.MethodInvocation;
23
24 import org.springframework.transaction.interceptor.TransactionAspectSupport;
25 import org.springframework.transaction.interceptor.TransactionAttribute;
26 import org.springframework.transaction.interceptor.TransactionAttributeSource;
27
28
33 public class TransactionInterceptor
34 extends TransactionAspectSupport implements MethodInterceptor {
35
36 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
37 Method method = methodInvocation.getMethod();
38
39 Class<?> targetClass = null;
40
41 Object thisObject = methodInvocation.getThis();
42
43 if (thisObject != null) {
44 targetClass = thisObject.getClass();
45 }
46
47 TransactionAttributeSource transactionAttributeSource =
48 getTransactionAttributeSource();
49
50 TransactionAttribute transactionAttribute =
51 transactionAttributeSource.getTransactionAttribute(
52 method, targetClass);
53
54 Class<?> declaringClass = method.getDeclaringClass();
55
56 String joinPointIdentification =
57 declaringClass.getName().concat(StringPool.PERIOD).concat(
58 method.getName());
59
60 TransactionInfo transactionInfo = createTransactionIfNecessary(
61 getTransactionManager(), transactionAttribute,
62 joinPointIdentification);
63
64 Object returnValue = null;
65
66 try {
67 returnValue = methodInvocation.proceed();
68 }
69 catch (Throwable throwable) {
70 completeTransactionAfterThrowing(transactionInfo, throwable);
71
72 throw throwable;
73 }
74 finally {
75 cleanupTransactionInfo(transactionInfo);
76 }
77
78 commitTransactionAfterReturning(transactionInfo);
79
80 return returnValue;
81 }
82
83 }