KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > jms > JmsMessageAdapter


1 /*
2  * $Id: JmsMessageAdapter.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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;
12
13 import java.util.Enumeration JavaDoc;
14
15 import javax.jms.Destination JavaDoc;
16 import javax.jms.JMSException JavaDoc;
17 import javax.jms.Message JavaDoc;
18
19 import org.mule.config.MuleProperties;
20 import org.mule.providers.AbstractMessageAdapter;
21 import org.mule.umo.MessagingException;
22 import org.mule.umo.provider.MessageTypeNotSupportedException;
23
24 /**
25  * <code>JmsMessageAdapter</code> allows a <code>MuleEvent</code> to access the
26  * properties and payload of a JMS Message in a uniform way. The JmsMessageAdapter
27  * expects a message of type <i>javax.jms.Message</i> and will throw an
28  * IllegalArgumentException if the source message type is not compatible. The
29  * JmsMessageAdapter should be suitable for all JMS Connector implementations.
30  */

31 public class JmsMessageAdapter extends AbstractMessageAdapter
32 {
33     /**
34      * Serial version
35      */

36     private static final long serialVersionUID = -8151716840620558143L;
37
38     private String JavaDoc jmsSpec;
39     private Message JavaDoc jmsMessage;
40
41     public JmsMessageAdapter(Object JavaDoc message) throws MessagingException
42     {
43         super();
44         this.setMessage(message);
45     }
46
47     public void setSpecification(String JavaDoc newSpec)
48     {
49         if (JmsConstants.JMS_SPECIFICATION_11.equals(newSpec)
50             || (JmsConstants.JMS_SPECIFICATION_102B.equals(newSpec)))
51         {
52             this.jmsSpec = newSpec;
53         }
54         else
55
56         {
57             throw new IllegalArgumentException JavaDoc(
58                 "JMS specification needs to be one of the defined values in JmsConstants but was: " + newSpec);
59         }
60     }
61
62     /**
63      * Converts the message implementation into a String representation
64      *
65      * @param encoding The encoding to use when transforming the message (if
66      * necessary). The parameter is used when converting from a byte array
67      * @return String representation of the message payload
68      * @throws Exception Implementation may throw an endpoint specific exception
69      */

70     public String JavaDoc getPayloadAsString(String JavaDoc encoding) throws Exception JavaDoc
71     {
72         return new String JavaDoc(getPayloadAsBytes(), encoding);
73     }
74
75     /**
76      * Converts the message implementation into a String representation
77      *
78      * @return String representation of the message
79      * @throws Exception Implemetation may throw an endpoint specific exception
80      */

81     public byte[] getPayloadAsBytes() throws Exception JavaDoc
82     {
83         return JmsMessageUtils.toByteArray(jmsMessage, jmsSpec);
84     }
85
86     /**
87      * @return the current message
88      */

89     public Object JavaDoc getPayload()
90     {
91         return jmsMessage;
92     }
93
94     /**
95      * @param message new value for the message
96      */

97     private void setMessage(Object JavaDoc message) throws MessagingException
98     {
99         if (message instanceof Message JavaDoc)
100         {
101             this.jmsMessage = (Message JavaDoc)message;
102         }
103         else
104         {
105             throw new MessageTypeNotSupportedException(message, getClass());
106         }
107
108         try
109         {
110             String JavaDoc value = this.jmsMessage.getJMSCorrelationID();
111             if (value != null)
112             {
113                 setProperty(JmsConstants.JMS_CORRELATION_ID, value);
114             }
115         }
116         catch (JMSException JavaDoc e)
117         {
118             // ignored
119
}
120
121         try
122         {
123             int value = this.jmsMessage.getJMSDeliveryMode();
124             setProperty(JmsConstants.JMS_DELIVERY_MODE, new Integer JavaDoc(value));
125         }
126         catch (JMSException JavaDoc e)
127         {
128             // ignored
129
}
130
131         try
132         {
133             Destination JavaDoc value = this.jmsMessage.getJMSDestination();
134             if (value != null)
135             {
136                 setProperty(JmsConstants.JMS_DESTINATION, value);
137             }
138         }
139         catch (JMSException JavaDoc e)
140         {
141             // ignored
142
}
143
144         try
145         {
146             long value = this.jmsMessage.getJMSExpiration();
147             setProperty(JmsConstants.JMS_EXPIRATION, new Long JavaDoc(value));
148         }
149         catch (JMSException JavaDoc e)
150         {
151             // ignored
152
}
153
154         try
155         {
156             String JavaDoc value = this.jmsMessage.getJMSMessageID();
157             if (value != null)
158             {
159                 setProperty(JmsConstants.JMS_MESSAGE_ID, value);
160             }
161         }
162         catch (JMSException JavaDoc e)
163         {
164             // ignored
165
}
166
167         try
168         {
169             int value = this.jmsMessage.getJMSPriority();
170             setProperty(JmsConstants.JMS_PRIORITY, new Integer JavaDoc(value));
171         }
172         catch (JMSException JavaDoc e)
173         {
174             // ignored
175
}
176
177         try
178         {
179             boolean value = this.jmsMessage.getJMSRedelivered();
180             setProperty(JmsConstants.JMS_REDELIVERED, Boolean.valueOf(value));
181         }
182         catch (JMSException JavaDoc e)
183         {
184             // ignored
185
}
186
187         try
188         {
189             Destination JavaDoc value = this.jmsMessage.getJMSReplyTo();
190             if (value != null)
191             {
192                 setProperty(JmsConstants.JMS_REPLY_TO, value);
193             }
194         }
195         catch (JMSException JavaDoc e)
196         {
197             // ignored
198
}
199
200         try
201         {
202             long value = this.jmsMessage.getJMSTimestamp();
203             setProperty(JmsConstants.JMS_TIMESTAMP, new Long JavaDoc(value));
204         }
205         catch (JMSException JavaDoc e)
206         {
207             // ignored
208
}
209
210         try
211         {
212             String JavaDoc value = this.jmsMessage.getJMSType();
213             if (value != null)
214             {
215                 setProperty(JmsConstants.JMS_TYPE, value);
216             }
217         }
218         catch (JMSException JavaDoc e)
219         {
220             // ignored
221
}
222
223         try
224         {
225             Enumeration JavaDoc e = this.jmsMessage.getPropertyNames();
226             while (e.hasMoreElements())
227             {
228                 String JavaDoc key = (String JavaDoc)e.nextElement();
229                 try
230                 {
231                     Object JavaDoc value = this.jmsMessage.getObjectProperty(key);
232                     if (value != null)
233                     {
234                         setProperty(key, value);
235                     }
236                 }
237                 catch (JMSException JavaDoc e1)
238                 {
239                     // ignored
240
}
241             }
242         }
243         catch (JMSException JavaDoc e1)
244         {
245             // ignored
246
}
247     }
248
249     public String JavaDoc getUniqueId()
250     {
251         return (String JavaDoc)getProperty(JmsConstants.JMS_MESSAGE_ID);
252     }
253
254     /**
255      * Sets a correlationId for this message. The correlation Id can be used by
256      * components in the system to manage message relations <p/> transport protocol.
257      * As such not all messages will support the notion of a correlationId i.e. tcp
258      * or file. In this situation the correlation Id is set as a property of the
259      * message where it's up to developer to keep the association with the message.
260      * For example if the message is serialised to xml the correlationId will be
261      * available in the message.
262      *
263      * @param id the Id reference for this relationship
264      */

265     public void setCorrelationId(String JavaDoc id)
266     {
267         setProperty(JmsConstants.JMS_CORRELATION_ID, id);
268     }
269
270     /**
271      * Sets a correlationId for this message. The correlation Id can be used by
272      * components in the system to manage message relations. <p/> The correlationId
273      * is associated with the message using the underlying transport protocol. As
274      * such not all messages will support the notion of a correlationId i.e. tcp or
275      * file. In this situation the correlation Id is set as a property of the message
276      * where it's up to developer to keep the association with the message. For
277      * example if the message is serialised to xml the correlationId will be
278      * available in the message.
279      *
280      * @return the correlationId for this message or null if one hasn't been set
281      */

282     public String JavaDoc getCorrelationId()
283     {
284         return (String JavaDoc)getProperty(JmsConstants.JMS_CORRELATION_ID);
285     }
286
287     /**
288      * Sets a replyTo address for this message. This is useful in an asynchronous
289      * environment where the caller doesn't wait for a response and the response
290      * needs to be routed somewhere for further processing. The value of this field
291      * can be any valid endpointUri url.
292      *
293      * @param replyTo the endpointUri url to reply to
294      */

295     public void setReplyTo(Object JavaDoc replyTo)
296     {
297         if (replyTo instanceof Destination JavaDoc)
298         {
299             setProperty(JmsConstants.JMS_REPLY_TO, replyTo);
300         }
301         else
302         {
303             super.setReplyTo(replyTo);
304         }
305     }
306
307     /**
308      * Sets a replyTo address for this message. This is useful in an asynchronous
309      * environment where the caller doesn't wait for a response and the response
310      * needs to be routed somewhere for further processing. The value of this field
311      * can be any valid endpointUri url.
312      *
313      * @return the endpointUri url to reply to or null if one has not been set
314      */

315     public Object JavaDoc getReplyTo()
316     {
317         Object JavaDoc replyTo = getProperty(JmsConstants.JMS_REPLY_TO);
318         if (replyTo == null)
319         {
320             replyTo = getProperty(MuleProperties.MULE_REPLY_TO_PROPERTY);
321         }
322         return replyTo;
323     }
324
325 }
326
Popular Tags