Coverage Report - org.mule.providers.jms.JmsTransaction
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsTransaction
46%
11/24
38%
3/8
4
 
 1  
 /*
 2  
  * $Id: JmsTransaction.java 9429 2007-10-29 11:09:10Z 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.providers.jms;
 12  
 
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.config.i18n.MessageFactory;
 15  
 import org.mule.providers.jms.i18n.JmsMessages;
 16  
 import org.mule.transaction.AbstractSingleResourceTransaction;
 17  
 import org.mule.transaction.IllegalTransactionStateException;
 18  
 import org.mule.umo.TransactionException;
 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  24
 public class JmsTransaction extends AbstractSingleResourceTransaction
 30  
 {
 31  
 
 32  
     public void bindResource(Object key, Object resource) throws TransactionException
 33  
     {
 34  24
         if (!(key instanceof Connection) || !(resource instanceof Session))
 35  
         {
 36  0
             throw new IllegalTransactionStateException(
 37  
                 CoreMessages.transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session"));
 38  
         }
 39  
 
 40  24
         Session session = (Session)resource;
 41  
         try
 42  
         {
 43  24
             if (!session.getTransacted())
 44  
             {
 45  0
                 throw new IllegalTransactionStateException(JmsMessages.sessionShouldBeTransacted());
 46  
             }
 47  
         }
 48  0
         catch (JMSException e)
 49  
         {
 50  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCannotReadState(), e);
 51  24
         }
 52  
 
 53  24
         super.bindResource(key, resource);
 54  24
     }
 55  
 
 56  
     protected void doBegin() throws TransactionException
 57  
     {
 58  
         // do nothing
 59  24
     }
 60  
 
 61  
     protected void doCommit() throws TransactionException
 62  
     {
 63  
         try
 64  
         {
 65  30
             ((Session)resource).commit();
 66  
         }
 67  0
         catch (JMSException e)
 68  
         {
 69  0
             throw new TransactionException(CoreMessages.transactionCommitFailed(), e);
 70  30
         }
 71  30
     }
 72  
 
 73  
     protected void doRollback() throws TransactionException
 74  
     {
 75  0
         if (resource != null)
 76  
         {
 77  
             try
 78  
             {
 79  0
                 ((Session)resource).rollback();
 80  
             }
 81  0
             catch (JMSException e)
 82  
             {
 83  0
                 throw new TransactionException(CoreMessages.transactionRollbackFailed(), e);
 84  0
             }
 85  
         }
 86  
         else 
 87  
         {
 88  0
             throw new TransactionException(MessageFactory.createStaticMessage("No resource has been bound to this transaction"));
 89  
         }
 90  0
     }
 91  
 }