View Javadoc

1   /*
2    * $Id: DefaultMessageAdapter.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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 CPAL v1.0
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 org.mule.impl.ThreadSafeAccess;
14  import org.mule.umo.provider.UMOMessageAdapter;
15  
16  import java.util.Map;
17  
18  /**
19   * <code>DefaultMessageAdapter</code> can be used to wrap an arbitary object where
20   * no special 'apapting' is needed. The adapter allows for a set of properties to be
21   * associated with an object.
22   */
23  
24  public class DefaultMessageAdapter extends AbstractMessageAdapter
25  {
26      /**
27       * Serial version
28       */
29      private static final long serialVersionUID = 1908152148142575505L;
30  
31      /**
32       * The message object wrapped by this adapter
33       */
34      protected Object message;
35  
36      /**
37       * Creates a default message adapter with properties and attachments
38       * 
39       * @param message the message to wrap. If this is null and NullPayload object
40       *            will be used
41       * @see NullPayload
42       */
43      public DefaultMessageAdapter(Object message)
44      {
45          if (message == null)
46          {
47              this.message = NullPayload.getInstance();
48          }
49          else
50          {
51              this.message = message;
52          }
53      }
54  
55      public DefaultMessageAdapter(Object message, UMOMessageAdapter previous)
56      {
57          super(previous);
58          if (previous != null)
59          {
60              if (message == null)
61              {
62                  this.message = NullPayload.getInstance();
63              }
64              else
65              {
66                  this.message = message;
67              }
68          }
69          else
70          {
71              throw new IllegalArgumentException("previousAdapter may not be null");
72          }
73      }
74  
75      /**
76       * Creates a default message adapter with properties and attachments
77       * 
78       * @param message the message to wrap. If this is null and NullPayload object
79       *            will be used
80       * @param properties a map properties to set on the adapter. Can be null.
81       * @param attachments a map attaches (DataHandler objects) to set on the adapter.
82       *            Can be null.
83       * @see NullPayload
84       * @see javax.activation.DataHandler
85       */
86      public DefaultMessageAdapter(Object message, Map properties, Map attachments)
87      {
88          this(message);
89          if (properties != null)
90          {
91              this.properties.putAll(properties);
92          }
93          if (attachments != null)
94          {
95              this.attachments.putAll(attachments);
96          }
97      }
98  
99      /**
100      * Converts the message implementation into a String representation
101      * 
102      * @param encoding The encoding to use when transforming the message (if
103      *            necessary). The parameter is used when converting from a byte array
104      * @return String representation of the message payload
105      * @throws Exception Implementation may throw an endpoint specific exception
106      */
107     public String getPayloadAsString(String encoding) throws Exception
108     {
109         if (message instanceof byte[])
110         {
111             if (encoding != null)
112             {
113                 return new String((byte[]) message, encoding);
114             }
115             else
116             {
117                 return new String((byte[]) message);
118             }
119         }
120         else
121         {
122             return message.toString();
123         }
124     }
125 
126     /**
127      * Converts the message implementation into a String representation
128      * 
129      * @return String representation of the message
130      * @throws Exception Implemetation may throw an endpoint specific exception
131      */
132     public byte[] getPayloadAsBytes() throws Exception
133     {
134         return convertToBytes(message);
135     }
136 
137     /**
138      * @return the current message
139      */
140     public Object getPayload()
141     {
142         return message;
143     }
144 
145     public String getUniqueId()
146     {
147         return id;
148     }
149 
150     public ThreadSafeAccess newThreadCopy()
151     {
152         return new DefaultMessageAdapter(getPayload(), this);
153     }
154 
155 }