Coverage Report - org.mule.providers.jms.JmsClientAcknowledgeTransaction
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsClientAcknowledgeTransaction
56%
15/27
42%
5/12
3.8
 
 1  
 /*
 2  
  * $Id: JmsClientAcknowledgeTransaction.java 8215 2007-09-05 16:56:51Z aperepel $
 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.providers.jms.i18n.JmsMessages;
 15  
 import org.mule.transaction.AbstractSingleResourceTransaction;
 16  
 import org.mule.transaction.IllegalTransactionStateException;
 17  
 import org.mule.umo.TransactionException;
 18  
 
 19  
 import javax.jms.Connection;
 20  
 import javax.jms.JMSException;
 21  
 import javax.jms.Message;
 22  
 import javax.jms.Session;
 23  
 
 24  
 /**
 25  
  * <code>JmsClientAcknowledgeTransaction</code> is a transaction implementation of
 26  
  * performing a message acknowledgement. There is no notion of rollback with client
 27  
  * acknowledgement, but this transaction can be useful for controlling how messages
 28  
  * are consumed from a destination.
 29  
  */
 30  8
 public class JmsClientAcknowledgeTransaction extends AbstractSingleResourceTransaction
 31  
 {
 32  
     private volatile Message message;
 33  
 
 34  
     public void setMessage(Message message)
 35  
     {
 36  8
         this.message = message;
 37  8
     }
 38  
 
 39  
     /*
 40  
     * (non-Javadoc)
 41  
     *
 42  
     * @see org.mule.transaction.AbstractSingleResourceTransaction#doBegin()
 43  
     */
 44  
     protected void doBegin() throws TransactionException
 45  
     {
 46  
         // nothing to do
 47  8
     }
 48  
 
 49  
     /*
 50  
      * (non-Javadoc)
 51  
      * 
 52  
      * @see org.mule.transaction.AbstractSingleResourceTransaction#doCommit()
 53  
      */
 54  
     protected void doCommit() throws TransactionException
 55  
     {
 56  
         try
 57  
         {
 58  8
             if (message == null)
 59  
             {
 60  0
                 throw new IllegalTransactionStateException(
 61  
                     JmsMessages.noMessageBoundForAck());
 62  
             }
 63  8
             message.acknowledge();
 64  
         }
 65  0
         catch (JMSException e)
 66  
         {
 67  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCommitFailed(), e);
 68  8
         }
 69  8
     }
 70  
 
 71  
     /*
 72  
      * (non-Javadoc)
 73  
      * 
 74  
      * @see org.mule.transaction.AbstractSingleResourceTransaction#doRollback()
 75  
      */
 76  
     protected void doRollback() throws TransactionException
 77  
     {
 78  
         // If a message has been bound, rollback is forbidden
 79  0
         if (message != null)
 80  
         {
 81  0
             throw new UnsupportedOperationException("Jms Client Acknowledge doesn't support rollback");
 82  
         }
 83  0
     }
 84  
 
 85  
     /*
 86  
      * (non-Javadoc)
 87  
      * 
 88  
      * @see org.mule.umo.UMOTransaction#bindResource(java.lang.Object,
 89  
      *      java.lang.Object)
 90  
      */
 91  
     public void bindResource(Object key, Object resource) throws TransactionException
 92  
     {
 93  8
         if (key instanceof Message)
 94  
         {
 95  0
             this.message = (Message)key;
 96  0
             return;
 97  
         }
 98  8
         if (!(key instanceof Connection) || !(resource instanceof Session))
 99  
         {
 100  0
             throw new IllegalTransactionStateException(
 101  
                 CoreMessages.transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session"));
 102  
         }
 103  
 
 104  8
         Session session = (Session)resource;
 105  
         try
 106  
         {
 107  8
             if (session.getTransacted())
 108  
             {
 109  0
                 throw new IllegalTransactionStateException(JmsMessages.sessionShouldNotBeTransacted());
 110  
             }
 111  
         }
 112  0
         catch (JMSException e)
 113  
         {
 114  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCannotReadState(), e);
 115  8
         }
 116  
 
 117  8
         super.bindResource(key, resource);
 118  8
     }
 119  
 }