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