View Javadoc

1   /*
2    * $Id: JmsMuleMessageFactory.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;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleContext;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.config.MuleProperties;
17  import org.mule.transport.AbstractMuleMessageFactory;
18  
19  import java.util.Enumeration;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.jms.Destination;
24  import javax.jms.JMSException;
25  import javax.jms.Message;
26  
27  public class JmsMuleMessageFactory extends AbstractMuleMessageFactory
28  {
29      public JmsMuleMessageFactory(MuleContext context)
30      {
31          super(context);
32      }
33  
34      @Override
35      protected Class<?>[] getSupportedTransportMessageTypes()
36      {
37          return new Class[]{ Message.class };
38      }
39  
40      @Override
41      protected Object extractPayload(Object transportMessage, String encoding) throws Exception
42      {
43          return transportMessage;
44      }
45  
46      @Override
47      protected void addProperties(DefaultMuleMessage muleMessage, Object transportMessage) throws Exception
48      {        
49          Message jmsMessage = (Message) transportMessage;
50          
51          Map<String, Object> messageProperties = new HashMap<String, Object>();
52          addCorrelationProperties(jmsMessage, muleMessage, messageProperties);
53          addDeliveryModeProperty(jmsMessage, messageProperties);
54          addDestinationProperty(jmsMessage, messageProperties);
55          addExpirationProperty(jmsMessage, messageProperties);
56          addMessageIdProperty(jmsMessage, messageProperties);
57          addPriorityProperty(jmsMessage, messageProperties);
58          addRedeliveredProperty(jmsMessage, messageProperties);
59          addJMSReplyTo(muleMessage, jmsMessage);
60          addTimestampProperty(jmsMessage, messageProperties);
61          addTypeProperty(jmsMessage, messageProperties);
62  
63          propagateJMSProperties(jmsMessage, messageProperties);
64          
65          muleMessage.addInboundProperties(messageProperties);
66      }
67  
68      protected void propagateJMSProperties(Message jmsMessage, Map<String, Object> messageProperties)
69      {
70          try
71          {
72              Enumeration<?> e = jmsMessage.getPropertyNames();
73              while (e.hasMoreElements())
74              {
75                  String key = (String) e.nextElement();
76                  try
77                  {
78                      Object value = jmsMessage.getObjectProperty(key);
79                      if (value != null)
80                      {
81                          messageProperties.put(key, value);
82                      }
83                  }
84                  catch (JMSException e1)
85                  {
86                      // ignored
87                  }
88              }
89          }
90          catch (JMSException e1)
91          {
92              // ignored
93          }
94      }
95  
96      protected void addTypeProperty(Message jmsMessage, Map<String, Object> messageProperties)
97      {
98          try
99          {
100             String value = jmsMessage.getJMSType();
101             if (value != null)
102             {
103                 messageProperties.put(JmsConstants.JMS_TYPE, value);
104             }
105         }
106         catch (JMSException e)
107         {
108             // ignored
109         }
110     }
111 
112     protected void addTimestampProperty(Message jmsMessage, Map<String, Object> messageProperties)
113     {
114         try
115         {
116             long value = jmsMessage.getJMSTimestamp();
117             messageProperties.put(JmsConstants.JMS_TIMESTAMP, Long.valueOf(value));
118         }
119         catch (JMSException e)
120         {
121             // ignored
122         }
123     }
124 
125     protected void addJMSReplyTo(MuleMessage muleMessage, Message jmsMessage)
126     {
127         try
128         {
129             Destination replyTo = jmsMessage.getJMSReplyTo();
130             if (replyTo != null)
131             {
132                 muleMessage.setOutboundProperty(JmsConstants.JMS_REPLY_TO, replyTo);
133             }
134 
135             muleMessage.setReplyTo(replyTo);
136         }
137         catch (JMSException e)
138         {
139             // ignored
140         }
141     }
142 
143     protected void addRedeliveredProperty(Message jmsMessage, Map<String, Object> messageProperties)
144     {
145         try
146         {
147             boolean value = jmsMessage.getJMSRedelivered();
148             messageProperties.put(JmsConstants.JMS_REDELIVERED, Boolean.valueOf(value));
149         }
150         catch (JMSException e)
151         {
152             // ignored
153         }
154     }
155 
156     protected void addPriorityProperty(Message jmsMessage, Map<String, Object> messageProperties)
157     {
158         try
159         {
160             int value = jmsMessage.getJMSPriority();
161             messageProperties.put(JmsConstants.JMS_PRIORITY, Integer.valueOf(value));
162         }
163         catch (JMSException e)
164         {
165             // ignored
166         }
167     }
168 
169     protected void addMessageIdProperty(Message jmsMessage, Map<String, Object> messageProperties)
170     {
171         try
172         {
173             String value = jmsMessage.getJMSMessageID();
174             if (value != null)
175             {
176                 messageProperties.put(JmsConstants.JMS_MESSAGE_ID, value);
177                 messageProperties.put(MuleProperties.MULE_MESSAGE_ID_PROPERTY, value);
178             }
179         }
180         catch (JMSException e)
181         {
182             // ignored
183         }
184     }
185 
186     protected void addExpirationProperty(Message jmsMessage, Map<String, Object> messageProperties)
187     {
188         try
189         {
190             long value = jmsMessage.getJMSExpiration();
191             messageProperties.put(JmsConstants.JMS_EXPIRATION, Long.valueOf(value));
192         }
193         catch (JMSException e)
194         {
195             // ignored
196         }
197     }
198 
199     protected void addDestinationProperty(Message jmsMessage, Map<String, Object> messageProperties)
200     {
201         try
202         {
203             Destination value = jmsMessage.getJMSDestination();
204             if (value != null)
205             {
206                 messageProperties.put(JmsConstants.JMS_DESTINATION, value);
207             }
208         }
209         catch (JMSException e)
210         {
211             // ignored
212         }
213     }
214 
215     protected void addDeliveryModeProperty(Message jmsMessage, Map<String, Object> messageProperties)
216     {
217         try
218         {
219             int value = jmsMessage.getJMSDeliveryMode();
220             messageProperties.put(JmsConstants.JMS_DELIVERY_MODE, Integer.valueOf(value));
221         }
222         catch (JMSException e)
223         {
224             // ignored
225         }
226     }
227 
228     protected void addCorrelationProperties(Message jmsMessage, MuleMessage muleMessage, 
229         Map<String, Object> messageProperties)
230     {
231         try
232         {
233             String value = jmsMessage.getJMSCorrelationID();
234             if (value != null)
235             {
236                 messageProperties.put(JmsConstants.JMS_CORRELATION_ID, value);
237                 // this property is used my getCorrelationId in MuleMessage, but we want
238                 // it on the INBOUND scoped properties so don't use setCorrelationId
239                 messageProperties.put(MuleProperties.MULE_CORRELATION_ID_PROPERTY, value);
240             }
241         }
242         catch (JMSException e)
243         {
244             // ignored
245         }
246     }
247 }