View Javadoc

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