View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ken.util;
17  
18  import javax.transaction.Status;
19  import javax.transaction.UserTransaction;
20  
21  import org.aopalliance.intercept.MethodInterceptor;
22  import org.aopalliance.intercept.MethodInvocation;
23  import org.apache.log4j.Level;
24  import org.apache.log4j.Logger;
25  import org.springframework.transaction.jta.JtaTransactionManager;
26  
27  /**
28   * Interceptor implementation that simply logs the JTA transaction status
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class TracingTransactionInterceptor implements MethodInterceptor {
32      private static final Logger LOG = Logger.getLogger(TracingTransactionInterceptor.class);
33  
34      private Level level;
35      private JtaTransactionManager txManager;
36  
37      /**
38       * @param lvl
39       */
40      public void setLevel(String lvl) {
41          level = Level.toLevel(lvl);
42      }
43  
44      /**
45       * @param manager
46       */
47      public void setJtaTransactionManager(JtaTransactionManager manager) {
48          this.txManager = manager;
49      }
50  
51      /**
52       * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
53       */
54      public Object invoke(MethodInvocation invocation) throws Throwable {
55          StringBuilder sb = new StringBuilder();
56          UserTransaction tx = txManager.getUserTransaction();
57          if (tx == null) {
58              sb.append("null");
59          } else {
60              sb.append(toString(tx.getStatus()));
61          }
62          LOG.log(level, invocation.getMethod() +" UserTransaction: " + sb);
63          return invocation.proceed();
64      }
65  
66      /**
67       * @param txStatus
68       * @return String
69       */
70      private static final String toString(int txStatus) {
71          switch (txStatus) {
72              case Status.STATUS_ACTIVE: return "STATUS_ACTIVE";
73              case Status.STATUS_COMMITTED: return "STATUS_COMMITTED";  
74              case Status.STATUS_COMMITTING: return "STATUS_COMMITTING";
75              case Status.STATUS_MARKED_ROLLBACK: return "STATUS_MARKED_ROLLBACK";
76              case Status.STATUS_NO_TRANSACTION: return "STATUS_NO_TRANSACTION";
77              case Status.STATUS_PREPARED: return "STATUS_PREPARED";
78              case Status.STATUS_PREPARING: return "STATUS_PREPARING";
79              case Status.STATUS_ROLLEDBACK: return "STATUS_ROLLEDBACK";
80              case Status.STATUS_ROLLING_BACK: return "STATUS_ROLLING_BACK";
81              case Status.STATUS_UNKNOWN: return "STATUS_UNKNOWN";
82              default: return "unknown status: " + txStatus;
83          }
84      }
85  }