View Javadoc

1   /*
2    * $Id: WriterMessageAdapter.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;
12  
13  import org.mule.impl.ThreadSafeAccess;
14  import org.mule.umo.provider.MessageTypeNotSupportedException;
15  
16  import java.io.IOException;
17  import java.io.StringWriter;
18  import java.io.Writer;
19  
20  /**
21   * <code>WriterMessageAdapter</code> wraps a java.io.StringWriter and allows meta
22   * information to be associated with the Writer.
23   */
24  public class WriterMessageAdapter extends AbstractMessageAdapter
25  {
26      /**
27       * Serial version
28       */
29      private static final long serialVersionUID = -1065602752454818625L;
30  
31      private final StringWriter writer;
32  
33      public WriterMessageAdapter(Object message) throws MessageTypeNotSupportedException
34      {
35          if (message instanceof String)
36          {
37              writer = new StringWriter();
38              writer.write((String) message);
39          }
40          else if (message instanceof StringWriter)
41          {
42              this.writer = (StringWriter) message;
43          }
44          else
45          {
46              throw new MessageTypeNotSupportedException(message, getClass());
47          }
48      }
49  
50      protected WriterMessageAdapter(WriterMessageAdapter template)
51      {
52          super(template);
53          writer = template.writer;
54      }
55  
56      /**
57       * Converts the message implementation into a String representation
58       * 
59       * @param encoding The encoding to use when transforming the message (if
60       *            necessary). The parameter is used when converting from a byte array
61       * @return String representation of the message payload
62       * @throws Exception Implementation may throw an endpoint specific exception
63       */
64      public String getPayloadAsString(String encoding) throws Exception
65      {
66          return writer.toString();
67      }
68  
69      /**
70       * Converts the message implementation into a String representation
71       * 
72       * @return String representation of the message
73       * @throws Exception Implemetation may throw an endpoint specific exception
74       */
75      public byte[] getPayloadAsBytes() throws Exception
76      {
77          return writer.toString().getBytes();
78      }
79  
80      /**
81       * @return the current message
82       */
83      public Object getPayload()
84      {
85          return writer.toString();
86      }
87  
88      public void write(String string)
89      {
90          writer.write(string);
91      }
92  
93      public void write(String string, int offset, int len)
94      {
95          writer.write(string, offset, len);
96      }
97  
98      public Writer getWriter()
99      {
100         return writer;
101     }
102 
103     public void flush()
104     {
105         writer.flush();
106     }
107 
108     public void close() throws IOException
109     {
110         writer.close();
111     }
112 
113     public ThreadSafeAccess newThreadCopy()
114     {
115         return new WriterMessageAdapter(this);
116     }
117 
118 }