Coverage Report - org.mule.transaction.AbstractSingleResourceTransaction
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSingleResourceTransaction
0%
0/39
0%
0/24
2.667
 
 1  
 /*
 2  
  * $Id: AbstractSingleResourceTransaction.java 12181 2008-06-26 20:05:55Z 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.api.transaction.TransactionException;
 14  
 import org.mule.config.i18n.CoreMessages;
 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  
     public void begin() throws TransactionException
 34  
     {
 35  0
         super.begin();
 36  0
         started.compareAndSet(false, true);
 37  0
     }
 38  
 
 39  
     public void commit() throws TransactionException
 40  
     {
 41  0
         super.commit();
 42  0
         committed.compareAndSet(false, true);
 43  0
     }
 44  
 
 45  
     public void rollback() throws TransactionException
 46  
     {
 47  0
         super.rollback();
 48  0
         rolledBack.compareAndSet(false, true);
 49  0
     }
 50  
 
 51  
     public int getStatus() throws TransactionStatusException
 52  
     {
 53  0
         if (rolledBack.get())
 54  
         {
 55  0
             return STATUS_ROLLEDBACK;
 56  
         }
 57  0
         if (committed.get())
 58  
         {
 59  0
             return STATUS_COMMITTED;
 60  
         }
 61  0
         if (rollbackOnly.get())
 62  
         {
 63  0
             return STATUS_MARKED_ROLLBACK;
 64  
         }
 65  0
         if (started.get())
 66  
         {
 67  0
             return STATUS_ACTIVE;
 68  
         }
 69  0
         return STATUS_NO_TRANSACTION;
 70  
     }
 71  
 
 72  
     public Object getResource(Object key)
 73  
     {
 74  0
         return key != null && this.key == key ? this.resource : null;
 75  
     }
 76  
 
 77  
     public boolean hasResource(Object key)
 78  
     {
 79  0
         return key != null && this.key == key;
 80  
     }
 81  
 
 82  
     public void bindResource(Object key, Object resource) throws TransactionException
 83  
     {
 84  0
         if (key == null)
 85  
         {
 86  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCannotBindToNullKey());
 87  
         }
 88  0
         if (resource == null)
 89  
         {
 90  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCannotBindNullResource());
 91  
         }
 92  0
         if (this.key != null)
 93  
         {
 94  0
             throw new IllegalTransactionStateException(CoreMessages.transactionSingleResourceOnly());
 95  
         }
 96  
         
 97  0
         if (logger.isDebugEnabled())
 98  
         {
 99  0
             logger.debug("Binding " + resource + " to " + key);
 100  
         }
 101  
         
 102  0
         this.key = key;
 103  0
         this.resource = resource;
 104  0
     }
 105  
 
 106  
     public void setRollbackOnly()
 107  
     {
 108  0
         rollbackOnly.set(true);
 109  0
     }
 110  
 
 111  
     public Object getId()
 112  
     {
 113  0
         return key;
 114  
     }
 115  
 }