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  
  * $Id: JmsClientAcknowledgeTransaction.java 20321 2010-11-24 15:21:24Z dfeist $
 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.Message;
 23  
 import javax.jms.Session;
 24  
 
 25  
 /**
 26  
  * <code>JmsClientAcknowledgeTransaction</code> is a transaction implementation of
 27  
  * performing a message acknowledgement. There is no notion of rollback with client
 28  
  * acknowledgement, but this transaction can be useful for controlling how messages
 29  
  * are consumed from a destination.
 30  
  */
 31  
 public class JmsClientAcknowledgeTransaction extends AbstractSingleResourceTransaction
 32  
 {
 33  
     private volatile Message message;
 34  
 
 35  
     public JmsClientAcknowledgeTransaction(MuleContext muleContext)
 36  
     {
 37  0
         super(muleContext);
 38  0
     }
 39  
 
 40  
     public void setMessage(Message message)
 41  
     {
 42  0
         this.message = message;
 43  0
     }
 44  
 
 45  
     @Override
 46  
     protected void doBegin() throws TransactionException
 47  
     {
 48  
         // nothing to do
 49  0
     }
 50  
 
 51  
     @Override
 52  
     protected void doCommit() throws TransactionException
 53  
     {
 54  
         try
 55  
         {
 56  0
             if (message == null)
 57  
             {
 58  0
                 throw new IllegalTransactionStateException(
 59  
                     JmsMessages.noMessageBoundForAck());
 60  
             }
 61  0
             message.acknowledge();
 62  
         }
 63  0
         catch (JMSException e)
 64  
         {
 65  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCommitFailed(), e);
 66  0
         }
 67  0
     }
 68  
 
 69  
     @Override
 70  
     protected void doRollback() throws TransactionException
 71  
     {
 72  
         // If a message has been bound, rollback is forbidden
 73  0
         if (message != null)
 74  
         {
 75  0
             throw new UnsupportedOperationException("Jms Client Acknowledge doesn't support rollback");
 76  
         }
 77  0
     }
 78  
 
 79  
     @Override
 80  
     public void bindResource(Object key, Object resource) throws TransactionException
 81  
     {
 82  0
         if (key instanceof Message)
 83  
         {
 84  0
             this.message = (Message)key;
 85  0
             return;
 86  
         }
 87  0
         if (!(key instanceof Connection) || !(resource instanceof Session))
 88  
         {
 89  0
             throw new IllegalTransactionStateException(
 90  
                 CoreMessages.transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session"));
 91  
         }
 92  
 
 93  0
         Session session = (Session)resource;
 94  
         try
 95  
         {
 96  0
             if (session.getTransacted())
 97  
             {
 98  0
                 throw new IllegalTransactionStateException(JmsMessages.sessionShouldNotBeTransacted());
 99  
             }
 100  
         }
 101  0
         catch (JMSException e)
 102  
         {
 103  0
             throw new IllegalTransactionStateException(CoreMessages.transactionCannotReadState(), e);
 104  0
         }
 105  
 
 106  0
         super.bindResource(key, resource);
 107  0
     }
 108  
 }