KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > MuleMessage


1 /*
2  * $Id: MuleMessage.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.impl;
12
13 import org.mule.MuleRuntimeException;
14 import org.mule.config.i18n.Message;
15 import org.mule.config.i18n.Messages;
16 import org.mule.providers.DefaultMessageAdapter;
17 import org.mule.umo.UMOExceptionPayload;
18 import org.mule.umo.UMOMessage;
19 import org.mule.umo.provider.UMOMessageAdapter;
20
21 import javax.activation.DataHandler JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * <code>MuleMessage</code> is a wrapper that contains a payload and properties
28  * associated with the payload.
29  *
30  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
31  * @version $Revision: 3798 $
32  */

33
34 public class MuleMessage implements UMOMessage
35 {
36     /**
37      * Serial version
38      */

39     private static final long serialVersionUID = 1541720810851984842L;
40
41     private UMOMessageAdapter adapter;
42
43     protected UMOExceptionPayload exceptionPayload;
44
45     public MuleMessage(Object JavaDoc message)
46     {
47         this(message, (Map JavaDoc)null);
48     }
49
50     public MuleMessage(Object JavaDoc message, Map JavaDoc properties)
51     {
52         if (message instanceof UMOMessageAdapter)
53         {
54             adapter = (UMOMessageAdapter)message;
55         }
56         else
57         {
58             adapter = new DefaultMessageAdapter(message);
59         }
60         addProperties(properties);
61     }
62
63     public MuleMessage(Object JavaDoc message, UMOMessageAdapter previous)
64     {
65         if (message instanceof UMOMessageAdapter)
66         {
67             adapter = (UMOMessageAdapter)message;
68         }
69         else
70         {
71             adapter = new DefaultMessageAdapter(message, previous);
72         }
73         if (previous.getExceptionPayload() != null)
74         {
75             setExceptionPayload(previous.getExceptionPayload());
76         }
77         setEncoding(previous.getEncoding());
78         if (previous.getAttachmentNames().size() > 0)
79         {
80             Set JavaDoc attNames = adapter.getAttachmentNames();
81             synchronized (attNames)
82             {
83                 for (Iterator JavaDoc iterator = attNames.iterator(); iterator.hasNext();)
84                 {
85                     String JavaDoc s = (String JavaDoc)iterator.next();
86                     try
87                     {
88                         addAttachment(s, adapter.getAttachment(s));
89                     }
90                     catch (Exception JavaDoc e)
91                     {
92                         throw new MuleRuntimeException(new Message(Messages.FAILED_TO_READ_ATTACHMENT_X, s),
93                             e);
94                     }
95                 }
96             }
97         }
98     }
99
100     public UMOMessageAdapter getAdapter()
101     {
102         return adapter;
103     }
104
105     /**
106      * Gets a property of the payload implementation
107      *
108      * @param key the key on which to lookup the property value
109      * @return the property value or null if the property does not exist
110      */

111     public Object JavaDoc getProperty(String JavaDoc key)
112     {
113         return adapter.getProperty(key);
114     }
115
116     public Object JavaDoc removeProperty(String JavaDoc key)
117     {
118         return adapter.removeProperty(key);
119     }
120
121     /**
122      * Gets a property of the payload implementation
123      *
124      * @param key the key on which to associate the value
125      * @param value the property value
126      */

127     public void setProperty(String JavaDoc key, Object JavaDoc value)
128     {
129         adapter.setProperty(key, value);
130     }
131
132     /**
133      * Converts the payload implementation into a String representation
134      *
135      * @return String representation of the payload
136      * @throws Exception Implemetation may throw an endpoint specific exception
137      */

138     public String JavaDoc getPayloadAsString() throws Exception JavaDoc
139     {
140         return adapter.getPayloadAsString();
141     }
142
143     /**
144      * Converts the message implementation into a String representation
145      *
146      * @param encoding The encoding to use when transforming the message (if
147      * necessary). The parameter is used when converting from a byte array
148      * @return String representation of the message payload
149      * @throws Exception Implementation may throw an endpoint specific exception
150      */

151     public String JavaDoc getPayloadAsString(String JavaDoc encoding) throws Exception JavaDoc
152     {
153         if (encoding == null)
154         {
155             return adapter.getPayloadAsString();
156         }
157         else
158         {
159             return adapter.getPayloadAsString(encoding);
160         }
161     }
162
163     /**
164      * @return all properties on this payload
165      */

166     public Set JavaDoc getPropertyNames()
167     {
168         return adapter.getPropertyNames();
169     }
170
171     /**
172      * Converts the payload implementation into a String representation
173      *
174      * @return String representation of the payload
175      * @throws Exception Implemetation may throw an endpoint specific exception
176      */

177     public byte[] getPayloadAsBytes() throws Exception JavaDoc
178     {
179         return adapter.getPayloadAsBytes();
180     }
181
182     /**
183      * @return the current payload
184      */

185     public Object JavaDoc getPayload()
186     {
187         return adapter.getPayload();
188     }
189
190     public void addProperties(Map JavaDoc properties)
191     {
192         adapter.addProperties(properties);
193     }
194
195     public void clearProperties()
196     {
197         adapter.clearProperties();
198     }
199
200     /**
201      * Gets a double property from the event
202      *
203      * @param name the name or key of the property
204      * @param defaultValue a default value if the property doesn't exist in the event
205      * @return the property value or the defaultValue if the property does not exist
206      */

207     public double getDoubleProperty(String JavaDoc name, double defaultValue)
208     {
209         return adapter.getDoubleProperty(name, defaultValue);
210     }
211
212     /**
213      * Sets a double property on the event
214      *
215      * @param name the property name or key
216      * @param value the property value
217      */

218     public void setDoubleProperty(String JavaDoc name, double value)
219     {
220         adapter.setDoubleProperty(name, value);
221     }
222
223     public String JavaDoc getUniqueId()
224     {
225         return adapter.getUniqueId();
226     }
227
228     public Object JavaDoc getProperty(String JavaDoc name, Object JavaDoc defaultValue)
229     {
230         return adapter.getProperty(name, defaultValue);
231     }
232
233     public int getIntProperty(String JavaDoc name, int defaultValue)
234     {
235         return adapter.getIntProperty(name, defaultValue);
236     }
237
238     public long getLongProperty(String JavaDoc name, long defaultValue)
239     {
240         return adapter.getLongProperty(name, defaultValue);
241     }
242
243     public boolean getBooleanProperty(String JavaDoc name, boolean defaultValue)
244     {
245         return adapter.getBooleanProperty(name, defaultValue);
246     }
247
248     public void setBooleanProperty(String JavaDoc name, boolean value)
249     {
250         adapter.setBooleanProperty(name, value);
251     }
252
253     public void setIntProperty(String JavaDoc name, int value)
254     {
255         adapter.setIntProperty(name, value);
256     }
257
258     public void setLongProperty(String JavaDoc name, long value)
259     {
260         adapter.setLongProperty(name, value);
261     }
262
263     /**
264      * Sets a correlationId for this message. The correlation Id can be used by
265      * components in the system to manage message relations <p/> transport protocol.
266      * As such not all messages will support the notion of a correlationId i.e. tcp
267      * or file. In this situation the correlation Id is set as a property of the
268      * message where it's up to developer to keep the association with the message.
269      * For example if the message is serialised to xml the correlationId will be
270      * available in the message.
271      *
272      * @param id the Id reference for this relationship
273      */

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

291     public String JavaDoc getCorrelationId()
292     {
293         return adapter.getCorrelationId();
294     }
295
296     /**
297      * Sets a replyTo address for this message. This is useful in an asynchronous
298      * environment where the caller doesn't wait for a response and the response
299      * needs to be routed somewhere for further processing. The value of this field
300      * can be any valid endpointUri url.
301      *
302      * @param replyTo the endpointUri url to reply to
303      */

304     public void setReplyTo(Object JavaDoc replyTo)
305     {
306         adapter.setReplyTo(replyTo);
307     }
308
309     /**
310      * Sets a replyTo address for this message. This is useful in an asynchronous
311      * environment where the caller doesn't wait for a response and the response
312      * needs to be routed somewhere for further processing. The value of this field
313      * can be any valid endpointUri url.
314      *
315      * @return the endpointUri url to reply to or null if one has not been set
316      */

317     public Object JavaDoc getReplyTo()
318     {
319         return adapter.getReplyTo();
320     }
321
322     /**
323      * Gets the sequence or ordering number for this message in the the correlation
324      * group (as defined by the correlationId)
325      *
326      * @return the sequence number or -1 if the sequence is not important
327      */

328     public int getCorrelationSequence()
329     {
330         return adapter.getCorrelationSequence();
331     }
332
333     /**
334      * Gets the sequence or ordering number for this message in the the correlation
335      * group (as defined by the correlationId)
336      *
337      * @param sequence the sequence number or -1 if the sequence is not important
338      */

339     public void setCorrelationSequence(int sequence)
340     {
341         adapter.setCorrelationSequence(sequence);
342     }
343
344     /**
345      * Determines how many messages are in the correlation group
346      *
347      * @return total messages in this group or -1 if the size is not known
348      */

349     public int getCorrelationGroupSize()
350     {
351         return adapter.getCorrelationGroupSize();
352     }
353
354     /**
355      * Determines how many messages are in the correlation group
356      *
357      * @param size the total messages in this group or -1 if the size is not known
358      */

359     public void setCorrelationGroupSize(int size)
360     {
361         adapter.setCorrelationGroupSize(size);
362     }
363
364     public UMOExceptionPayload getExceptionPayload()
365     {
366         return exceptionPayload;
367     }
368
369     public void setExceptionPayload(UMOExceptionPayload exceptionPayload)
370     {
371         this.exceptionPayload = exceptionPayload;
372     }
373
374     public String JavaDoc toString()
375     {
376         return adapter.toString();
377     }
378
379     public void addAttachment(String JavaDoc name, DataHandler JavaDoc dataHandler) throws Exception JavaDoc
380     {
381         adapter.addAttachment(name, dataHandler);
382     }
383
384     public void removeAttachment(String JavaDoc name) throws Exception JavaDoc
385     {
386         adapter.removeAttachment(name);
387     }
388
389     public DataHandler JavaDoc getAttachment(String JavaDoc name)
390     {
391         return adapter.getAttachment(name);
392     }
393
394     public Set JavaDoc getAttachmentNames()
395     {
396         return adapter.getAttachmentNames();
397     }
398
399     /**
400      * Gets the encoding for the current message. For potocols that send encoding
401      * Information with the message, this method should be overriden to expose the
402      * transport encoding, otherwise the default encoding in the Mule configuration
403      * will be used
404      *
405      * @return the encoding for this message. This method must never return null
406      */

407     public String JavaDoc getEncoding()
408     {
409         return adapter.getEncoding();
410     }
411
412     /**
413      * Sets the encoding for this message
414      *
415      * @param encoding the encoding to use
416      */

417     public void setEncoding(String JavaDoc encoding)
418     {
419         adapter.setEncoding(encoding);
420     }
421
422     /**
423      * Gets a String property from the event
424      *
425      * @param name the name or key of the property
426      * @param defaultValue a default value if the property doesn't exist in the event
427      * @return the property value or the defaultValue if the property does not exist
428      */

429     public String JavaDoc getStringProperty(String JavaDoc name, String JavaDoc defaultValue)
430     {
431         return adapter.getStringProperty(name, defaultValue);
432     }
433
434     /**
435      * Sets a String property on the event
436      *
437      * @param name the property name or key
438      * @param value the property value
439      */

440     public void setStringProperty(String JavaDoc name, String JavaDoc value)
441     {
442         adapter.setStringProperty(name, value);
443     }
444 }
445
Popular Tags