View Javadoc

1   /*
2    * $Id: VMMessageAdapter.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.vm;
12  
13  import org.mule.impl.ThreadSafeAccess;
14  import org.mule.providers.AbstractMessageAdapter;
15  import org.mule.umo.UMOMessage;
16  import org.mule.umo.provider.MessageTypeNotSupportedException;
17  
18  /**
19   * <code>VMMessageAdapter</code> provides a common abstraction of Mule Event
20   * message. The message adapter allows a Mule event to be read and manipulated like
21   * any other object data type from any external system that has a Mule endpoint
22   * implementation.
23   */
24  public class VMMessageAdapter extends AbstractMessageAdapter
25  {
26      /**
27       * Serial version
28       */
29      private static final long serialVersionUID = 4037066880189053665L;
30  
31      /**
32       * The message itself in this case an UMOEvent
33       */
34      private UMOMessage message = null;
35  
36      public VMMessageAdapter(UMOMessage message) throws MessageTypeNotSupportedException
37      {
38          setMessage(message);
39      }
40  
41      protected VMMessageAdapter(VMMessageAdapter template)
42      {
43          super(template);
44          // if this is an event, should we do a deep copy?
45          message = template.message;
46      }
47  
48      /**
49       * Converts the message implementation into a String representation
50       * 
51       * @param encoding The encoding to use when transforming the message (if
52       *            necessary). The parameter is used when converting from a byte array
53       * @return String representation of the message payload
54       * @throws Exception Implementation may throw an endpoint specific exception
55       */
56      public String getPayloadAsString(String encoding) throws Exception
57      {
58          return message.getPayloadAsString(encoding);
59      }
60  
61      /**
62       * Converts the message implementation into a String representation
63       * 
64       * @return String representation of the message
65       * @throws Exception Implemetation may throw an endpoint specific exception
66       */
67      public byte[] getPayloadAsBytes() throws Exception
68      {
69          return convertToBytes(message.getPayload());
70      }
71  
72      /**
73       * @return the current message
74       */
75      public Object getPayload()
76      {
77          return message;
78      }
79  
80      /**
81       * @param message new value for the message
82       */
83      private void setMessage(UMOMessage message) throws MessageTypeNotSupportedException
84      {
85          if (message == null)
86          {
87              throw new MessageTypeNotSupportedException(null, getClass());
88          }
89          this.message = message;
90      }
91  
92      public String getUniqueId()
93      {
94          return message.getUniqueId();
95      }
96  
97      public ThreadSafeAccess newThreadCopy()
98      {
99          return new VMMessageAdapter(this);
100     }
101 
102 }