Coverage Report - org.mule.transport.jms.JmsTransaction
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsTransaction
0%
0/31
0%
0/12
4
 
 1  
 /*
 2  
  * $Id: JmsTransaction.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.transport.jms;
 12  
 
 13  
 import org.mule.api.MuleContext;
 14  
 import org.mule.api.transaction.TransactionException;
 15  
 import org.mule.config.i18n.CoreMessages;
 16  
 import org.mule.transaction.AbstractSingleResourceTransaction;
 17  
 import org.mule.transaction.IllegalTransactionStateException;
 18  
 import org.mule.transport.jms.i18n.JmsMessages;
 19  
 
 20  
 import javax.jms.Connection;
 21  
 import javax.jms.JMSException;
 22  
 import javax.jms.Session;
 23  
 
 24  
 /**
 25  
  * <code>JmsTransaction</code> is a wrapper for a JMS local transaction. This
 26  
  * object holds the JMS session and controls when the transaction is committed or
 27  
  * rolled back.
 28  
  */
 29  
 public class JmsTransaction extends AbstractSingleResourceTransaction
 30  
 {
 31  
 
 32  
     public JmsTransaction(MuleContext muleContext)
 33  
     {
 34  0
         super(muleContext);
 35  0
     }
 36  
 
 37  
     public void bindResource(Object key, Object resource) throws TransactionException
 38  
     {
 39  0
         if (!(key instanceof Connection) || !(resource instanceof Session))
 40  
         {
 41  0
             throw new IllegalTransactionStateException(
 42  
                 CoreMessages.transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session"));
 43  
         }
 44  
 
 45  0
         Session session = (Session)resource;
 46  
         try
 47  
         {
 48  0
             if (!session.getTransacted())
 49  
             {
 50  0
                 throw new IllegalTransactionStateException(JmsMessages.sessionShouldBeTransacted());
 51  
             }
 52  
         }
 53  0
         catch (JMSException e)
 54  
         {
 55  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCannotReadState(), e);
 56  0
         }
 57  
 
 58  0
         super.bindResource(key, resource);
 59  0
     }
 60  
 
 61  
     protected void doBegin() throws TransactionException
 62  
     {
 63  
         // do nothing
 64  0
     }
 65  
 
 66  
     protected void doCommit() throws TransactionException
 67  
     {
 68  0
         if (resource == null)
 69  
         {
 70  0
             logger.warn(CoreMessages.commitTxButNoResource(this));
 71  0
             return;
 72  
         }
 73  
         
 74  
         try
 75  
         {
 76  0
             ((Session)resource).commit();
 77  
         }
 78  0
         catch (JMSException e)
 79  
         {
 80  0
             throw new TransactionException(CoreMessages.transactionCommitFailed(), e);
 81  0
         }
 82  0
     }
 83  
 
 84  
     protected void doRollback() throws TransactionException
 85  
     {
 86  0
         if (resource == null)
 87  
         {
 88  0
             logger.warn(CoreMessages.rollbackTxButNoResource(this));
 89  0
             return;
 90  
         }
 91  
 
 92  
         try
 93  
         {
 94  0
             if (logger.isDebugEnabled())
 95  
             {
 96  0
                 logger.debug("Rolling back transaction: " + getId());
 97  
             }
 98  0
             ((Session)resource).rollback();
 99  
         }
 100  0
         catch (JMSException e)
 101  
         {
 102  0
             throw new TransactionException(CoreMessages.transactionRollbackFailed(), e);
 103  0
         }    
 104  0
     }
 105  
     
 106  
 }