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