View Javadoc

1   /*
2    * $Id: ObjectToJMSMessage.java 19191 2010-08-25 21:05:23Z tcarlson $
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.Message;
19  
20  /**
21   * <code>ObjectToJMSMessage</code> will convert any object to a
22   * <code>javax.jms.Message</code> or sub-type into an object. One of the 5 types of
23   * JMS message will be created based on the type of Object passed in.
24   * <ul>
25   * <li>java.lang.String - javax.jms.TextMessage</li>
26   * <li>byte[] - javax.jms.BytesMessage</li>
27   * <li>java.util.Map - javax.jms.MapMessage</li>
28   * <li>java.io.InputStream - javax.jms.StreamMessage</li>
29   * <li>java.lang.Object - javax.jms.ObjectMessage</li>
30   * </ul>
31   * Note that if compression is turned on then a <code>javax.jms.BytesMessage</code>
32   * is sent.
33   */
34  public class ObjectToJMSMessage extends AbstractJmsTransformer
35  {
36  
37      public ObjectToJMSMessage()
38      {
39          super();
40      }
41  
42      @Override
43      protected void declareInputOutputClasses()
44      {
45          setReturnDataType(DataTypeFactory.create(Message.class));
46      }
47  
48      @Override
49      public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
50      {
51          try
52          {
53              if (logger.isDebugEnabled())
54              {
55                  logger.debug("Source object is " + ClassUtils.getSimpleName(message.getPayload().getClass()));
56              }
57  
58              Object result = transformToMessage(message);
59  
60              if (logger.isDebugEnabled())
61              {
62                  logger.debug("Resulting object is " + ClassUtils.getSimpleName(result.getClass()));
63              }
64  
65              return result;
66          }
67          catch (Exception e)
68          {
69              throw new TransformerException(this, e);
70          }
71      }
72  
73  }