Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 121   Methods: 9
NCLOC: 82   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
OrderedSynchronizationHandler.java 75% 96.3% 100% 93.2%
coverage coverage
 1    package org.jboss.cache.interceptors;
 2   
 3    import org.apache.commons.logging.Log;
 4    import org.apache.commons.logging.LogFactory;
 5   
 6    import javax.transaction.RollbackException;
 7    import javax.transaction.Synchronization;
 8    import javax.transaction.SystemException;
 9    import javax.transaction.Transaction;
 10    import java.util.HashMap;
 11    import java.util.LinkedList;
 12    import java.util.Map;
 13   
 14    /**
 15    * Maintains a list of Synchronization handlers. Reason is that we have to
 16    * invoke certain handlers <em>before</em> others. See the description in
 17    * SyncTxUnitTestCase.testConcurrentPuts(). For example, for synchronous
 18    * replication, we have to execute the ReplicationInterceptor's
 19    * afterCompletion() <em>before</em> the TransactionInterceptor's.
 20    *
 21    * @author Bela Ban
 22    * @version $Id: OrderedSynchronizationHandler.java,v 1.6 2007/06/29 04:27:33 bstansberry Exp $
 23    */
 24    public class OrderedSynchronizationHandler implements Synchronization
 25    {
 26    private Transaction tx = null;
 27    private LinkedList<Synchronization> handlers = new LinkedList<Synchronization>();
 28   
 29    /**
 30    * Map<Transaction,OrderedSynchronizationHandler>
 31    */
 32    static Map instances = new HashMap();
 33   
 34    static Log log = LogFactory.getLog(OrderedSynchronizationHandler.class);
 35   
 36   
 37  1121795 private OrderedSynchronizationHandler(Transaction tx)
 38    {
 39  1121795 this.tx = tx;
 40    }
 41   
 42    /**
 43    * Creates a new instance of OrderedSynchronizationHandler, or fetches an existing instance. Key is the local
 44    * transaction (tx). This instance registers with the TransactionManager automatically
 45    *
 46    * @param tx
 47    */
 48  1121831 public static OrderedSynchronizationHandler getInstance(Transaction tx) throws SystemException, RollbackException
 49    {
 50  1121831 OrderedSynchronizationHandler retval = (OrderedSynchronizationHandler) instances.get(tx);
 51  36 if (retval != null) return retval;
 52  1121794 retval = new OrderedSynchronizationHandler(tx);
 53  1121795 tx.registerSynchronization(retval);
 54  1121795 instances.put(tx, retval);
 55  1121795 return retval;
 56    }
 57   
 58   
 59  1121830 public void registerAtHead(Synchronization handler)
 60    {
 61  1121830 register(handler, true);
 62    }
 63   
 64  1 public void registerAtTail(Synchronization handler)
 65    {
 66  1 register(handler, false);
 67    }
 68   
 69  1121831 void register(Synchronization handler, boolean head)
 70    {
 71  1121831 if (handler != null && !handlers.contains(handler))
 72    {
 73  1121831 if (head)
 74  1121830 handlers.addFirst(handler);
 75    else
 76  1 handlers.addLast(handler);
 77    }
 78    }
 79   
 80  1121500 public void beforeCompletion()
 81    {
 82  1121500 for (Synchronization sync : handlers)
 83    {
 84  1121522 sync.beforeCompletion();
 85    }
 86    }
 87   
 88  1121738 public void afterCompletion(int status)
 89    {
 90  1121738 for (Synchronization sync : handlers)
 91    {
 92  1121774 try
 93    {
 94  1121774 sync.afterCompletion(status);
 95    }
 96    catch (Throwable t)
 97    {
 98  8 log.error("failed calling afterCompletion() on " + sync, t);
 99    }
 100    }
 101   
 102    // finally unregister us from the hashmap
 103  1121738 instances.remove(tx);
 104    }
 105   
 106  63 public String toString()
 107    {
 108  63 StringBuffer sb = new StringBuffer();
 109  63 sb.append("tx=" + getTxAsString() + ", handlers=" + handlers);
 110  63 return sb.toString();
 111    }
 112   
 113  63 private String getTxAsString()
 114    {
 115    // JBCACHE-1114 -- don't call toString() on tx or it can lead to stack overflow
 116  63 if (tx == null)
 117  0 return null;
 118   
 119  63 return tx.getClass().getName() + "@" + System.identityHashCode(tx);
 120    }
 121    }