KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > AbstractMessageAdapter


1 /*
2  * $Id: AbstractMessageAdapter.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;
12
13 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
14 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentMap;
15
16 import java.io.Serializable JavaDoc;
17 import java.io.UnsupportedEncodingException JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import javax.activation.DataHandler JavaDoc;
23
24 import org.apache.commons.lang.SerializationUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.mule.MuleManager;
29 import org.mule.config.MuleProperties;
30 import org.mule.config.i18n.Message;
31 import org.mule.config.i18n.Messages;
32 import org.mule.umo.UMOExceptionPayload;
33 import org.mule.umo.provider.UMOMessageAdapter;
34 import org.mule.umo.transformer.TransformerException;
35 import org.mule.util.MapUtils;
36 import org.mule.util.UUID;
37
38 /**
39  * <code>AbstractMessageAdapter</code> provides a base implementation for simple
40  * message types that maybe don't normally allow for meta information, such as a File
41  * or TCP.
42  */

43 public abstract class AbstractMessageAdapter implements UMOMessageAdapter
44 {
45
46     /**
47      * logger used by this class
48      */

49     protected transient Log logger = LogFactory.getLog(getClass());
50
51     protected ConcurrentMap properties = new ConcurrentHashMap();
52     protected ConcurrentMap attachments = new ConcurrentHashMap();
53     protected String JavaDoc encoding = MuleManager.getConfiguration().getEncoding();
54
55     protected UMOExceptionPayload exceptionPayload;
56     protected String JavaDoc id = UUID.getUUID();
57
58     public String JavaDoc toString()
59     {
60         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(120);
61         buf.append(getClass().getName());
62         buf.append('{');
63         buf.append("id=").append(getUniqueId());
64         buf.append(", payload=").append(getPayload().getClass().getName());
65         buf.append(", correlationId=").append(getCorrelationId());
66         buf.append(", correlationGroup=").append(getCorrelationGroupSize());
67         buf.append(", correlationSeq=").append(getCorrelationSequence());
68         buf.append(", encoding=").append(getEncoding());
69         buf.append(", exceptionPayload=").append(exceptionPayload);
70         buf.append(", properties=").append(MapUtils.toString(properties, true));
71         buf.append('}');
72         return buf.toString();
73     }
74
75     public void addProperties(Map props)
76     {
77         if (props != null)
78         {
79             synchronized (props)
80             {
81                 for (Iterator JavaDoc iter = props.entrySet().iterator(); iter.hasNext();)
82                 {
83                     Map.Entry entry = (Map.Entry)iter.next();
84                     setProperty((String JavaDoc)entry.getKey(), entry.getValue());
85                 }
86             }
87         }
88     }
89
90     public void clearProperties()
91     {
92         properties.clear();
93     }
94
95     /**
96      * Removes an associated property from the message
97      *
98      * @param key the key of the property to remove
99      */

100     public Object JavaDoc removeProperty(String JavaDoc key)
101     {
102         return properties.remove(key);
103     }
104
105     /*
106      * (non-Javadoc)
107      *
108      * @see org.mule.providers.UMOMessageAdapter#getProperty(java.lang.Object)
109      */

110     public Object JavaDoc getProperty(String JavaDoc key)
111     {
112         return properties.get(key);
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see org.mule.providers.UMOMessageAdapter#getPropertyNames()
119      */

120     public Set JavaDoc getPropertyNames()
121     {
122         return properties.keySet();
123     }
124
125     /*
126      * (non-Javadoc)
127      *
128      * @see org.mule.providers.UMOMessageAdapter#setProperty(java.lang.Object,
129      * java.lang.Object)
130      */

131     public void setProperty(String JavaDoc key, Object JavaDoc value)
132     {
133         if (key != null)
134         {
135             if (value != null)
136             {
137                 properties.put(key, value);
138             }
139             else
140             {
141                 logger.warn("setProperty(key, value) called with null value; removing key: " + key
142                             + "; please report the following stack trace to dev@mule.codehaus.org.",
143                     new Throwable JavaDoc());
144                 properties.remove(key);
145             }
146         }
147         else
148         {
149             logger.warn("setProperty(key, value) ignored because of null key for object: " + value
150                         + "; please report the following stack trace to dev@mule.codehaus.org.",
151                 new Throwable JavaDoc());
152         }
153     }
154
155     public String JavaDoc getUniqueId()
156     {
157         return id;
158     }
159
160     public Object JavaDoc getProperty(String JavaDoc name, Object JavaDoc defaultValue)
161     {
162         return MapUtils.getObject(properties, name, defaultValue);
163     }
164
165     public int getIntProperty(String JavaDoc name, int defaultValue)
166     {
167         return MapUtils.getIntValue(properties, name, defaultValue);
168     }
169
170     public long getLongProperty(String JavaDoc name, long defaultValue)
171     {
172         return MapUtils.getLongValue(properties, name, defaultValue);
173     }
174
175     public double getDoubleProperty(String JavaDoc name, double defaultValue)
176     {
177         return MapUtils.getDoubleValue(properties, name, defaultValue);
178     }
179
180     public boolean getBooleanProperty(String JavaDoc name, boolean defaultValue)
181     {
182         return MapUtils.getBooleanValue(properties, name, defaultValue);
183     }
184
185     public String JavaDoc getStringProperty(String JavaDoc name, String JavaDoc defaultValue)
186     {
187         return MapUtils.getString(properties, name, defaultValue);
188     }
189
190     public void setBooleanProperty(String JavaDoc name, boolean value)
191     {
192         setProperty(name, Boolean.valueOf(value));
193     }
194
195     public void setIntProperty(String JavaDoc name, int value)
196     {
197         setProperty(name, new Integer JavaDoc(value));
198     }
199
200     public void setLongProperty(String JavaDoc name, long value)
201     {
202         setProperty(name, new Long JavaDoc(value));
203     }
204
205     public void setDoubleProperty(String JavaDoc name, double value)
206     {
207         setProperty(name, new Double JavaDoc(value));
208     }
209
210     public void setStringProperty(String JavaDoc name, String JavaDoc value)
211     {
212         setProperty(name, value);
213     }
214
215     public Object JavaDoc getReplyTo()
216     {
217         return getProperty(MuleProperties.MULE_REPLY_TO_PROPERTY);
218     }
219
220     public void setReplyTo(Object JavaDoc replyTo)
221     {
222         if (replyTo != null)
223         {
224             setProperty(MuleProperties.MULE_REPLY_TO_PROPERTY, replyTo);
225         }
226         else
227         {
228             removeProperty(MuleProperties.MULE_REPLY_TO_PROPERTY);
229         }
230     }
231
232     public String JavaDoc getCorrelationId()
233     {
234         return (String JavaDoc)getProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY);
235     }
236
237     public void setCorrelationId(String JavaDoc correlationId)
238     {
239         if (StringUtils.isNotBlank(correlationId))
240         {
241             setProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY, correlationId);
242         }
243         else
244         {
245             removeProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY);
246         }
247     }
248
249     /**
250      * Gets the sequence or ordering number for this message in the the correlation
251      * group (as defined by the correlationId)
252      *
253      * @return the sequence number or -1 if the sequence is not important
254      */

255     public int getCorrelationSequence()
256     {
257         return getIntProperty(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, -1);
258     }
259
260     /**
261      * Gets the sequence or ordering number for this message in the the correlation
262      * group (as defined by the correlationId)
263      *
264      * @param sequence the sequence number or -1 if the sequence is not important
265      */

266     public void setCorrelationSequence(int sequence)
267     {
268         setIntProperty(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, sequence);
269     }
270
271     /**
272      * Determines how many messages are in the correlation group
273      *
274      * @return total messages in this group or -1 if the size is not known
275      */

276     public int getCorrelationGroupSize()
277     {
278         return getIntProperty(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, -1);
279     }
280
281     /**
282      * Determines how many messages are in the correlation group
283      *
284      * @param size the total messages in this group or -1 if the size is not known
285      */

286     public void setCorrelationGroupSize(int size)
287     {
288         setIntProperty(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, size);
289     }
290
291     public UMOExceptionPayload getExceptionPayload()
292     {
293         return exceptionPayload;
294     }
295
296     public void setExceptionPayload(UMOExceptionPayload payload)
297     {
298         exceptionPayload = payload;
299     }
300
301     public void addAttachment(String JavaDoc name, DataHandler JavaDoc dataHandler) throws Exception JavaDoc
302     {
303         attachments.put(name, dataHandler);
304     }
305
306     public void removeAttachment(String JavaDoc name) throws Exception JavaDoc
307     {
308         attachments.remove(name);
309     }
310
311     public DataHandler JavaDoc getAttachment(String JavaDoc name)
312     {
313         return (DataHandler JavaDoc)attachments.get(name);
314     }
315
316     public Set JavaDoc getAttachmentNames()
317     {
318         return attachments.keySet();
319     }
320
321     public String JavaDoc getEncoding()
322     {
323         return encoding;
324     }
325
326     /**
327      * Sets the encoding for this message
328      *
329      * @param encoding the encoding to use
330      */

331     public void setEncoding(String JavaDoc encoding)
332     {
333         this.encoding = encoding;
334     }
335
336     /**
337      * Converts the message implementation into a String representation. If encoding
338      * is required it will use the encoding set on the message
339      *
340      * @return String representation of the message payload
341      * @throws Exception Implementation may throw an endpoint specific exception
342      */

343     public final String JavaDoc getPayloadAsString() throws Exception JavaDoc
344     {
345         return getPayloadAsString(getEncoding());
346     }
347
348     protected byte[] convertToBytes(Object JavaDoc object) throws TransformerException, UnsupportedEncodingException JavaDoc
349     {
350         if (object instanceof String JavaDoc)
351         {
352             return object.toString().getBytes(getEncoding());
353         }
354
355         if (object instanceof byte[])
356         {
357             return (byte[])object;
358         }
359         else if (object instanceof Serializable JavaDoc)
360         {
361             try
362             {
363                 return SerializationUtils.serialize((Serializable JavaDoc)object);
364             }
365             catch (Exception JavaDoc e)
366             {
367                 throw new TransformerException(new Message(Messages.TRANSFORM_FAILED_FROM_X_TO_X,
368                     object.getClass().getName(), "byte[]"), e);
369             }
370         }
371         else
372         {
373             throw new TransformerException(new Message(Messages.TRANSFORM_ON_X_NOT_OF_SPECIFIED_TYPE_X,
374                 object.getClass().getName(), "byte[] or " + Serializable JavaDoc.class.getName()));
375         }
376     }
377 }
378
Popular Tags