View Javadoc

1   /*
2    * $Id: TcpMessageAdapter.java 7963 2007-08-21 08:53:15Z 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.tcp;
12  
13  import org.mule.impl.MuleMessage;
14  import org.mule.impl.ThreadSafeAccess;
15  import org.mule.providers.AbstractMessageAdapter;
16  
17  import java.io.Serializable;
18  import java.util.Iterator;
19  
20  import org.apache.commons.lang.SerializationUtils;
21  
22  /**
23   * <code>TcpMessageAdapter</code> TODO
24   */
25  
26  public class TcpMessageAdapter extends AbstractMessageAdapter
27  {
28      /**
29       * Serial version
30       */
31      private static final long serialVersionUID = 7229837140160407794L;
32  
33      private Object message;
34  
35      public TcpMessageAdapter(Object message)
36      {
37          if (message instanceof MuleMessage)
38          {
39              MuleMessage muleMessage = (MuleMessage)message;
40              Iterator names = muleMessage.getPropertyNames().iterator();
41              while (names.hasNext())
42              {
43                  Object name = names.next();
44                  this.properties.put(name, muleMessage.getProperty(name.toString()));
45              }
46              this.message = muleMessage.getPayload();
47          }
48          else
49          {
50              this.message = message;
51          }
52      }
53  
54      protected TcpMessageAdapter(TcpMessageAdapter template)
55      {
56          super(template);
57          message = template.message;
58      }
59  
60      /**
61       * Converts the message implementation into a String representation
62       * 
63       * @param encoding The encoding to use when transforming the message (if
64       *         necessary). The parameter is used when converting from a byte array
65       * @return String representation of the message payload
66       * @throws Exception Implementation may throw an endpoint specific exception
67       */
68      public String getPayloadAsString(String encoding) throws Exception
69      {
70          if (message instanceof byte[])
71          {
72              return new String((byte[])message, encoding);
73          }
74          else
75          {
76              return message.toString();
77          }
78      }
79  
80      public byte[] getPayloadAsBytes() throws Exception
81      {
82          if (message instanceof byte[])
83          {
84              return (byte[])message;
85          }
86          else
87          {
88              return SerializationUtils.serialize((Serializable)message);
89          }
90      }
91  
92      public Object getPayload()
93      {
94          return message;
95      }
96  
97      public ThreadSafeAccess newThreadCopy()
98      {
99          return new TcpMessageAdapter(this);
100     }
101 
102 }