Coverage Report - org.mule.transport.jms.JmsClientAcknowledgeTransaction
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsClientAcknowledgeTransaction
0%
0/28
0%
0/12
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.transport.jms;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.transaction.TransactionException;
 11  
 import org.mule.config.i18n.CoreMessages;
 12  
 import org.mule.transaction.AbstractSingleResourceTransaction;
 13  
 import org.mule.transaction.IllegalTransactionStateException;
 14  
 import org.mule.transport.jms.i18n.JmsMessages;
 15  
 
 16  
 import javax.jms.Connection;
 17  
 import javax.jms.JMSException;
 18  
 import javax.jms.Message;
 19  
 import javax.jms.Session;
 20  
 
 21  
 /**
 22  
  * <code>JmsClientAcknowledgeTransaction</code> is a transaction implementation of
 23  
  * performing a message acknowledgement. There is no notion of rollback with client
 24  
  * acknowledgement, but this transaction can be useful for controlling how messages
 25  
  * are consumed from a destination.
 26  
  */
 27  
 public class JmsClientAcknowledgeTransaction extends AbstractSingleResourceTransaction
 28  
 {
 29  
     private volatile Message message;
 30  
 
 31  
     public JmsClientAcknowledgeTransaction(MuleContext muleContext)
 32  
     {
 33  0
         super(muleContext);
 34  0
     }
 35  
 
 36  
     public void setMessage(Message message)
 37  
     {
 38  0
         this.message = message;
 39  0
     }
 40  
 
 41  
     @Override
 42  
     protected void doBegin() throws TransactionException
 43  
     {
 44  
         // nothing to do
 45  0
     }
 46  
 
 47  
     @Override
 48  
     protected void doCommit() throws TransactionException
 49  
     {
 50  
         try
 51  
         {
 52  0
             if (message == null)
 53  
             {
 54  0
                 throw new IllegalTransactionStateException(
 55  
                     JmsMessages.noMessageBoundForAck());
 56  
             }
 57  0
             message.acknowledge();
 58  
         }
 59  0
         catch (JMSException e)
 60  
         {
 61  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCommitFailed(), e);
 62  0
         }
 63  0
     }
 64  
 
 65  
     @Override
 66  
     protected void doRollback() throws TransactionException
 67  
     {
 68  
         // If a message has been bound, rollback is forbidden
 69  0
         if (message != null)
 70  
         {
 71  0
             throw new UnsupportedOperationException("Jms Client Acknowledge doesn't support rollback");
 72  
         }
 73  0
     }
 74  
 
 75  
     @Override
 76  
     public void bindResource(Object key, Object resource) throws TransactionException
 77  
     {
 78  0
         if (key instanceof Message)
 79  
         {
 80  0
             this.message = (Message)key;
 81  0
             return;
 82  
         }
 83  0
         if (!(key instanceof Connection) || !(resource instanceof Session))
 84  
         {
 85  0
             throw new IllegalTransactionStateException(
 86  
                 CoreMessages.transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session"));
 87  
         }
 88  
 
 89  0
         Session session = (Session)resource;
 90  
         try
 91  
         {
 92  0
             if (session.getTransacted())
 93  
             {
 94  0
                 throw new IllegalTransactionStateException(JmsMessages.sessionShouldNotBeTransacted());
 95  
             }
 96  
         }
 97  0
         catch (JMSException e)
 98  
         {
 99  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCannotReadState(), e);
 100  0
         }
 101  
 
 102  0
         super.bindResource(key, resource);
 103  0
     }
 104  
 }