View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.jms.transformers;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.transformer.TransformerException;
11  import org.mule.transformer.types.DataTypeFactory;
12  import org.mule.util.ClassUtils;
13  
14  import javax.jms.BytesMessage;
15  import javax.jms.MapMessage;
16  import javax.jms.Message;
17  import javax.jms.ObjectMessage;
18  import javax.jms.StreamMessage;
19  import javax.jms.TextMessage;
20  
21  /**
22   * <code>JMSMessageToObject</code> Will convert a <code>javax.jms.Message</code>
23   * or sub-type into an object by extracting the message payload. Users of this
24   * transformer can set different return types on the transform to control the way it
25   * behaves.
26   * <ul>
27   * <li>javax.jms.TextMessage - java.lang.String</li>
28   * <li>javax.jms.ObjectMessage - java.lang.Object</li>
29   * <li>javax.jms.BytesMessage - Byte[]. Note that the transformer will check if the
30   * payload is compressed and automatically uncompress the message.</li>
31   * <li>javax.jms.MapMessage - java.util.Map</li>
32   * <li>javax.jms.StreamMessage - java.util.Vector of objects from the Stream
33   * Message.</li>
34   * </ul>
35   */
36  public class JMSMessageToObject extends AbstractJmsTransformer
37  {
38      public JMSMessageToObject()
39      {
40          super();
41      }
42  
43      @Override
44      protected void declareInputOutputClasses()
45      {
46          registerSourceType(DataTypeFactory.create(Message.class));
47          registerSourceType(DataTypeFactory.create(TextMessage.class));
48          registerSourceType(DataTypeFactory.create(ObjectMessage.class));
49          registerSourceType(DataTypeFactory.create(BytesMessage.class));
50          registerSourceType(DataTypeFactory.create(MapMessage.class));
51          registerSourceType(DataTypeFactory.create(StreamMessage.class));
52      }
53  
54      @Override
55      public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
56      {
57          try
58          {
59              if (logger.isDebugEnabled())
60              {
61                  logger.debug("Source object is " + ClassUtils.getSimpleName(message.getPayload().getClass()));
62              }
63  
64              Object result = transformFromMessage((Message) message.getPayload(), outputEncoding);
65  
66              // We need to handle String / byte[] explicitly since this transformer does not define
67              // a single return type
68              if (returnType.getType().equals(byte[].class) && result instanceof String)
69              {
70                  result = result.toString().getBytes(outputEncoding);
71              }
72              else if (returnType.getType().equals(String.class) && result instanceof byte[])
73              {
74                  result = new String((byte[]) result, outputEncoding);
75              }
76  
77  
78              if (logger.isDebugEnabled())
79              {
80                  logger.debug("Resulting object is " + ClassUtils.getSimpleName(result.getClass()));
81              }
82  
83              return result;
84          }
85          catch (Exception e)
86          {
87              throw new TransformerException(this, e);
88          }
89      }
90  }