Coverage Report - org.mule.transport.jms.transformers.JMSMessageToObject
 
Classes in this File Line Coverage Branch Coverage Complexity
JMSMessageToObject
0%
0/21
0%
0/12
0
 
 1  
 /*
 2  
  * $Id: JMSMessageToObject.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.transformers;
 12  
 
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.transformer.TransformerException;
 15  
 import org.mule.transformer.types.DataTypeFactory;
 16  
 import org.mule.util.ClassUtils;
 17  
 
 18  
 import javax.jms.BytesMessage;
 19  
 import javax.jms.MapMessage;
 20  
 import javax.jms.Message;
 21  
 import javax.jms.ObjectMessage;
 22  
 import javax.jms.StreamMessage;
 23  
 import javax.jms.TextMessage;
 24  
 
 25  
 /**
 26  
  * <code>JMSMessageToObject</code> Will convert a <code>javax.jms.Message</code>
 27  
  * or sub-type into an object by extracting the message payload. Users of this
 28  
  * transformer can set different return types on the transform to control the way it
 29  
  * behaves.
 30  
  * <ul>
 31  
  * <li>javax.jms.TextMessage - java.lang.String</li>
 32  
  * <li>javax.jms.ObjectMessage - java.lang.Object</li>
 33  
  * <li>javax.jms.BytesMessage - Byte[]. Note that the transformer will check if the
 34  
  * payload is compressed and automatically uncompress the message.</li>
 35  
  * <li>javax.jms.MapMessage - java.util.Map</li>
 36  
  * <li>javax.jms.StreamMessage - java.util.Vector of objects from the Stream
 37  
  * Message.</li>
 38  
  * </ul>
 39  
  */
 40  
 public class JMSMessageToObject extends AbstractJmsTransformer
 41  
 {
 42  
     public JMSMessageToObject()
 43  
     {
 44  0
         super();
 45  0
     }
 46  
 
 47  
     @Override
 48  
     protected void declareInputOutputClasses()
 49  
     {
 50  0
         registerSourceType(DataTypeFactory.create(Message.class));
 51  0
         registerSourceType(DataTypeFactory.create(TextMessage.class));
 52  0
         registerSourceType(DataTypeFactory.create(ObjectMessage.class));
 53  0
         registerSourceType(DataTypeFactory.create(BytesMessage.class));
 54  0
         registerSourceType(DataTypeFactory.create(MapMessage.class));
 55  0
         registerSourceType(DataTypeFactory.create(StreamMessage.class));
 56  0
     }
 57  
 
 58  
     @Override
 59  
     public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
 60  
     {
 61  
         try
 62  
         {
 63  0
             if (logger.isDebugEnabled())
 64  
             {
 65  0
                 logger.debug("Source object is " + ClassUtils.getSimpleName(message.getPayload().getClass()));
 66  
             }
 67  
 
 68  0
             Object result = transformFromMessage((Message) message.getPayload(), outputEncoding);
 69  
 
 70  
             // We need to handle String / byte[] explicitly since this transformer does not define
 71  
             // a single return type
 72  0
             if (returnType.getType().equals(byte[].class) && result instanceof String)
 73  
             {
 74  0
                 result = result.toString().getBytes(outputEncoding);
 75  
             }
 76  0
             else if (returnType.getType().equals(String.class) && result instanceof byte[])
 77  
             {
 78  0
                 result = new String((byte[]) result, outputEncoding);
 79  
             }
 80  
 
 81  
 
 82  0
             if (logger.isDebugEnabled())
 83  
             {
 84  0
                 logger.debug("Resulting object is " + ClassUtils.getSimpleName(result.getClass()));
 85  
             }
 86  
 
 87  0
             return result;
 88  
         }
 89  0
         catch (Exception e)
 90  
         {
 91  0
             throw new TransformerException(this, e);
 92  
         }
 93  
     }
 94  
 }