Coverage Report - org.mule.transaction.AbstractSingleResourceTransaction
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSingleResourceTransaction
0%
0/55
0%
0/26
0
 
 1  
 /*
 2  
  * $Id: AbstractSingleResourceTransaction.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.MuleContext;
 14  
 import org.mule.api.transaction.TransactionException;
 15  
 import org.mule.config.i18n.CoreMessages;
 16  
 
 17  
 import java.lang.reflect.Field;
 18  
 import java.util.Collections;
 19  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 
 22  
 import javax.transaction.Status;
 23  
 
 24  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
 25  
 
 26  
 /**
 27  
  * This abstract class can be used as a base class for transactions that can enlist
 28  
  * only one resource (such as a JMS session or JDBC connection).
 29  
  */
 30  
 public abstract class AbstractSingleResourceTransaction extends AbstractTransaction
 31  
 {
 32  
 
 33  
     /**
 34  
      * TX status code to human-readable string mappings.
 35  
      *
 36  
      * @see javax.transaction.Status
 37  
      */
 38  0
     protected static Map<Integer, String> txStatusMappings = new HashMap<Integer, String>(10); // populated later
 39  
 
 40  
     protected volatile Object key;
 41  
     protected volatile Object resource;
 42  
 
 43  0
     protected final AtomicBoolean started = new AtomicBoolean(false);
 44  0
     protected final AtomicBoolean committed = new AtomicBoolean(false);
 45  0
     protected final AtomicBoolean rolledBack = new AtomicBoolean(false);
 46  0
     protected final AtomicBoolean rollbackOnly = new AtomicBoolean(false);
 47  
 
 48  
     static
 49  
     {
 50  0
         Field[] fields = Status.class.getFields();
 51  0
         for (Field field : fields)
 52  
         {
 53  
             try
 54  
             {
 55  0
                 txStatusMappings.put(field.getInt(Status.class), field.getName());
 56  
             }
 57  0
             catch (IllegalAccessException e)
 58  
             {
 59  
                 // ignore
 60  0
             }
 61  
         }
 62  
 
 63  0
         txStatusMappings = Collections.unmodifiableMap(txStatusMappings);
 64  0
     }
 65  
 
 66  
     protected AbstractSingleResourceTransaction(MuleContext muleContext)
 67  
     {
 68  0
         super(muleContext);
 69  0
     }
 70  
 
 71  
     public void begin() throws TransactionException
 72  
     {
 73  0
         super.begin();
 74  0
         started.compareAndSet(false, true);
 75  0
     }
 76  
 
 77  
     public void commit() throws TransactionException
 78  
     {
 79  0
         super.commit();
 80  0
         committed.compareAndSet(false, true);
 81  0
     }
 82  
 
 83  
     public void rollback() throws TransactionException
 84  
     {
 85  0
         super.rollback();
 86  0
         rolledBack.compareAndSet(false, true);
 87  0
     }
 88  
 
 89  
     public int getStatus() throws TransactionStatusException
 90  
     {
 91  0
         if (rolledBack.get())
 92  
         {
 93  0
             return STATUS_ROLLEDBACK;
 94  
         }
 95  0
         if (committed.get())
 96  
         {
 97  0
             return STATUS_COMMITTED;
 98  
         }
 99  0
         if (rollbackOnly.get())
 100  
         {
 101  0
             return STATUS_MARKED_ROLLBACK;
 102  
         }
 103  0
         if (started.get())
 104  
         {
 105  0
             return STATUS_ACTIVE;
 106  
         }
 107  0
         return STATUS_NO_TRANSACTION;
 108  
     }
 109  
 
 110  
     public Object getResource(Object key)
 111  
     {
 112  0
         return key != null && this.key == key ? this.resource : null;
 113  
     }
 114  
 
 115  
     public boolean hasResource(Object key)
 116  
     {
 117  0
         return key != null && this.key == key;
 118  
     }
 119  
 
 120  
     public void bindResource(Object key, Object resource) throws TransactionException
 121  
     {
 122  0
         if (key == null)
 123  
         {
 124  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCannotBindToNullKey());
 125  
         }
 126  0
         if (resource == null)
 127  
         {
 128  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCannotBindNullResource());
 129  
         }
 130  0
         if (this.key != null)
 131  
         {
 132  0
             throw new IllegalTransactionStateException(CoreMessages.transactionSingleResourceOnly());
 133  
         }
 134  
         
 135  0
         if (logger.isDebugEnabled())
 136  
         {
 137  0
             logger.debug("Binding " + resource + " to " + key);
 138  
         }
 139  
         
 140  0
         this.key = key;
 141  0
         this.resource = resource;
 142  0
     }
 143  
 
 144  
     public void setRollbackOnly()
 145  
     {
 146  0
         rollbackOnly.set(true);
 147  0
     }
 148  
 
 149  
     @Override
 150  
     public String toString()
 151  
     {
 152  
         int status;
 153  
         try
 154  
         {
 155  0
             status = getStatus();
 156  
         }
 157  0
         catch (TransactionException e)
 158  
         {
 159  0
             status = -1;
 160  0
         }
 161  
 
 162  
         // map status to a human-readable string
 163  
 
 164  0
         String statusName = txStatusMappings.get(status);
 165  0
         if (statusName == null)
 166  
         {
 167  0
             statusName = "*undefined*";
 168  
         }
 169  
 
 170  0
         return new StringBuilder().append(getClass().getName())
 171  
                 .append('@').append(id)
 172  
                 .append("[status=").append(statusName)
 173  
                 .append(", key=").append(key)
 174  
                 .append(", resource=").append(resource)
 175  
                 .append("]").toString();
 176  
     }
 177  
 }