Coverage Report - org.mule.providers.jms.transformers.AbstractJmsTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractJmsTransformer
0%
0/44
0%
0/11
5.6
 
 1  
 /*
 2  
  * $Id: AbstractJmsTransformer.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.transformers;
 12  
 
 13  
 import org.mule.config.MuleProperties;
 14  
 import org.mule.impl.RequestContext;
 15  
 import org.mule.providers.jms.JmsConnector;
 16  
 import org.mule.providers.jms.JmsConstants;
 17  
 import org.mule.providers.jms.JmsMessageUtils;
 18  
 import org.mule.transformers.AbstractTransformer;
 19  
 import org.mule.umo.UMOEventContext;
 20  
 import org.mule.umo.UMOMessage;
 21  
 import org.mule.umo.endpoint.UMOImmutableEndpoint;
 22  
 import org.mule.umo.provider.UMOConnector;
 23  
 import org.mule.umo.transformer.TransformerException;
 24  
 import org.mule.util.ClassUtils;
 25  
 
 26  
 import java.util.Iterator;
 27  
 
 28  
 import javax.jms.Destination;
 29  
 import javax.jms.JMSException;
 30  
 import javax.jms.Message;
 31  
 import javax.jms.Session;
 32  
 
 33  
 /**
 34  
  * <code>AbstractJmsTransformer</code> is an abstract class that should be used for
 35  
  * all transformers where a JMS message will be the transformed or transformee
 36  
  * object. It provides services for compressing and uncompressing messages.
 37  
  */
 38  
 
 39  
 public abstract class AbstractJmsTransformer extends AbstractTransformer
 40  
 {
 41  
 
 42  
     public AbstractJmsTransformer()
 43  
     {
 44  0
         super();
 45  0
     }
 46  
 
 47  
     protected Message transformToMessage(Object src) throws TransformerException
 48  
     {
 49  
         try
 50  
         {
 51  
             Message result;
 52  
 
 53  0
             if (src instanceof Message)
 54  
             {
 55  0
                 result = (Message)src;
 56  0
                 result.clearProperties();
 57  
             }
 58  
             else
 59  
             {
 60  0
                 result = JmsMessageUtils.toMessage(src, this.getSession());
 61  
             }
 62  
 
 63  
             // set the event properties on the Message
 64  0
             UMOEventContext ctx = RequestContext.getEventContext();
 65  0
             if (ctx == null)
 66  
             {
 67  0
                 logger.warn("There is no current event context");
 68  0
                 return result;
 69  
             }
 70  
 
 71  0
             this.setJmsProperties(ctx.getMessage(), result);
 72  
 
 73  0
             return result;
 74  
         }
 75  0
         catch (TransformerException tex)
 76  
         {
 77  
             // rethrow
 78  0
             throw tex;
 79  
         }
 80  0
         catch (Exception e)
 81  
         {
 82  0
             throw new TransformerException(this, e);
 83  
         }
 84  
     }
 85  
 
 86  
     protected Object transformFromMessage(Message source) throws TransformerException
 87  
     {
 88  
         try
 89  
         {
 90  0
             if (logger.isDebugEnabled())
 91  
             {
 92  0
                 logger.debug("Message type received is: " +
 93  
                         ClassUtils.getSimpleName(source.getClass()));
 94  
             }
 95  
 
 96  
             // Try to figure out our endpoint's JMS Specification and fall back to
 97  
             // 1.0.2 if none is set.
 98  0
             String jmsSpec = JmsConstants.JMS_SPECIFICATION_102B;
 99  0
             UMOImmutableEndpoint endpoint = this.getEndpoint();
 100  0
             if (endpoint != null)
 101  
             {
 102  0
                 UMOConnector connector = endpoint.getConnector();
 103  0
                 if (connector instanceof JmsConnector)
 104  
                 {
 105  0
                     jmsSpec = ((JmsConnector)connector).getSpecification();
 106  
                 }
 107  
             }
 108  
 
 109  0
             return JmsMessageUtils.toObject(source, jmsSpec);
 110  
         }
 111  0
         catch (Exception e)
 112  
         {
 113  0
             throw new TransformerException(this, e);
 114  
         }
 115  
     }
 116  
 
 117  
     protected void setJmsProperties(UMOMessage umoMessage, Message msg) throws JMSException
 118  
     {
 119  0
         for (Iterator iterator = umoMessage.getPropertyNames().iterator(); iterator.hasNext();)
 120  
         {
 121  0
             String key = iterator.next().toString();
 122  
 
 123  0
             if (!JmsConstants.JMS_PROPERTY_NAMES.contains(key))
 124  
             {
 125  0
                 Object value = umoMessage.getProperty(key);
 126  
 
 127  0
                 if (MuleProperties.MULE_CORRELATION_ID_PROPERTY.equals(key))
 128  
                 {
 129  0
                     msg.setJMSCorrelationID(umoMessage.getCorrelationId());
 130  
                 }
 131  
 
 132  
                 // We dont want to set the ReplyTo property again as it will be set
 133  
                 // using JMSReplyTo
 134  0
                 if (!(MuleProperties.MULE_REPLY_TO_PROPERTY.equals(key) && value instanceof Destination))
 135  
                 {
 136  
                     // sanitize key as JMS header
 137  0
                     key = JmsMessageUtils.encodeHeader(key);
 138  
 
 139  
                     try
 140  
                     {
 141  0
                         msg.setObjectProperty(key, value);
 142  
                     }
 143  0
                     catch (JMSException e)
 144  
                     {
 145  
                         // Various JMS servers have slightly different rules to what
 146  
                         // can be set as an object property on the message; therefore
 147  
                         // we have to take a hit n' hope approach
 148  0
                         if (logger.isDebugEnabled())
 149  
                         {
 150  0
                             logger.debug("Unable to set property '" + key + "' of type "
 151  
                                     + ClassUtils.getSimpleName(value.getClass())
 152  
                                     + "': " + e.getMessage());
 153  
                         }
 154  0
                     }
 155  
                 }
 156  
             }
 157  
         }
 158  0
     }
 159  
 
 160  
     protected Session getSession() throws TransformerException, JMSException
 161  
     {
 162  0
         if (endpoint != null)
 163  
         {
 164  0
             return ((JmsConnector)endpoint.getConnector()).getSession(endpoint);
 165  
         }
 166  
         else
 167  
         {
 168  0
             throw new TransformerException(this, new IllegalStateException(
 169  
                 "This transformer needs a valid endpoint"));
 170  
         }
 171  
     }
 172  
 
 173  
 }