View Javadoc

1   /*
2    * $Id: AbstractJmsTransformer.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.providers.jms.transformers;
12  
13  import org.mule.config.MuleProperties;
14  import org.mule.impl.RequestContext;
15  import org.mule.providers.jms.JmsConnector;
16  import org.mule.providers.jms.JmsConstants;
17  import org.mule.providers.jms.JmsMessageUtils;
18  import org.mule.transformers.AbstractTransformer;
19  import org.mule.umo.UMOEventContext;
20  import org.mule.umo.UMOMessage;
21  import org.mule.umo.endpoint.UMOImmutableEndpoint;
22  import org.mule.umo.provider.UMOConnector;
23  import org.mule.umo.transformer.TransformerException;
24  import org.mule.util.ClassUtils;
25  
26  import java.util.Iterator;
27  
28  import javax.jms.Destination;
29  import javax.jms.JMSException;
30  import javax.jms.Message;
31  import javax.jms.Session;
32  
33  /**
34   * <code>AbstractJmsTransformer</code> is an abstract class that should be used for
35   * all transformers where a JMS message will be the transformed or transformee
36   * object. It provides services for compressing and uncompressing messages.
37   */
38  
39  public abstract class AbstractJmsTransformer extends AbstractTransformer
40  {
41  
42      public AbstractJmsTransformer()
43      {
44          super();
45      }
46  
47      protected Message transformToMessage(Object src) throws TransformerException
48      {
49          try
50          {
51              Message result;
52  
53              if (src instanceof Message)
54              {
55                  result = (Message)src;
56                  result.clearProperties();
57              }
58              else
59              {
60                  result = JmsMessageUtils.toMessage(src, this.getSession());
61              }
62  
63              // set the event properties on the Message
64              UMOEventContext ctx = RequestContext.getEventContext();
65              if (ctx == null)
66              {
67                  logger.warn("There is no current event context");
68                  return result;
69              }
70  
71              this.setJmsProperties(ctx.getMessage(), result);
72  
73              return result;
74          }
75          catch (TransformerException tex)
76          {
77              // rethrow
78              throw tex;
79          }
80          catch (Exception e)
81          {
82              throw new TransformerException(this, e);
83          }
84      }
85  
86      protected Object transformFromMessage(Message source) throws TransformerException
87      {
88          try
89          {
90              if (logger.isDebugEnabled())
91              {
92                  logger.debug("Message type received is: " +
93                          ClassUtils.getSimpleName(source.getClass()));
94              }
95  
96              // Try to figure out our endpoint's JMS Specification and fall back to
97              // 1.0.2 if none is set.
98              String jmsSpec = JmsConstants.JMS_SPECIFICATION_102B;
99              UMOImmutableEndpoint endpoint = this.getEndpoint();
100             if (endpoint != null)
101             {
102                 UMOConnector connector = endpoint.getConnector();
103                 if (connector instanceof JmsConnector)
104                 {
105                     jmsSpec = ((JmsConnector)connector).getSpecification();
106                 }
107             }
108 
109             return JmsMessageUtils.toObject(source, jmsSpec);
110         }
111         catch (Exception e)
112         {
113             throw new TransformerException(this, e);
114         }
115     }
116 
117     protected void setJmsProperties(UMOMessage umoMessage, Message msg) throws JMSException
118     {
119         for (Iterator iterator = umoMessage.getPropertyNames().iterator(); iterator.hasNext();)
120         {
121             String key = iterator.next().toString();
122 
123             if (!JmsConstants.JMS_PROPERTY_NAMES.contains(key))
124             {
125                 Object value = umoMessage.getProperty(key);
126 
127                 if (MuleProperties.MULE_CORRELATION_ID_PROPERTY.equals(key))
128                 {
129                     msg.setJMSCorrelationID(umoMessage.getCorrelationId());
130                 }
131 
132                 // We dont want to set the ReplyTo property again as it will be set
133                 // using JMSReplyTo
134                 if (!(MuleProperties.MULE_REPLY_TO_PROPERTY.equals(key) && value instanceof Destination))
135                 {
136                     // sanitize key as JMS header
137                     key = JmsMessageUtils.encodeHeader(key);
138 
139                     try
140                     {
141                         msg.setObjectProperty(key, value);
142                     }
143                     catch (JMSException e)
144                     {
145                         // Various JMS servers have slightly different rules to what
146                         // can be set as an object property on the message; therefore
147                         // we have to take a hit n' hope approach
148                         if (logger.isDebugEnabled())
149                         {
150                             logger.debug("Unable to set property '" + key + "' of type "
151                                     + ClassUtils.getSimpleName(value.getClass())
152                                     + "': " + e.getMessage());
153                         }
154                     }
155                 }
156             }
157         }
158     }
159 
160     protected Session getSession() throws TransformerException, JMSException
161     {
162         if (endpoint != null)
163         {
164             return ((JmsConnector)endpoint.getConnector()).getSession(endpoint);
165         }
166         else
167         {
168             throw new TransformerException(this, new IllegalStateException(
169                 "This transformer needs a valid endpoint"));
170         }
171     }
172 
173 }