View Javadoc

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