1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
29   * <a href="TransactionInterceptor.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Shuyang Zhou
32   */
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  }