001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ken.util;
017
018import javax.transaction.Status;
019import javax.transaction.UserTransaction;
020
021import org.aopalliance.intercept.MethodInterceptor;
022import org.aopalliance.intercept.MethodInvocation;
023import org.apache.log4j.Level;
024import org.apache.log4j.Logger;
025import org.springframework.transaction.jta.JtaTransactionManager;
026
027/**
028 * Interceptor implementation that simply logs the JTA transaction status
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031public class TracingTransactionInterceptor implements MethodInterceptor {
032    private static final Logger LOG = Logger.getLogger(TracingTransactionInterceptor.class);
033
034    private Level level;
035    private JtaTransactionManager txManager;
036
037    /**
038     * @param lvl
039     */
040    public void setLevel(String lvl) {
041        level = Level.toLevel(lvl);
042    }
043
044    /**
045     * @param manager
046     */
047    public void setJtaTransactionManager(JtaTransactionManager manager) {
048        this.txManager = manager;
049    }
050
051    /**
052     * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
053     */
054    public Object invoke(MethodInvocation invocation) throws Throwable {
055        StringBuilder sb = new StringBuilder();
056        UserTransaction tx = txManager.getUserTransaction();
057        if (tx == null) {
058            sb.append("null");
059        } else {
060            sb.append(toString(tx.getStatus()));
061        }
062        LOG.log(level, invocation.getMethod() +" UserTransaction: " + sb);
063        return invocation.proceed();
064    }
065
066    /**
067     * @param txStatus
068     * @return String
069     */
070    private static final String toString(int txStatus) {
071        switch (txStatus) {
072            case Status.STATUS_ACTIVE: return "STATUS_ACTIVE";
073            case Status.STATUS_COMMITTED: return "STATUS_COMMITTED";  
074            case Status.STATUS_COMMITTING: return "STATUS_COMMITTING";
075            case Status.STATUS_MARKED_ROLLBACK: return "STATUS_MARKED_ROLLBACK";
076            case Status.STATUS_NO_TRANSACTION: return "STATUS_NO_TRANSACTION";
077            case Status.STATUS_PREPARED: return "STATUS_PREPARED";
078            case Status.STATUS_PREPARING: return "STATUS_PREPARING";
079            case Status.STATUS_ROLLEDBACK: return "STATUS_ROLLEDBACK";
080            case Status.STATUS_ROLLING_BACK: return "STATUS_ROLLING_BACK";
081            case Status.STATUS_UNKNOWN: return "STATUS_UNKNOWN";
082            default: return "unknown status: " + txStatus;
083        }
084    }
085}