KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DefaultMessageAdapter.java 3982 2006-11-22 14:28:01Z lajos $
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 java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.mule.MuleRuntimeException;
17 import org.mule.config.i18n.Message;
18 import org.mule.config.i18n.Messages;
19 import org.mule.umo.provider.UMOMessageAdapter;
20
21 /**
22  * <code>DefaultMessageAdapter</code> can be used to wrap an arbitary object where
23  * no special 'apapting' is needed. The adapter allows for a set of properties to be
24  * associated with an object.
25  */

26
27 public class DefaultMessageAdapter extends AbstractMessageAdapter
28 {
29     /**
30      * Serial version
31      */

32     private static final long serialVersionUID = 1908152148142575505L;
33
34     /**
35      * The message object wrapped by this adapter
36      */

37     protected Object JavaDoc message;
38
39     /**
40      * Creates a default message adapter with properties and attachments
41      *
42      * @param message the message to wrap. If this is null and NullPayload object
43      * will be used
44      * @see NullPayload
45      */

46     public DefaultMessageAdapter(Object JavaDoc message)
47     {
48         if (message == null)
49         {
50             this.message = new NullPayload();
51         }
52         else
53         {
54             this.message = message;
55         }
56     }
57
58     public DefaultMessageAdapter(Object JavaDoc message, UMOMessageAdapter previous)
59     {
60         if (previous != null)
61         {
62             id = previous.getUniqueId();
63
64             if (message == null)
65             {
66                 this.message = new NullPayload();
67             }
68             else
69             {
70                 this.message = message;
71             }
72             for (Iterator JavaDoc iterator = previous.getAttachmentNames().iterator(); iterator.hasNext();)
73             {
74                 String JavaDoc name = (String JavaDoc)iterator.next();
75                 try
76                 {
77                     addAttachment(name, previous.getAttachment(name));
78                 }
79                 catch (Exception JavaDoc e)
80                 {
81                     throw new MuleRuntimeException(new Message(Messages.FAILED_TO_READ_PAYLOAD), e);
82                 }
83             }
84             for (Iterator JavaDoc iterator = previous.getPropertyNames().iterator(); iterator.hasNext();)
85             {
86                 String JavaDoc name = (String JavaDoc)iterator.next();
87                 try
88                 {
89                     setProperty(name, previous.getProperty(name));
90                 }
91                 catch (Exception JavaDoc e)
92                 {
93                     throw new MuleRuntimeException(new Message(Messages.FAILED_TO_READ_PAYLOAD), e);
94                 }
95             }
96         }
97         else
98         {
99             throw new NullPointerException JavaDoc("previousAdapter");
100         }
101     }
102
103     /**
104      * Creates a default message adapter with properties and attachments
105      *
106      * @param message the message to wrap. If this is null and NullPayload object
107      * will be used
108      * @param properties a map properties to set on the adapter. Can be null.
109      * @param attachments a map attaches (DataHandler objects) to set on the adapter.
110      * Can be null.
111      * @see NullPayload
112      * @see javax.activation.DataHandler
113      */

114     public DefaultMessageAdapter(Object JavaDoc message, Map JavaDoc properties, Map JavaDoc attachments)
115     {
116         this(message);
117         if (properties != null)
118         {
119             this.properties.putAll(properties);
120         }
121         if (attachments != null)
122         {
123             this.attachments.putAll(attachments);
124         }
125     }
126
127     /**
128      * Converts the message implementation into a String representation
129      *
130      * @param encoding The encoding to use when transforming the message (if
131      * necessary). The parameter is used when converting from a byte array
132      * @return String representation of the message payload
133      * @throws Exception Implementation may throw an endpoint specific exception
134      */

135     public String JavaDoc getPayloadAsString(String JavaDoc encoding) throws Exception JavaDoc
136     {
137         if (message instanceof byte[])
138         {
139             if (encoding != null)
140             {
141                 return new String JavaDoc((byte[])message, encoding);
142             }
143             else
144             {
145                 return new String JavaDoc((byte[])message);
146             }
147         }
148         else
149         {
150             return message.toString();
151         }
152     }
153
154     /**
155      * Converts the message implementation into a String representation
156      *
157      * @return String representation of the message
158      * @throws Exception Implemetation may throw an endpoint specific exception
159      */

160     public byte[] getPayloadAsBytes() throws Exception JavaDoc
161     {
162         return convertToBytes(message);
163     }
164
165     /**
166      * @return the current message
167      */

168     public Object JavaDoc getPayload()
169     {
170         return message;
171     }
172
173     public String JavaDoc getUniqueId()
174     {
175         return id;
176     }
177 }
178
Popular Tags