Coverage Report - org.mule.transport.jms.JmsMessageRequester
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsMessageRequester
73%
30/41
36%
8/22
3.4
 
 1  
 /*
 2  
  * $Id: JmsMessageRequester.java 11710 2008-05-09 08:27:48Z dirk.olmes $
 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.transport.jms;
 12  
 
 13  
 import org.mule.DefaultMuleMessage;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.endpoint.InboundEndpoint;
 16  
 import org.mule.transport.AbstractMessageRequester;
 17  
 import org.mule.transport.jms.filters.JmsSelectorFilter;
 18  
 
 19  
 import javax.jms.Destination;
 20  
 import javax.jms.Message;
 21  
 import javax.jms.MessageConsumer;
 22  
 import javax.jms.Session;
 23  
 
 24  
 /**
 25  
  * <code>JmsMessageDispatcher</code> is responsible for dispatching messages to JMS
 26  
  * destinations. All JMS semantics apply and settings such as replyTo and QoS
 27  
  * properties are read from the event properties or defaults are used (according to
 28  
  * the JMS specification)
 29  
  */
 30  
 public class JmsMessageRequester extends AbstractMessageRequester
 31  
 {
 32  
 
 33  
     private JmsConnector connector;
 34  
 
 35  
     public JmsMessageRequester(InboundEndpoint endpoint)
 36  
     {
 37  12
         super(endpoint);
 38  12
         this.connector = (JmsConnector) endpoint.getConnector();
 39  12
     }
 40  
 
 41  
     protected void doConnect() throws Exception
 42  
     {
 43  
         // template method
 44  12
     }
 45  
 
 46  
     protected void doDisconnect() throws Exception
 47  
     {
 48  
         // template method
 49  12
     }
 50  
 
 51  
     /**
 52  
      * Make a specific request to the underlying transport
 53  
      *
 54  
      * @param timeout the maximum time the operation should block before returning.
 55  
      *            The call should return immediately if there is data available. If
 56  
      *            no data becomes available before the timeout elapses, null will be
 57  
      *            returned
 58  
      * @return the result of the request wrapped in a MuleMessage object. Null will be
 59  
      *         returned if no data was avaialable
 60  
      * @throws Exception if the call to the underlying protocal cuases an exception
 61  
      */
 62  
     protected MuleMessage doRequest(long timeout) throws Exception
 63  
     {
 64  28
         Session session = null;
 65  28
         MessageConsumer consumer = null;
 66  
 
 67  
         try
 68  
         {
 69  28
             final boolean topic = connector.getTopicResolver().isTopic(endpoint);
 70  
 
 71  28
             JmsSupport support = connector.getJmsSupport();
 72  28
             session = connector.getSession(false, topic);
 73  28
             Destination dest = support.createDestination(session, endpoint.getEndpointURI().getAddress(),
 74  
                 topic);
 75  
 
 76  
             // Extract jms selector
 77  28
             String selector = null;
 78  28
             if (endpoint.getFilter() != null && endpoint.getFilter() instanceof JmsSelectorFilter)
 79  
             {
 80  0
                 selector = ((JmsSelectorFilter) endpoint.getFilter()).getExpression();
 81  
             }
 82  28
             else if (endpoint.getProperties() != null)
 83  
             {
 84  
                 // still allow the selector to be set as a property on the endpoint
 85  
                 // to be backward compatable
 86  28
                 selector = (String) endpoint.getProperties().get(JmsConstants.JMS_SELECTOR_PROPERTY);
 87  
             }
 88  28
             String tempDurable = (String) endpoint.getProperties().get(JmsConstants.DURABLE_PROPERTY);
 89  28
             boolean durable = connector.isDurable();
 90  28
             if (tempDurable != null)
 91  
             {
 92  0
                 durable = Boolean.valueOf(tempDurable).booleanValue();
 93  
             }
 94  
 
 95  
             // Get the durable subscriber name if there is one
 96  28
             String durableName = (String) endpoint.getProperties().get(JmsConstants.DURABLE_NAME_PROPERTY);
 97  28
             if (durableName == null && durable && topic)
 98  
             {
 99  0
                 durableName = "mule." + connector.getName() + "." + endpoint.getEndpointURI().getAddress();
 100  0
                 if (logger.isDebugEnabled())
 101  
                 {
 102  0
                     logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: "
 103  
                              + durableName);
 104  
                 }
 105  
             }
 106  
 
 107  
             // Create consumer
 108  28
             consumer = support.createConsumer(session, dest, selector, connector.isNoLocal(), durableName,
 109  
                 topic);
 110  
 
 111  
             try
 112  
             {
 113  
                 Message message;
 114  
 
 115  28
                 if (timeout == JmsMessageDispatcher.RECEIVE_NO_WAIT)
 116  
                 {
 117  0
                     message = consumer.receiveNoWait();
 118  
                 }
 119  28
                 else if (timeout == JmsMessageDispatcher.RECEIVE_WAIT_INDEFINITELY)
 120  
                 {
 121  0
                     message = consumer.receive();
 122  
                 }
 123  
                 else
 124  
                 {
 125  28
                     message = consumer.receive(timeout);
 126  
                 }
 127  
 
 128  28
                 if (message == null)
 129  
                 {
 130  0
                     return null;
 131  
                 }
 132  
 
 133  28
                 message = connector.preProcessMessage(message, session);
 134  
 
 135  28
                 return new DefaultMuleMessage(connector.getMessageAdapter(message));
 136  
             }
 137  0
             catch (Exception e)
 138  
             {
 139  0
                 connector.handleException(e);
 140  0
                 return null;
 141  
             }
 142  
         }
 143  
         finally
 144  
         {
 145  28
             connector.closeQuietly(consumer);
 146  28
             connector.closeQuietly(session);
 147  
         }
 148  
     }
 149  
 
 150  
     protected void doDispose()
 151  
     {
 152  
         // template method
 153  12
     }
 154  
 
 155  
 }