Coverage Report - org.mule.transaction.AbstractSingleResourceTransaction
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSingleResourceTransaction
0%
0/37
0%
0/9
2.556
 
 1  
 /*
 2  
  * $Id: AbstractSingleResourceTransaction.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.transaction;
 12  
 
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.umo.TransactionException;
 15  
 
 16  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
 17  
 
 18  
 /**
 19  
  * This abstract class can be used as a base class for transactions that can enlist
 20  
  * only one resource (such as a JMS session or JDBC connection).
 21  
  */
 22  0
 public abstract class AbstractSingleResourceTransaction extends AbstractTransaction
 23  
 {
 24  
 
 25  
     protected volatile Object key;
 26  
     protected volatile Object resource;
 27  
 
 28  0
     protected final AtomicBoolean started = new AtomicBoolean(false);
 29  0
     protected final AtomicBoolean committed = new AtomicBoolean(false);
 30  0
     protected final AtomicBoolean rolledBack = new AtomicBoolean(false);
 31  0
     protected final AtomicBoolean rollbackOnly = new AtomicBoolean(false);
 32  
 
 33  
     /*
 34  
      * (non-Javadoc)
 35  
      * 
 36  
      * @see org.mule.umo.UMOTransaction#begin()
 37  
      */
 38  
     public void begin() throws TransactionException
 39  
     {
 40  0
         super.begin();
 41  0
         started.compareAndSet(false, true);
 42  0
     }
 43  
 
 44  
     /*
 45  
      * (non-Javadoc)
 46  
      * 
 47  
      * @see org.mule.umo.UMOTransaction#commit()
 48  
      */
 49  
     public void commit() throws TransactionException
 50  
     {
 51  0
         super.commit();
 52  0
         committed.compareAndSet(false, true);
 53  0
     }
 54  
 
 55  
     /*
 56  
      * (non-Javadoc)
 57  
      * 
 58  
      * @see org.mule.umo.UMOTransaction#rollback()
 59  
      */
 60  
     public void rollback() throws TransactionException
 61  
     {
 62  0
         super.rollback();
 63  0
         rolledBack.compareAndSet(false, true);
 64  0
     }
 65  
 
 66  
     /*
 67  
      * (non-Javadoc)
 68  
      * 
 69  
      * @see org.mule.umo.UMOTransaction#getStatus()
 70  
      */
 71  
     public int getStatus() throws TransactionStatusException
 72  
     {
 73  0
         if (rolledBack.get())
 74  
         {
 75  0
             return STATUS_ROLLEDBACK;
 76  
         }
 77  0
         if (committed.get())
 78  
         {
 79  0
             return STATUS_COMMITTED;
 80  
         }
 81  0
         if (rollbackOnly.get())
 82  
         {
 83  0
             return STATUS_MARKED_ROLLBACK;
 84  
         }
 85  0
         if (started.get())
 86  
         {
 87  0
             return STATUS_ACTIVE;
 88  
         }
 89  0
         return STATUS_NO_TRANSACTION;
 90  
     }
 91  
 
 92  
     /*
 93  
      * (non-Javadoc)
 94  
      * 
 95  
      * @see org.mule.umo.UMOTransaction#getResource(java.lang.Object)
 96  
      */
 97  
     public Object getResource(Object key)
 98  
     {
 99  0
         return key != null && this.key == key ? this.resource : null;
 100  
     }
 101  
 
 102  
     /*
 103  
      * (non-Javadoc)
 104  
      * 
 105  
      * @see org.mule.umo.UMOTransaction#hasResource(java.lang.Object)
 106  
      */
 107  
     public boolean hasResource(Object key)
 108  
     {
 109  0
         return key != null && this.key == key;
 110  
     }
 111  
 
 112  
     /*
 113  
      * (non-Javadoc)
 114  
      * 
 115  
      * @see org.mule.umo.UMOTransaction#bindResource(java.lang.Object,
 116  
      *      java.lang.Object)
 117  
      */
 118  
     public void bindResource(Object key, Object resource) throws TransactionException
 119  
     {
 120  0
         if (key == null)
 121  
         {
 122  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCannotBindToNullKey());
 123  
         }
 124  0
         if (resource == null)
 125  
         {
 126  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCannotBindNullResource());
 127  
         }
 128  0
         if (this.key != null)
 129  
         {
 130  0
             throw new IllegalTransactionStateException(CoreMessages.transactionSingleResourceOnly());
 131  
         }
 132  0
         this.key = key;
 133  0
         this.resource = resource;
 134  0
     }
 135  
 
 136  
     /*
 137  
      * (non-Javadoc)
 138  
      * 
 139  
      * @see org.mule.umo.UMOTransaction#setRollbackOnly()
 140  
      */
 141  
     public void setRollbackOnly()
 142  
     {
 143  0
         rollbackOnly.set(true);
 144  0
     }
 145  
 
 146  
     public Object getId()
 147  
     {
 148  0
         return key;
 149  
     }
 150  
 }