Coverage Report - org.mule.transaction.AbstractTransaction
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractTransaction
0%
0/43
0%
0/12
0
 
 1  
 /*
 2  
  * $Id: AbstractTransaction.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.Transaction;
 15  
 import org.mule.api.transaction.TransactionException;
 16  
 import org.mule.config.i18n.CoreMessages;
 17  
 import org.mule.context.notification.TransactionNotification;
 18  
 import org.mule.util.UUID;
 19  
 
 20  
 import java.text.MessageFormat;
 21  
 
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 
 25  
 /**
 26  
  * This base class provides low level features for transactions.
 27  
  */
 28  
 public abstract class AbstractTransaction implements Transaction
 29  
 {
 30  
 
 31  0
     protected final transient Log logger = LogFactory.getLog(getClass());
 32  
 
 33  0
     protected String id = UUID.getUUID();
 34  
 
 35  
     protected MuleContext muleContext;
 36  
 
 37  
     protected AbstractTransaction(MuleContext muleContext)
 38  0
     {
 39  0
         this.muleContext = muleContext;
 40  0
     }
 41  
 
 42  
     public boolean isRollbackOnly() throws TransactionException
 43  
     {
 44  0
         return getStatus() == STATUS_MARKED_ROLLBACK;
 45  
     }
 46  
 
 47  
     public boolean isBegun() throws TransactionException
 48  
     {
 49  0
         int status = getStatus();
 50  0
         return status != STATUS_NO_TRANSACTION && status != STATUS_UNKNOWN;
 51  
     }
 52  
 
 53  
     public boolean isRolledBack() throws TransactionException
 54  
     {
 55  0
         return getStatus() == STATUS_ROLLEDBACK;
 56  
     }
 57  
 
 58  
     public boolean isCommitted() throws TransactionException
 59  
     {
 60  0
         return getStatus() == STATUS_COMMITTED;
 61  
     }
 62  
 
 63  
     public void begin() throws TransactionException
 64  
     {
 65  0
         logger.debug("Beginning transaction");
 66  0
         doBegin();
 67  0
         TransactionCoordination.getInstance().bindTransaction(this);
 68  0
         fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_BEGAN));
 69  0
     }
 70  
 
 71  
     public void commit() throws TransactionException
 72  
     {
 73  
         try
 74  
         {
 75  0
             logger.debug("Committing transaction " + this);
 76  
 
 77  0
             if (isRollbackOnly())
 78  
             {
 79  0
                 throw new IllegalTransactionStateException(CoreMessages.transactionMarkedForRollback());
 80  
             }
 81  
 
 82  0
             doCommit();
 83  0
             fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_COMMITTED));
 84  
         }
 85  
         finally
 86  
         {
 87  0
             TransactionCoordination.getInstance().unbindTransaction(this);
 88  0
         }
 89  0
     }
 90  
 
 91  
     public void rollback() throws TransactionException
 92  
     {
 93  
         try
 94  
         {
 95  0
             logger.debug("Rolling back transaction");
 96  0
             setRollbackOnly();
 97  0
             doRollback();
 98  0
             fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_ROLLEDBACK));
 99  
         }
 100  
         finally
 101  
         {
 102  0
             unbindTransaction();
 103  0
         }
 104  0
     }
 105  
 
 106  
     /**
 107  
      * Unbind this transaction when complete
 108  
      */
 109  
     protected void unbindTransaction() throws TransactionException
 110  
     {
 111  0
         TransactionCoordination.getInstance().unbindTransaction(this);
 112  0
     }
 113  
 
 114  
 
 115  
     /**
 116  
      * Really begin the transaction. Note that resources are enlisted yet.
 117  
      * 
 118  
      * @throws TransactionException
 119  
      */
 120  
     protected abstract void doBegin() throws TransactionException;
 121  
 
 122  
     /**
 123  
      * Commit the transaction on the underlying resource
 124  
      * 
 125  
      * @throws TransactionException
 126  
      */
 127  
     protected abstract void doCommit() throws TransactionException;
 128  
 
 129  
     /**
 130  
      * Rollback the transaction on the underlying resource
 131  
      * 
 132  
      * @throws TransactionException
 133  
      */
 134  
     protected abstract void doRollback() throws TransactionException;
 135  
 
 136  
     /**
 137  
      * Fires a server notification to all registered
 138  
      * {@link org.mule.api.context.notification.TransactionNotificationListener}s.
 139  
      *
 140  
      */
 141  
     protected void fireNotification(TransactionNotification notification)
 142  
     {
 143  
         // TODO profile this piece of code
 144  0
         muleContext.fireNotification(notification);
 145  0
     }
 146  
 
 147  
     public boolean isXA()
 148  
     {
 149  0
         return false;
 150  
     }
 151  
 
 152  
     public void resume() throws TransactionException
 153  
     {
 154  0
         throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(this));
 155  
     }
 156  
 
 157  
     public javax.transaction.Transaction suspend() throws TransactionException
 158  
     {
 159  0
         throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(this));
 160  
     }
 161  
 
 162  
     public String getId()
 163  
     {
 164  0
         return id;
 165  
     }
 166  
 
 167  
     @Override
 168  
     public String toString()
 169  
     {
 170  
         int status;
 171  
         try
 172  
         {
 173  0
             status = getStatus();
 174  
         }
 175  0
         catch (TransactionException e)
 176  
         {
 177  0
             status = -1;
 178  0
         }
 179  0
         return MessageFormat.format("{0}[id={1} , status={2}]", getClass().getName(), id, status);
 180  
     }
 181  
 }