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