001: /*
002: * $Id: BaseMessage.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.message;
012:
013: import java.io.Serializable;
014: import java.util.HashMap;
015: import java.util.Map;
016:
017: /**
018: * <code>BaseMessage</code> A default message implementation used for messages sent
019: * over the wire. client messages should NOT implement MuleMessage.
020: */
021: public class BaseMessage implements Serializable {
022: /**
023: * Serial version
024: */
025: private static final long serialVersionUID = -6105691921086093748L;
026:
027: protected Object message;
028:
029: protected Map context;
030:
031: public BaseMessage(Object message) {
032: this .message = message;
033: context = new HashMap();
034: }
035:
036: /**
037: * Converts the message implementation into a String representation
038: *
039: * @return String representation of the message payload
040: * @throws Exception Implementation may throw an endpoint specific exception
041: */
042: public String getPayloadAsString(String encoding) throws Exception {
043: return message.toString();
044: }
045:
046: /**
047: * Converts the message implementation into a String representation
048: *
049: * @return String representation of the message
050: * @throws Exception Implemetation may throw an endpoint specific exception
051: */
052: public byte[] getPayloadAsBytes() throws Exception {
053: return getPayloadAsString(message.toString()).getBytes();
054: }
055:
056: /**
057: * @return the current message
058: */
059: public Object getPayload() {
060: return message;
061: }
062:
063: /**
064: * Adds a map of properties to associated with this message
065: *
066: * @param properties the properties add to this message
067: */
068: public void addProperties(Map properties) {
069: context.putAll(properties);
070: }
071:
072: /**
073: * Removes all properties on this message
074: */
075: public void clearProperties() {
076: context.clear();
077: }
078:
079: /**
080: * Returns a map of all properties on this message
081: *
082: * @return a map of all properties on this message
083: */
084: public Map getProperties() {
085: return context;
086: }
087:
088: public void setProperty(Object key, Object value) {
089: context.put(key, value);
090: }
091:
092: public Object getProperty(Object key) {
093: return context.get(key);
094: }
095:
096: public String toString() {
097: return "BaseMessage{" + "message=" + message + ", context="
098: + context + "}";
099: }
100: }
|