View Javadoc

1   /*
2    * $Id: BaseMessage.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.impl.message;
12  
13  import java.io.Serializable;
14  import java.util.HashMap;
15  import java.util.Map;
16  
17  /**
18   * <code>BaseMessage</code> A default message implementation used for messages sent
19   * over the wire. client messages should NOT implement UMOMessage.
20   */
21  public class BaseMessage implements Serializable
22  {
23      /**
24       * Serial version
25       */
26      private static final long serialVersionUID = -6105691921086093748L;
27  
28      protected Object message;
29  
30      protected Map context;
31  
32      public BaseMessage(Object message)
33      {
34          this.message = message;
35          context = new HashMap();
36      }
37  
38      /**
39       * Converts the message implementation into a String representation
40       * 
41       * @return String representation of the message payload
42       * @throws Exception Implementation may throw an endpoint specific exception
43       */
44      public String getPayloadAsString(String encoding) throws Exception
45      {
46          return message.toString();
47      }
48  
49      /**
50       * Converts the message implementation into a String representation
51       * 
52       * @return String representation of the message
53       * @throws Exception Implemetation may throw an endpoint specific exception
54       */
55      public byte[] getPayloadAsBytes() throws Exception
56      {
57          return getPayloadAsString(message.toString()).getBytes();
58      }
59  
60      /**
61       * @return the current message
62       */
63      public Object getPayload()
64      {
65          return message;
66      }
67  
68      /**
69       * Adds a map of properties to associated with this message
70       * 
71       * @param properties the properties add to this message
72       */
73      public void addProperties(Map properties)
74      {
75          context.putAll(properties);
76      }
77  
78      /**
79       * Removes all properties on this message
80       */
81      public void clearProperties()
82      {
83          context.clear();
84      }
85  
86      /**
87       * Returns a map of all properties on this message
88       * 
89       * @return a map of all properties on this message
90       */
91      public Map getProperties()
92      {
93          return context;
94      }
95  
96      public void setProperty(Object key, Object value)
97      {
98          context.put(key, value);
99      }
100 
101     public Object getProperty(Object key)
102     {
103         return context.get(key);
104     }
105 
106     public String toString()
107     {
108         return "BaseMessage{" + "message=" + message + ", context=" + context + "}";
109     }
110 }