View Javadoc

1   /*
2    * $Id: StreamMessageAdapter.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.stream;
12  
13  import org.mule.impl.ThreadSafeAccess;
14  import org.mule.providers.AbstractMessageAdapter;
15  import org.mule.umo.provider.MessageTypeNotSupportedException;
16  
17  /**
18   * <code>StreamMessageAdapter</code> TODO document
19   */
20  public class StreamMessageAdapter extends AbstractMessageAdapter
21  {
22      /**
23       * Serial version
24       */
25      private static final long serialVersionUID = 3094357859680956607L;
26  
27      // TODO shouldn't this be an Object, for handling at least byte[]s too?
28      private final String message;
29  
30      public StreamMessageAdapter(Object message) throws MessageTypeNotSupportedException
31      {
32          if (message instanceof String)
33          {
34              this.message = (String)message;
35          }
36          else
37          {
38              throw new MessageTypeNotSupportedException(message, StreamMessageAdapter.class);
39          }
40      }
41  
42      protected StreamMessageAdapter(StreamMessageAdapter template)
43      {
44          super(template);
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.toString();
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          String msg = getPayloadAsString();
70          return msg.getBytes();
71      }
72  
73      /**
74       * @return the current message
75       */
76      public Object getPayload()
77      {
78          return message;
79      }
80  
81      public ThreadSafeAccess newThreadCopy()
82      {
83          return new StreamMessageAdapter(this);
84      }
85      
86  }