1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.jms; |
12 | |
|
13 | |
import org.mule.config.MuleProperties; |
14 | |
import org.mule.impl.ThreadSafeAccess; |
15 | |
import org.mule.providers.AbstractMessageAdapter; |
16 | |
import org.mule.umo.MessagingException; |
17 | |
import org.mule.umo.provider.MessageTypeNotSupportedException; |
18 | |
|
19 | |
import java.util.Enumeration; |
20 | |
|
21 | |
import javax.jms.Destination; |
22 | |
import javax.jms.JMSException; |
23 | |
import javax.jms.Message; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class JmsMessageAdapter extends AbstractMessageAdapter |
33 | |
{ |
34 | |
|
35 | |
|
36 | |
|
37 | |
private static final long serialVersionUID = -8151716840620558143L; |
38 | |
|
39 | |
private String jmsSpec; |
40 | |
private Message jmsMessage; |
41 | |
|
42 | |
public JmsMessageAdapter(Object message) throws MessagingException |
43 | |
{ |
44 | 92 | super(); |
45 | 94 | this.setMessage(message); |
46 | 90 | } |
47 | |
|
48 | |
protected JmsMessageAdapter(JmsMessageAdapter template) |
49 | |
{ |
50 | 18 | super(template); |
51 | 18 | jmsSpec = template.jmsSpec; |
52 | 18 | jmsMessage = template.jmsMessage; |
53 | 17 | } |
54 | |
|
55 | |
public void setSpecification(String newSpec) |
56 | |
{ |
57 | 90 | if (JmsConstants.JMS_SPECIFICATION_11.equals(newSpec) |
58 | |
|| (JmsConstants.JMS_SPECIFICATION_102B.equals(newSpec))) |
59 | |
{ |
60 | 87 | this.jmsSpec = newSpec; |
61 | |
} |
62 | |
else |
63 | |
|
64 | |
{ |
65 | 2 | throw new IllegalArgumentException( |
66 | |
"JMS specification needs to be one of the defined values in JmsConstants but was: " + newSpec); |
67 | |
} |
68 | 85 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public String getPayloadAsString(String encoding) throws Exception |
79 | |
{ |
80 | 4 | return new String(getPayloadAsBytes(), encoding); |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public byte[] getPayloadAsBytes() throws Exception |
90 | |
{ |
91 | 6 | return JmsMessageUtils.toByteArray(jmsMessage, jmsSpec); |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public Object getPayload() |
98 | |
{ |
99 | 182 | return jmsMessage; |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private void setMessage(Object message) throws MessagingException |
106 | |
{ |
107 | 94 | if (message instanceof Message) |
108 | |
{ |
109 | 90 | this.jmsMessage = (Message)message; |
110 | |
} |
111 | |
else |
112 | |
{ |
113 | 2 | throw new MessageTypeNotSupportedException(message, getClass()); |
114 | |
} |
115 | |
|
116 | |
try |
117 | |
{ |
118 | 91 | String value = this.jmsMessage.getJMSCorrelationID(); |
119 | 92 | if (value != null) |
120 | |
{ |
121 | 32 | setProperty(JmsConstants.JMS_CORRELATION_ID, value); |
122 | |
} |
123 | |
} |
124 | 0 | catch (JMSException e) |
125 | |
{ |
126 | |
|
127 | 92 | } |
128 | |
|
129 | |
try |
130 | |
{ |
131 | 90 | int value = this.jmsMessage.getJMSDeliveryMode(); |
132 | 91 | setProperty(JmsConstants.JMS_DELIVERY_MODE, new Integer(value)); |
133 | |
} |
134 | 0 | catch (JMSException e) |
135 | |
{ |
136 | |
|
137 | 91 | } |
138 | |
|
139 | |
try |
140 | |
{ |
141 | 91 | Destination value = this.jmsMessage.getJMSDestination(); |
142 | 90 | if (value != null) |
143 | |
{ |
144 | 81 | setProperty(JmsConstants.JMS_DESTINATION, value); |
145 | |
} |
146 | |
} |
147 | 0 | catch (JMSException e) |
148 | |
{ |
149 | |
|
150 | 90 | } |
151 | |
|
152 | |
try |
153 | |
{ |
154 | 90 | long value = this.jmsMessage.getJMSExpiration(); |
155 | 90 | setProperty(JmsConstants.JMS_EXPIRATION, new Long(value)); |
156 | |
} |
157 | 0 | catch (JMSException e) |
158 | |
{ |
159 | |
|
160 | 91 | } |
161 | |
|
162 | |
try |
163 | |
{ |
164 | 88 | String value = this.jmsMessage.getJMSMessageID(); |
165 | 91 | if (value != null) |
166 | |
{ |
167 | 92 | setProperty(JmsConstants.JMS_MESSAGE_ID, value); |
168 | |
} |
169 | |
} |
170 | 0 | catch (JMSException e) |
171 | |
{ |
172 | |
|
173 | 92 | } |
174 | |
|
175 | |
try |
176 | |
{ |
177 | 89 | int value = this.jmsMessage.getJMSPriority(); |
178 | 92 | setProperty(JmsConstants.JMS_PRIORITY, new Integer(value)); |
179 | |
} |
180 | 0 | catch (JMSException e) |
181 | |
{ |
182 | |
|
183 | 91 | } |
184 | |
|
185 | |
try |
186 | |
{ |
187 | 92 | boolean value = this.jmsMessage.getJMSRedelivered(); |
188 | 91 | setProperty(JmsConstants.JMS_REDELIVERED, Boolean.valueOf(value)); |
189 | |
} |
190 | 0 | catch (JMSException e) |
191 | |
{ |
192 | |
|
193 | 91 | } |
194 | |
|
195 | |
try |
196 | |
{ |
197 | 90 | Destination value = this.jmsMessage.getJMSReplyTo(); |
198 | 92 | if (value != null) |
199 | |
{ |
200 | 8 | setProperty(JmsConstants.JMS_REPLY_TO, value); |
201 | |
} |
202 | |
} |
203 | 0 | catch (JMSException e) |
204 | |
{ |
205 | |
|
206 | 90 | } |
207 | |
|
208 | |
try |
209 | |
{ |
210 | 91 | long value = this.jmsMessage.getJMSTimestamp(); |
211 | 88 | setProperty(JmsConstants.JMS_TIMESTAMP, new Long(value)); |
212 | |
} |
213 | 0 | catch (JMSException e) |
214 | |
{ |
215 | |
|
216 | 90 | } |
217 | |
|
218 | |
try |
219 | |
{ |
220 | 90 | String value = this.jmsMessage.getJMSType(); |
221 | 90 | if (value != null) |
222 | |
{ |
223 | 0 | setProperty(JmsConstants.JMS_TYPE, value); |
224 | |
} |
225 | |
} |
226 | 0 | catch (JMSException e) |
227 | |
{ |
228 | |
|
229 | 91 | } |
230 | |
|
231 | |
try |
232 | |
{ |
233 | 91 | Enumeration e = this.jmsMessage.getPropertyNames(); |
234 | 242 | while (e.hasMoreElements()) |
235 | |
{ |
236 | 149 | String key = (String)e.nextElement(); |
237 | |
try |
238 | |
{ |
239 | 149 | Object value = this.jmsMessage.getObjectProperty(key); |
240 | 150 | if (value != null) |
241 | |
{ |
242 | 150 | setProperty(key, value); |
243 | |
} |
244 | |
} |
245 | 0 | catch (JMSException e1) |
246 | |
{ |
247 | |
|
248 | 150 | } |
249 | 149 | } |
250 | |
} |
251 | 0 | catch (JMSException e1) |
252 | |
{ |
253 | |
|
254 | 91 | } |
255 | 91 | } |
256 | |
|
257 | |
public String getUniqueId() |
258 | |
{ |
259 | 104 | return (String)getProperty(JmsConstants.JMS_MESSAGE_ID); |
260 | |
} |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
public void setCorrelationId(String id) |
274 | |
{ |
275 | 0 | setProperty(JmsConstants.JMS_CORRELATION_ID, id); |
276 | 0 | } |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
public String getCorrelationId() |
291 | |
{ |
292 | 2 | return (String)getProperty(JmsConstants.JMS_CORRELATION_ID); |
293 | |
} |
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
public void setReplyTo(Object replyTo) |
304 | |
{ |
305 | 0 | if (replyTo instanceof Destination) |
306 | |
{ |
307 | 0 | setProperty(JmsConstants.JMS_REPLY_TO, replyTo); |
308 | |
} |
309 | |
else |
310 | |
{ |
311 | 0 | super.setReplyTo(replyTo); |
312 | |
} |
313 | 0 | } |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
public Object getReplyTo() |
324 | |
{ |
325 | 108 | Object replyTo = getProperty(JmsConstants.JMS_REPLY_TO); |
326 | 106 | if (replyTo == null) |
327 | |
{ |
328 | 91 | replyTo = getProperty(MuleProperties.MULE_REPLY_TO_PROPERTY); |
329 | |
} |
330 | 107 | return replyTo; |
331 | |
} |
332 | |
|
333 | |
public ThreadSafeAccess newThreadCopy() |
334 | |
{ |
335 | 18 | return new JmsMessageAdapter(this); |
336 | |
} |
337 | |
|
338 | |
} |