View Javadoc

1   /*
2    * $Id: JMSMessageToObject.java 19250 2010-08-30 16:53:14Z dirk.olmes $
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          super();
45      }
46  
47      @Override
48      protected void declareInputOutputClasses()
49      {
50          registerSourceType(DataTypeFactory.create(Message.class));
51          registerSourceType(DataTypeFactory.create(TextMessage.class));
52          registerSourceType(DataTypeFactory.create(ObjectMessage.class));
53          registerSourceType(DataTypeFactory.create(BytesMessage.class));
54          registerSourceType(DataTypeFactory.create(MapMessage.class));
55          registerSourceType(DataTypeFactory.create(StreamMessage.class));
56      }
57  
58      @Override
59      public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
60      {
61          try
62          {
63              if (logger.isDebugEnabled())
64              {
65                  logger.debug("Source object is " + ClassUtils.getSimpleName(message.getPayload().getClass()));
66              }
67  
68              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              if (returnType.getType().equals(byte[].class) && result instanceof String)
73              {
74                  result = result.toString().getBytes(outputEncoding);
75              }
76              else if (returnType.getType().equals(String.class) && result instanceof byte[])
77              {
78                  result = new String((byte[]) result, outputEncoding);
79              }
80  
81  
82              if (logger.isDebugEnabled())
83              {
84                  logger.debug("Resulting object is " + ClassUtils.getSimpleName(result.getClass()));
85              }
86  
87              return result;
88          }
89          catch (Exception e)
90          {
91              throw new TransformerException(this, e);
92          }
93      }
94  }