Coverage Report - org.mule.providers.jms.JmsReplyToHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsReplyToHandler
0%
0/47
0%
0/10
7
 
 1  
 /*
 2  
  * $Id: JmsReplyToHandler.java 7976 2007-08-21 14:26:13Z 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.providers.jms;
 12  
 
 13  
 import org.mule.impl.model.AbstractComponent;
 14  
 import org.mule.providers.DefaultReplyToHandler;
 15  
 import org.mule.providers.jms.i18n.JmsMessages;
 16  
 import org.mule.umo.UMOEvent;
 17  
 import org.mule.umo.UMOException;
 18  
 import org.mule.umo.UMOMessage;
 19  
 import org.mule.umo.provider.DispatchException;
 20  
 import org.mule.umo.transformer.UMOTransformer;
 21  
 
 22  
 import javax.jms.DeliveryMode;
 23  
 import javax.jms.Destination;
 24  
 import javax.jms.Message;
 25  
 import javax.jms.MessageProducer;
 26  
 import javax.jms.Session;
 27  
 
 28  
 /**
 29  
  * <code>JmsReplyToHandler</code> will process a JMS replyTo or hand off to the
 30  
  * default replyTo handler if the replyTo is a URL.
 31  
  */
 32  
 public class JmsReplyToHandler extends DefaultReplyToHandler
 33  
 {
 34  
     private final JmsConnector connector;
 35  
 
 36  
     public JmsReplyToHandler(JmsConnector connector, UMOTransformer transformer)
 37  
     {
 38  0
         super(transformer);
 39  0
         this.connector = connector;
 40  0
     }
 41  
 
 42  
     public void processReplyTo(UMOEvent event, UMOMessage returnMessage, Object replyTo) throws UMOException
 43  
     {
 44  0
         Destination replyToDestination = null;
 45  0
         MessageProducer replyToProducer = null;
 46  0
         Session session = null;
 47  
         try
 48  
         {
 49  
             // now we need to send the response
 50  0
             if (replyTo instanceof Destination)
 51  
             {
 52  0
                 replyToDestination = (Destination)replyTo;
 53  
             }
 54  0
             if (replyToDestination == null)
 55  
             {
 56  0
                 super.processReplyTo(event, returnMessage, replyTo);
 57  
                 return;
 58  
             }
 59  0
             Object payload = returnMessage.getPayload();
 60  0
             if (getTransformer() != null)
 61  
             {
 62  0
                 getTransformer().setEndpoint(getEndpoint(event, "jms://temporary"));
 63  0
                 if (getTransformer().isSourceTypeSupported(payload.getClass()))
 64  
                 {
 65  0
                     payload = getTransformer().transform(payload);
 66  
                 }
 67  0
                 else if (logger.isDebugEnabled())
 68  
                 {
 69  0
                     logger.debug("Transformer for replyTo Handler: " + getTransformer().toString()
 70  
                                  + " does not support source type: " + payload.getClass()
 71  
                                  + ". Not doing a transform");
 72  
                 }
 73  
             }
 74  
 
 75  0
             final boolean topic = connector.getTopicResolver().isTopic(replyToDestination);
 76  0
             session = connector.getSession(false, topic);
 77  0
             Message replyToMessage = JmsMessageUtils.toMessage(payload, session);
 78  
 
 79  0
             replyToMessage.setJMSReplyTo(null);
 80  0
             if (logger.isDebugEnabled())
 81  
             {
 82  0
                 logger.debug("Sending jms reply to: " + replyToDestination + "("
 83  
                              + replyToDestination.getClass().getName() + ")");
 84  
             }
 85  0
             replyToProducer = connector.getJmsSupport().createProducer(session, replyToDestination, topic);
 86  
 
 87  
             // QoS support
 88  0
             UMOMessage eventMsg = event.getMessage();
 89  0
             String ttlString = (String)eventMsg.removeProperty(JmsConstants.TIME_TO_LIVE_PROPERTY);
 90  0
             String priorityString = (String)eventMsg.removeProperty(JmsConstants.PRIORITY_PROPERTY);
 91  0
             String persistentDeliveryString = (String)eventMsg.removeProperty(JmsConstants.PERSISTENT_DELIVERY_PROPERTY);
 92  
 
 93  0
             if (ttlString == null && priorityString == null && persistentDeliveryString == null)
 94  
             {
 95  0
                 connector.getJmsSupport().send(replyToProducer, replyToMessage, topic);
 96  
             }
 97  
             else
 98  
             {
 99  0
                 long ttl = Message.DEFAULT_TIME_TO_LIVE;
 100  0
                 int priority = Message.DEFAULT_PRIORITY;
 101  
 
 102  
                 // TODO this first assignment is ignored anyway, review and remove if need to
 103  0
                 boolean persistent = Message.DEFAULT_DELIVERY_MODE == DeliveryMode.PERSISTENT;
 104  
 
 105  0
                 if (ttlString != null)
 106  
                 {
 107  0
                     ttl = Long.parseLong(ttlString);
 108  
                 }
 109  0
                 if (priorityString != null)
 110  
                 {
 111  0
                     priority = Integer.parseInt(priorityString);
 112  
                 }
 113  
                 // TODO StringUtils.notBlank() would be more robust here
 114  0
                 persistent = persistentDeliveryString != null
 115  
                                 ? Boolean.valueOf(persistentDeliveryString).booleanValue()
 116  
                                 : connector.isPersistentDelivery();
 117  
 
 118  0
                 connector.getJmsSupport().send(replyToProducer, replyToMessage, persistent, priority, ttl,
 119  
                     topic);
 120  
             }
 121  
 
 122  
             // connector.getJmsSupport().send(replyToProducer, replyToMessage,
 123  
             // replyToDestination);
 124  0
             logger.info("Reply Message sent to: " + replyToDestination);
 125  0
             ((AbstractComponent)event.getComponent()).getStatistics().incSentReplyToEvent();
 126  
         }
 127  0
         catch (Exception e)
 128  
         {
 129  0
             throw new DispatchException(
 130  
                 JmsMessages.failedToCreateAndDispatchResponse(replyToDestination), returnMessage, null, e);
 131  
         }
 132  
         finally
 133  
         {
 134  0
             connector.closeQuietly(replyToProducer);
 135  0
             connector.closeQuietly(session);
 136  0
         }
 137  0
     }
 138  
 }