Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 145   Methods: 2
NCLOC: 115   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PojoTxInterceptor.java 83.3% 86.4% 100% 85.9%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7   
 8    package org.jboss.cache.pojo.interceptors;
 9   
 10    import org.jboss.aop.joinpoint.Invocation;
 11    import org.jboss.aop.joinpoint.MethodInvocation;
 12    import org.jboss.cache.Fqn;
 13    import org.jboss.cache.pojo.PojoCacheException;
 14    import org.jboss.cache.transaction.BatchModeTransactionManager;
 15   
 16    import javax.transaction.RollbackException;
 17    import javax.transaction.Status;
 18    import javax.transaction.Transaction;
 19    import javax.transaction.TransactionManager;
 20   
 21    /**
 22    * Interceptor (done via aop advice) for transaction
 23    *
 24    * @author Ben Wang
 25    * @version $Id: PojoTxInterceptor.java,v 1.3 2007/05/23 10:28:56 msurtani Exp $
 26    */
 27    public class PojoTxInterceptor extends AbstractInterceptor
 28    {
 29    private TransactionManager localTm_ = null;
 30    private TransactionManager txManager_;
 31    public static final String TAG = "PC";
 32    public static final String TX = "TX";
 33   
 34   
 35  25119 public Object invoke(Invocation in) throws Throwable
 36    {
 37  25119 if (!(in instanceof MethodInvocation))
 38    {
 39  0 throw new IllegalArgumentException("TxInterceptor.invoke(): invocation not MethodInvocation");
 40    }
 41  25119 MethodInvocation invocation = (MethodInvocation) in;
 42   
 43  25119 if (txManager_ == null)
 44    {
 45  351 txManager_ = getCache(invocation).getTransactionManager();
 46    }
 47   
 48  25119 Transaction tx = null;
 49  25119 if (txManager_ != null)
 50    {
 51    // Use the configured one if so setup.
 52  25117 localTm_ = txManager_;
 53    }
 54    else
 55    {
 56  2 localTm_ = BatchModeTransactionManager.getInstance();
 57    }
 58   
 59  25119 tx = localTm_.getTransaction();
 60   
 61  25119 boolean needTx = false;
 62  2941 if (tx == null) needTx = true;
 63  25119 Fqn id = null;
 64  25119 try
 65    {
 66  25119 if (needTx)
 67    {
 68  2941 id = (Fqn) invocation.getArguments()[0];
 69  2941 log.debug("Initiating a local transaction for batch processing with id: " + id.toString());
 70    // Start one of our own as batch processing.
 71  2941 try
 72    {
 73  2941 localTm_.begin();
 74  2941 tx = localTm_.getTransaction();
 75    // Stuff tx context into the metadata
 76  2941 invocation.getMetaData().addMetaData(TAG, TX, tx);
 77  2941 return invocation.invokeNext(); // proceed to next advice or actual call
 78    }
 79    catch (Exception e)
 80    {
 81  2 log.warn(invocation.getMethod().getName() + ": exception occurred: " + e);
 82  2 try
 83    {
 84  2 localTm_.setRollbackOnly();
 85    }
 86    catch (Exception e2)
 87    {
 88  0 log.error("setRollbackOnly", e2);
 89    }
 90   
 91  2 throw new PojoCacheException("PojoCache operation will be rollback. id: " + id
 92    + ". Operation: " + invocation.getMethod().getName(), e);
 93    }
 94    }
 95    else
 96    {
 97  22178 invocation.getMetaData().addMetaData(TAG, TX, tx);
 98  22178 return invocation.invokeNext(); // proceed to next advice or actual call
 99    }
 100    }
 101    finally
 102    {
 103  25119 if (needTx)
 104    {
 105  2941 endTransaction(id);
 106    }
 107    }
 108    }
 109   
 110  2941 private void endTransaction(Fqn id)
 111    {
 112  2941 if (localTm_ == null)
 113    {
 114  0 log.warn("endTransaction(): tm is null for id: " + id);
 115  0 return;
 116    }
 117   
 118   
 119  2941 try
 120    {
 121  2941 if (localTm_.getTransaction().getStatus() != Status.STATUS_MARKED_ROLLBACK)
 122    {
 123  2928 localTm_.commit();
 124    }
 125  13 else if (localTm_.getTransaction().getStatus() == Status.STATUS_ROLLEDBACK)
 126    {
 127  0 log.info("endTransaction(): has been rolled back for id: " + id);
 128    }
 129    else
 130    {
 131  13 log.info("endTransaction(): rolling back tx for id: " + id);
 132  13 localTm_.rollback();
 133    }
 134    }
 135    catch (RollbackException re)
 136    {
 137    // Do nothing here since cache may rollback automatically.
 138  2 log.warn("endTransaction(): rolling back transaction with exception: " + re);
 139    }
 140    catch (Exception e)
 141    {
 142  0 log.warn("endTransaction(): Failed with exception: " + e);
 143    }
 144    }
 145    }