Coverage Report - org.mule.transport.jms.JmsMessageRequester
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsMessageRequester
0%
0/53
0%
0/36
0
JmsMessageRequester$1
0%
0/6
0%
0/6
0
 
 1  
 /*
 2  
  * $Id: JmsMessageRequester.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.MuleMessage;
 14  
 import org.mule.api.context.notification.TransactionNotificationListener;
 15  
 import org.mule.api.endpoint.InboundEndpoint;
 16  
 import org.mule.api.transaction.Transaction;
 17  
 import org.mule.api.transaction.TransactionConfig;
 18  
 import org.mule.context.notification.TransactionNotification;
 19  
 import org.mule.transaction.TransactionCoordination;
 20  
 import org.mule.transport.AbstractMessageRequester;
 21  
 import org.mule.transport.jms.filters.JmsSelectorFilter;
 22  
 import org.mule.util.StringUtils;
 23  
 
 24  
 import javax.jms.Destination;
 25  
 import javax.jms.Message;
 26  
 import javax.jms.MessageConsumer;
 27  
 import javax.jms.Session;
 28  
 
 29  
 /**
 30  
  * <code>JmsMessageDispatcher</code> is responsible for dispatching messages to JMS
 31  
  * destinations. All JMS semantics apply and settings such as replyTo and QoS
 32  
  * properties are read from the event properties or defaults are used (according to
 33  
  * the JMS specification)
 34  
  */
 35  0
 public class JmsMessageRequester extends AbstractMessageRequester
 36  
 {
 37  
 
 38  
     private JmsConnector connector;
 39  
 
 40  
     public JmsMessageRequester(InboundEndpoint endpoint)
 41  
     {
 42  0
         super(endpoint);
 43  0
         this.connector = (JmsConnector) endpoint.getConnector();
 44  0
     }
 45  
 
 46  
     @Override
 47  
     protected void doConnect() throws Exception
 48  
     {
 49  
         // template method
 50  0
     }
 51  
 
 52  
     @Override
 53  
     protected void doDisconnect() throws Exception
 54  
     {
 55  
         // template method
 56  0
     }
 57  
 
 58  
     /**
 59  
      * Make a specific request to the underlying transport
 60  
      *
 61  
      * @param timeout the maximum time the operation should block before returning.
 62  
      *            The call should return immediately if there is data available. If
 63  
      *            no data becomes available before the timeout elapses, null will be
 64  
      *            returned
 65  
      * @return the result of the request wrapped in a MuleMessage object. Null will be
 66  
      *         returned if no data was avaialable
 67  
      * @throws Exception if the call to the underlying protocal cuases an exception
 68  
      */
 69  
     @Override
 70  
     protected MuleMessage doRequest(long timeout) throws Exception
 71  
     {
 72  0
         Session session = null;
 73  0
         MessageConsumer consumer = null;
 74  0
         boolean cleanupListenerRegistered = false;
 75  
 
 76  
         try
 77  
         {
 78  0
             final boolean topic = connector.getTopicResolver().isTopic(endpoint);
 79  
 
 80  0
             JmsSupport support = connector.getJmsSupport();
 81  0
             final TransactionConfig transactionConfig = endpoint.getTransactionConfig();
 82  0
             final Transaction tx = TransactionCoordination.getInstance().getTransaction();
 83  0
             boolean transacted = transactionConfig != null && transactionConfig.isTransacted();
 84  
 
 85  0
             session = connector.getSession(transacted, topic);
 86  
 
 87  0
             if (transacted && !tx.isXA())
 88  
             {
 89  
                 // register a session close listener
 90  0
                 final Session finalSession = session;
 91  0
                 getConnector().getMuleContext().registerListener(new TransactionNotificationListener<TransactionNotification>()
 92  0
                 {
 93  
                     public void onNotification(TransactionNotification txNotification)
 94  
                     {
 95  0
                         final int txAction = txNotification.getAction();
 96  0
                         final String txId = txNotification.getTransactionStringId();
 97  0
                         if ((txAction == TransactionNotification.TRANSACTION_COMMITTED || txAction == TransactionNotification.TRANSACTION_ROLLEDBACK) &&
 98  
                             txId.equals(tx.getId())) {
 99  0
                             connector.closeQuietly(finalSession);
 100  
                         }
 101  0
                     }
 102  
                 }, tx.getId());
 103  
 
 104  0
                 cleanupListenerRegistered = true;
 105  
             }
 106  
 
 107  0
             Destination dest = support.createDestination(session, endpoint);
 108  
 
 109  
             // Extract jms selector
 110  0
             String selector = null;
 111  0
             if (endpoint.getFilter() != null && endpoint.getFilter() instanceof JmsSelectorFilter)
 112  
             {
 113  0
                 final String expressionTemplate = ((JmsSelectorFilter) endpoint.getFilter()).getExpression();
 114  0
                 if (StringUtils.isNotBlank(expressionTemplate))
 115  
                 {
 116  0
                     selector = connector.getMuleContext().getExpressionManager().parse(expressionTemplate, null);
 117  
                 }
 118  0
             }
 119  0
             else if (endpoint.getProperties() != null)
 120  
             {
 121  
                 // still allow the selector to be set as a property on the endpoint
 122  
                 // to be backward compatable
 123  0
                 final String expressionTemplate = (String) endpoint.getProperty(JmsConstants.JMS_SELECTOR_PROPERTY);
 124  0
                 if (StringUtils.isNotBlank(expressionTemplate))
 125  
                 {
 126  0
                     selector = connector.getMuleContext().getExpressionManager().parse(expressionTemplate, null);
 127  
                 }
 128  
             }
 129  0
             String tempDurable = (String) endpoint.getProperties().get(JmsConstants.DURABLE_PROPERTY);
 130  0
             boolean durable = connector.isDurable();
 131  0
             if (tempDurable != null)
 132  
             {
 133  0
                 durable = Boolean.valueOf(tempDurable);
 134  
             }
 135  
 
 136  
             // Get the durable subscriber name if there is one
 137  0
             String durableName = (String) endpoint.getProperties().get(JmsConstants.DURABLE_NAME_PROPERTY);
 138  0
             if (durableName == null && durable && topic)
 139  
             {
 140  0
                 durableName = "mule." + connector.getName() + "." + endpoint.getEndpointURI().getAddress();
 141  0
                 if (logger.isDebugEnabled())
 142  
                 {
 143  0
                     logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: "
 144  
                              + durableName);
 145  
                 }
 146  
             }
 147  
 
 148  
             // Create consumer
 149  0
             consumer = support.createConsumer(session, dest, selector, connector.isNoLocal(), durableName,
 150  
                 topic, endpoint);
 151  
 
 152  
             Message message;
 153  
 
 154  0
             if (timeout == JmsMessageDispatcher.RECEIVE_NO_WAIT)
 155  
             {
 156  0
                 message = consumer.receiveNoWait();
 157  
             }
 158  0
             else if (timeout == JmsMessageDispatcher.RECEIVE_WAIT_INDEFINITELY)
 159  
             {
 160  0
                 message = consumer.receive();
 161  
             }
 162  
             else
 163  
             {
 164  0
                 message = consumer.receive(timeout);
 165  
             }
 166  
 
 167  0
             if (message == null)
 168  
             {
 169  0
                 return null;
 170  
             }
 171  
 
 172  0
             message = connector.preProcessMessage(message, session);
 173  0
             return createMuleMessage(message, endpoint.getEncoding());
 174  
         }
 175  
         finally
 176  
         {
 177  0
             if (!cleanupListenerRegistered)
 178  
             {
 179  0
                 connector.closeQuietly(consumer);
 180  0
                 connector.closeSessionIfNoTransactionActive(session);
 181  
             }
 182  
         }
 183  
     }
 184  
 
 185  
     @Override
 186  
     protected void doDispose()
 187  
     {
 188  
         // template method
 189  0
     }
 190  
 
 191  
 }