org.mule
Class DefaultMuleMessage

java.lang.Object
  extended by org.mule.DefaultMuleMessage
All Implemented Interfaces:
Serializable, MuleMessage, ThreadSafeAccess, MessageAdapter
Direct Known Subclasses:
DefaultMessageCollection

public class DefaultMuleMessage
extends Object
implements MuleMessage, ThreadSafeAccess

DefaultMuleMessage is a wrapper that contains a payload and properties associated with the payload.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.mule.api.ThreadSafeAccess
ThreadSafeAccess.AccessControl
 
Field Summary
 
Fields inherited from interface org.mule.api.ThreadSafeAccess
READ, WRITE
 
Constructor Summary
DefaultMuleMessage(Object message)
           
DefaultMuleMessage(Object message, Map properties)
           
DefaultMuleMessage(Object message, MessageAdapter previous)
           
 
Method Summary
 void addAttachment(String name, DataHandler dataHandler)
          Allows for arbitary data attachments to be associated with the Message.
 void addProperties(Map properties)
          Adds a map of properties to be associated with this message
 void addProperties(Map properties, PropertyScope scope)
          Adds a map of properties to be associated with this message
protected  void applyAllTransformers(List transformers)
           
 void applyTransformers(List transformers)
          Will apply a list of transformers to the payload of the message.
 void applyTransformers(List transformers, Class outputType)
          Will apply a list of transformers to the payload of the message.
 void assertAccess(boolean write)
          This method may be called before data in the object are accessed.
 void clearProperties()
          Removes all properties on this message
 MessageAdapter getAdapter()
          Returns the currently edited Message adapter for this message.
 DataHandler getAttachment(String name)
          Retrieve an attachment with the given name.
 Set getAttachmentNames()
          Returns a set of the names of the attachments on this message.
 boolean getBooleanProperty(String name, boolean defaultValue)
          Gets a boolean property from the message
 int getCorrelationGroupSize()
          Determines how many messages are in the correlation group
 String getCorrelationId()
          Sets a correlationId for this message.
 int getCorrelationSequence()
          Gets the sequence or ordering number for this message in the the correlation group (as defined by the correlationId)
 double getDoubleProperty(String name, double defaultValue)
          Gets a double property from the message
 String getEncoding()
          Gets the encoding for the current message.
 ExceptionPayload getExceptionPayload()
          If an error occurred during the processing of this message this will return a ErrorPayload that contains the root exception and Mule error code, plus any other releated info
 int getIntProperty(String name, int defaultValue)
          Gets an integer property from the message
 long getLongProperty(String name, long defaultValue)
          Gets a long property from the message
 Object getOrginalPayload()
          Returns the original payload used to create this message.
 MessageAdapter getOriginalAdapter()
          Returns the original payload used to create this message.
 Object getPayload()
          
 Object getPayload(Class outputType)
          Will attempt to obtain the payload of this message with the desired Class type.
protected  Object getPayload(Class outputType, String encoding)
          Will attempt to obtain the payload of this message with the desired Class type.
 byte[] getPayloadAsBytes()
          Converts the message implementation into a byte array representation
 String getPayloadAsString()
          Converts the message implementation into a String representation.
 String getPayloadAsString(String encoding)
          Converts the message implementation into a String representation
 Object getProperty(String key)
          Gets a property of the message implementation
 Object getProperty(String name, Object defaultValue)
          Gets a property from the message
 Object getProperty(String name, PropertyScope scope)
          Gets a property from the message with a given scope
 Set getPropertyNames()
          
 Set getPropertyNames(PropertyScope scope)
          Gets all property names in a given scope
 Object getReplyTo()
          Returns a replyTo address for this message.
 String getStringProperty(String name, String defaultValue)
          Gets a String property from the message
 String getUniqueId()
          gets the unique identifier for the message.
 boolean isConsumable()
          Determines if the payload of this message is consumable i.e.
protected  boolean isPayloadConsumed(Class inputCls)
          Checks if the payload has been consumed for this message.
 ThreadSafeAccess newThreadCopy()
          
 void release()
          Perform any clean up operations on the message resource.
 void removeAttachment(String name)
          Remove an attahcment form this message with the specifed name
 Object removeProperty(String key)
          Removes a property on this message
 void resetAccessControl()
          This method should ONLY be used in the construction of composite ThreadSafeAccess instances.
 void setBooleanProperty(String name, boolean value)
          Sets a boolean property on the message
 void setCorrelationGroupSize(int size)
          Determines how many messages are in the correlation group
 void setCorrelationId(String id)
          Sets a correlationId for this message.
 void setCorrelationSequence(int sequence)
          Gets the sequence or ordering number for this message in the the correlation group (as defined by the correlationId)
 void setDoubleProperty(String name, double value)
          Sets a double property on the message
 void setEncoding(String encoding)
          Sets the encoding for this message
 void setExceptionPayload(ExceptionPayload exceptionPayload)
          If an error occurs while processing this message, a ErrorPayload is attached which contains the root exception and Mule error code, plus any other releated info.
 void setIntProperty(String name, int value)
          Sets a integerproperty on the message
 void setLongProperty(String name, long value)
          Sets a long property on the message
 void setPayload(Object payload)
          Update the message payload.
 void setProperty(String key, Object value)
          Set a property on the message
 void setProperty(String key, Object value, PropertyScope scope)
          Set a property on the message
 void setReplyTo(Object replyTo)
          Sets a replyTo address for this message.
 void setStringProperty(String name, String value)
          Sets a String property on the message
 String toString()
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DefaultMuleMessage

public DefaultMuleMessage(Object message)

DefaultMuleMessage

public DefaultMuleMessage(Object message,
                          Map properties)

DefaultMuleMessage

public DefaultMuleMessage(Object message,
                          MessageAdapter previous)
Method Detail

getPayload

public Object getPayload(Class outputType)
                  throws TransformerException
Will attempt to obtain the payload of this message with the desired Class type. This will try and resolve a transformer that can do this transformation. If a transformer cannot be found an exception is thrown. Any transfromers added to the reqgistry will be checked for compatability

Specified by:
getPayload in interface MuleMessage
Parameters:
outputType - the desired return type
Returns:
The converted payload of this message. Note that this method will not alter the payload of this message *unless* the payload is an inputstream in which case the stream will be read and the payload will become the fully read stream.
Throws:
TransformerException - if a transformer cannot be found or there is an error during transformation of the payload

getPayload

protected Object getPayload(Class outputType,
                            String encoding)
                     throws TransformerException
Will attempt to obtain the payload of this message with the desired Class type. This will try and resolve a trnsformr that can do this transformation. If a transformer cannot be found an exception is thrown. Any transfromers added to the reqgistry will be checked for compatability

Parameters:
outputType - the desired return type
encoding - the encoding to use if required
Returns:
The converted payload of this message. Note that this method will not alter the payload of this message *unless* the payload is an inputstream in which case the stream will be read and the payload will become the fully read stream.
Throws:
TransformerException - if a transformer cannot be found or there is an error during transformation of the payload

isPayloadConsumed

protected boolean isPayloadConsumed(Class inputCls)
Checks if the payload has been consumed for this message. This only applies to Streaming payload types since once the stream has been read, the payload of the message should be updated to represent the data read from the stream

Parameters:
inputCls - the input type of the message payload
Returns:
true if the payload message type was stream-based, false otherwise

getAdapter

public MessageAdapter getAdapter()
Returns the currently edited Message adapter for this message. If no edits have been made this methd will return the same as MuleMessage.getOriginalAdapter()

Specified by:
getAdapter in interface MuleMessage
Returns:

getOrginalPayload

public Object getOrginalPayload()
Returns the original payload used to create this message. The payload of the message can change if MuleMessage.applyTransformers(java.util.List) or MuleMessage.applyTransformers(java.util.List, Class) is called.

Specified by:
getOrginalPayload in interface MuleMessage
Returns:
the original payload used to create this message

getOriginalAdapter

public MessageAdapter getOriginalAdapter()
Returns the original payload used to create this message. The payload of the message can change if MuleMessage.applyTransformers(java.util.List) or MuleMessage.applyTransformers(java.util.List, Class) is called.

Specified by:
getOriginalAdapter in interface MuleMessage
Returns:
the original payload used to create this message

setProperty

public void setProperty(String key,
                        Object value,
                        PropertyScope scope)
Set a property on the message

Specified by:
setProperty in interface MessageAdapter
Parameters:
key - the key on which to associate the value
value - the property value
scope - The scope at which to set the property at
See Also:
PropertyScope

getProperty

public Object getProperty(String key)
Gets a property of the message implementation

Specified by:
getProperty in interface MessageAdapter
Parameters:
key - the key on which to lookup the property value
Returns:
the property value or null if the property does not exist

removeProperty

public Object removeProperty(String key)
Removes a property on this message

Specified by:
removeProperty in interface MessageAdapter
Parameters:
key - the property key to remove
Returns:
the removed property value or null if the property did not exist

setProperty

public void setProperty(String key,
                        Object value)
Set a property on the message

Specified by:
setProperty in interface MessageAdapter
Parameters:
key - the key on which to associate the value
value - the property value

getPayloadAsString

public final String getPayloadAsString()
                                throws Exception
Converts the message implementation into a String representation. If encoding is required it will use the encoding set on the message

Specified by:
getPayloadAsString in interface MuleMessage
Returns:
String representation of the message payload
Throws:
Exception - Implementation may throw an endpoint specific exception

getPayloadAsBytes

public byte[] getPayloadAsBytes()
                         throws Exception
Converts the message implementation into a byte array representation

Specified by:
getPayloadAsBytes in interface MuleMessage
Returns:
byte array of the message
Throws:
Exception - Implemetation may throw an endpoint specific exception

getPayloadAsString

public String getPayloadAsString(String encoding)
                          throws Exception
Converts the message implementation into a String representation

Specified by:
getPayloadAsString in interface MuleMessage
Parameters:
encoding - The encoding to use when transforming the message (if necessary). The parameter is used when converting from a byte array
Returns:
String representation of the message payload
Throws:
Exception - Implementation may throw an endpoint specific exception

getPropertyNames

public Set getPropertyNames()

Specified by:
getPropertyNames in interface MessageAdapter
Returns:
all property keys on this message

getPropertyNames

public Set getPropertyNames(PropertyScope scope)
Gets all property names in a given scope

Specified by:
getPropertyNames in interface MessageAdapter
Parameters:
scope - the scope of property names
Returns:
all property keys on this message

getDoubleProperty

public double getDoubleProperty(String name,
                                double defaultValue)
Description copied from interface: MessageAdapter
Gets a double property from the message

Specified by:
getDoubleProperty in interface MessageAdapter
Parameters:
name - the name or key of the property
defaultValue - a default value if the property doesn't exist in the event
Returns:
the property value or the defaultValue if the property does not exist

setDoubleProperty

public void setDoubleProperty(String name,
                              double value)
Sets a double property on the message

Specified by:
setDoubleProperty in interface MessageAdapter
Parameters:
name - the property name or key
value - the property value

getUniqueId

public String getUniqueId()
gets the unique identifier for the message. It's up to the implementation to ensure a unique id

Specified by:
getUniqueId in interface MessageAdapter
Returns:
a unique message id. The Id should never be null. If the underlying transport does not have the notion of a message Id, one shuold be generated. The generated Id should be a UUID.

getProperty

public Object getProperty(String name,
                          Object defaultValue)
Gets a property from the message

Specified by:
getProperty in interface MessageAdapter
Parameters:
name - the name or key of the property
defaultValue - a default value if the property doesn't exist in the event
Returns:
the property value or the defaultValue if the property does not exist

getProperty

public Object getProperty(String name,
                          PropertyScope scope)
Gets a property from the message with a given scope

Specified by:
getProperty in interface MessageAdapter
Parameters:
name - the name or key of the property
scope - The scope of the property to retrieve
Returns:
the property value or the defaultValue if the property does not exist in the specificed scope

getIntProperty

public int getIntProperty(String name,
                          int defaultValue)
Gets an integer property from the message

Specified by:
getIntProperty in interface MessageAdapter
Parameters:
name - the name or key of the property
defaultValue - a default value if the property doesn't exist in the event
Returns:
the property value or the defaultValue if the property does not exist

getLongProperty

public long getLongProperty(String name,
                            long defaultValue)
Gets a long property from the message

Specified by:
getLongProperty in interface MessageAdapter
Parameters:
name - the name or key of the property
defaultValue - a default value if the property doesn't exist in the event
Returns:
the property value or the defaultValue if the property does not exist

getBooleanProperty

public boolean getBooleanProperty(String name,
                                  boolean defaultValue)
Gets a boolean property from the message

Specified by:
getBooleanProperty in interface MessageAdapter
Parameters:
name - the name or key of the property
defaultValue - a default value if the property doesn't exist in the event
Returns:
the property value or the defaultValue if the property does not exist

setBooleanProperty

public void setBooleanProperty(String name,
                               boolean value)
Sets a boolean property on the message

Specified by:
setBooleanProperty in interface MessageAdapter
Parameters:
name - the property name or key
value - the property value

setIntProperty

public void setIntProperty(String name,
                           int value)
Sets a integerproperty on the message

Specified by:
setIntProperty in interface MessageAdapter
Parameters:
name - the property name or key
value - the property value

setLongProperty

public void setLongProperty(String name,
                            long value)
Sets a long property on the message

Specified by:
setLongProperty in interface MessageAdapter
Parameters:
name - the property name or key
value - the property value

setCorrelationId

public void setCorrelationId(String id)
Sets a correlationId for this message. The correlation Id can be used by components in the system to manage message relations

transport protocol. As such not all messages will support the notion of a correlationId i.e. tcp or file. In this situation the correlation Id is set as a property of the message where it's up to developer to keep the association with the message. For example if the message is serialised to xml the correlationId will be available in the message.

Specified by:
setCorrelationId in interface MessageAdapter
Parameters:
id - the Id reference for this relationship

getCorrelationId

public String getCorrelationId()
Sets a correlationId for this message. The correlation Id can be used by components in the system to manage message relations.

The correlationId is associated with the message using the underlying transport protocol. As such not all messages will support the notion of a correlationId i.e. tcp or file. In this situation the correlation Id is set as a property of the message where it's up to developer to keep the association with the message. For example if the message is serialised to xml the correlationId will be available in the message.

Specified by:
getCorrelationId in interface MessageAdapter
Returns:
the correlationId for this message or null if one hasn't been set

setReplyTo

public void setReplyTo(Object replyTo)
Sets a replyTo address for this message. This is useful in an asynchronous environment where the caller doesn't wait for a response and the response needs to be routed somewhere for further processing. The value of this field can be any valid endpointUri url.

Specified by:
setReplyTo in interface MessageAdapter
Parameters:
replyTo - the endpointUri url to reply to

getReplyTo

public Object getReplyTo()
Returns a replyTo address for this message. This is useful in an asynchronous environment where the caller doesn't wait for a response and the response needs to be routed somewhere for further processing. The value of this field can be any valid endpointUri url.

Specified by:
getReplyTo in interface MessageAdapter
Returns:
the endpointUri url to reply to or null if one has not been set

getCorrelationSequence

public int getCorrelationSequence()
Gets the sequence or ordering number for this message in the the correlation group (as defined by the correlationId)

Specified by:
getCorrelationSequence in interface MessageAdapter
Returns:
the sequence number or -1 if the sequence is not important

setCorrelationSequence

public void setCorrelationSequence(int sequence)
Gets the sequence or ordering number for this message in the the correlation group (as defined by the correlationId)

Specified by:
setCorrelationSequence in interface MessageAdapter
Parameters:
sequence - the sequence number or -1 if the sequence is not important

getCorrelationGroupSize

public int getCorrelationGroupSize()
Determines how many messages are in the correlation group

Specified by:
getCorrelationGroupSize in interface MessageAdapter
Returns:
total messages in this group or -1 if the size is not known

setCorrelationGroupSize

public void setCorrelationGroupSize(int size)
Description copied from interface: MessageAdapter
Determines how many messages are in the correlation group

Specified by:
setCorrelationGroupSize in interface MessageAdapter
Parameters:
size - the total messages in this group or -1 if the size is not known

getExceptionPayload

public ExceptionPayload getExceptionPayload()
If an error occurred during the processing of this message this will return a ErrorPayload that contains the root exception and Mule error code, plus any other releated info

Specified by:
getExceptionPayload in interface MessageAdapter
Returns:
The exception payload (if any) attached to this message

setExceptionPayload

public void setExceptionPayload(ExceptionPayload exceptionPayload)
If an error occurs while processing this message, a ErrorPayload is attached which contains the root exception and Mule error code, plus any other releated info.

Specified by:
setExceptionPayload in interface MessageAdapter
Parameters:
exceptionPayload - The exception payload to attach to this message

toString

public String toString()

Overrides:
toString in class Object

addAttachment

public void addAttachment(String name,
                          DataHandler dataHandler)
                   throws Exception
Allows for arbitary data attachments to be associated with the Message. These attachements work in the same way that email attachments work. Attachments can be binary or text

Specified by:
addAttachment in interface MessageAdapter
Parameters:
name - the name to associate with the attachment
dataHandler - The attachment datahandler to use. This will be used to interract with the attachment data
Throws:
Exception
See Also:
DataHandler

removeAttachment

public void removeAttachment(String name)
                      throws Exception
Remove an attahcment form this message with the specifed name

Specified by:
removeAttachment in interface MessageAdapter
Parameters:
name - the name of the attachment to remove. If the attachment does not exist, the request may be ignorred
Throws:
Exception - different messaging systems handle attachments differnetly, as such some will throw an exception if an attahcment does dot exist.

getAttachment

public DataHandler getAttachment(String name)
Retrieve an attachment with the given name. If the attachment does not exist, null will be returned

Specified by:
getAttachment in interface MessageAdapter
Parameters:
name - the name of the attachment to retrieve
Returns:
the attachment with the given name or null if the attachment does not exist
See Also:
DataHandler

getAttachmentNames

public Set getAttachmentNames()
Returns a set of the names of the attachments on this message. If there are no attachments an empty set will be returned.

Specified by:
getAttachmentNames in interface MessageAdapter
Returns:
a set of the names of the attachments on this message. If there are no attachments an empty set will be returned.

getEncoding

public String getEncoding()
Gets the encoding for the current message. For potocols that send encoding information with the message, this method should be overriden to expose the transport encoding, otherwise the default encoding in the Mule configuration will be used.

Specified by:
getEncoding in interface MessageAdapter
Returns:
the encoding for this message. This method must never return null

setEncoding

public void setEncoding(String encoding)
Sets the encoding for this message

Specified by:
setEncoding in interface MessageAdapter
Parameters:
encoding - the encoding to use

getStringProperty

public String getStringProperty(String name,
                                String defaultValue)
Gets a String property from the message

Specified by:
getStringProperty in interface MessageAdapter
Parameters:
name - the name or key of the property
defaultValue - a default value if the property doesn't exist in the event
Returns:
the property value or the defaultValue if the property does not exist

setStringProperty

public void setStringProperty(String name,
                              String value)
Sets a String property on the message

Specified by:
setStringProperty in interface MessageAdapter
Parameters:
name - the property name or key
value - the property value

addProperties

public void addProperties(Map properties)
Adds a map of properties to be associated with this message

Specified by:
addProperties in interface MessageAdapter
Parameters:
properties - the properties add to this message

addProperties

public void addProperties(Map properties,
                          PropertyScope scope)
Adds a map of properties to be associated with this message

Specified by:
addProperties in interface MessageAdapter
Parameters:
properties - the properties add to this message
scope - the scope in which the proeprties should be added

clearProperties

public void clearProperties()
Removes all properties on this message

Specified by:
clearProperties in interface MessageAdapter

getPayload

public Object getPayload()

Specified by:
getPayload in interface MessageAdapter
Returns:
the current message

setPayload

public void setPayload(Object payload)
Update the message payload. This is typically only called if the payload was originally an InputStream. In which case, if the InputStream is consumed, it needs to be replaced for future access.

Specified by:
setPayload in interface MuleMessage
Parameters:
payload - the object to assign as the message payload

release

public void release()
Perform any clean up operations on the message resource. Typically this is used to esure that a message stream is closed

Specified by:
release in interface MessageAdapter

applyTransformers

public void applyTransformers(List transformers)
                       throws TransformerException
Will apply a list of transformers to the payload of the message. This *Will* change the payload of the message. This method provides the only way to alter the paylaod of this message without recreating a copy of the message

Specified by:
applyTransformers in interface MuleMessage
Parameters:
transformers - the transformers to apply to the message payload
Throws:
TransformerException - if a transformation error occurs or one or more of the transformers passed in a are incompatible with the message payload

applyTransformers

public void applyTransformers(List transformers,
                              Class outputType)
                       throws TransformerException
Description copied from interface: MuleMessage
Will apply a list of transformers to the payload of the message. This *Will* change the payload of the message. This method provides the only way to alter the paylaod of this message without recreating a copy of the message

Specified by:
applyTransformers in interface MuleMessage
Parameters:
transformers - the transformers to apply to the message payload
outputType - the required output type for this transformation. by adding this parameter some additional transformations will occur on the message payload to ensure that the final payload is of the specified type. If no transformers can be found in the registry that can transform from the return type of the transformation list to the outputType and exception will be thrown
Throws:
TransformerException - if a transformation error occurs or one or more of the transformers passed in a are incompatible with the message payload

applyAllTransformers

protected void applyAllTransformers(List transformers)
                             throws TransformerException
Throws:
TransformerException

newThreadCopy

public ThreadSafeAccess newThreadCopy()

Specified by:
newThreadCopy in interface ThreadSafeAccess
Returns:
A new instance of the implementing class, unbound to any thread and mutable.

resetAccessControl

public void resetAccessControl()
This method should ONLY be used in the construction of composite ThreadSafeAccess instances. For example, a ThreadSafeAccess MuleEvent contains a ThreadSafeAccess MessageAdapter. During the construction of the event, the message adapter may be bound to the contructing thread. Calling this method releases that binding so that the event as a whole can be passed to a new thread unbound.

Specified by:
resetAccessControl in interface ThreadSafeAccess

assertAccess

public void assertAccess(boolean write)
This method may be called before data in the object are accessed. It should verify that the access policy is followed correctly (if not, a runtime exception may be thrown).

Specified by:
assertAccess in interface ThreadSafeAccess
Parameters:
write - True if the access will mutate values.

isConsumable

public boolean isConsumable()
Determines if the payload of this message is consumable i.e. it can't be read more than once. This is here temporarily without adding to MuleMessage interface until MULE-4256 is implemented.

Returns:


Copyright © 2003-2009 MuleSource, Inc.. All Rights Reserved.