KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > WriterMessageAdapter


1 /*
2  * $Id: WriterMessageAdapter.java 3982 2006-11-22 14:28:01Z lajos $
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 MuleSource MPL
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.umo.provider.MessageTypeNotSupportedException;
14
15 import java.io.IOException JavaDoc;
16 import java.io.StringWriter JavaDoc;
17 import java.io.Writer JavaDoc;
18
19 /**
20  * <code>WriterMessageAdapter</code> wraps a java.io.StringWriter and allows meta
21  * information to be associated with the Writer.
22  */

23 public class WriterMessageAdapter extends AbstractMessageAdapter
24 {
25     /**
26      * Serial version
27      */

28     private static final long serialVersionUID = -1065602752454818625L;
29
30     private final StringWriter JavaDoc writer;
31
32     public WriterMessageAdapter(Object JavaDoc message) throws MessageTypeNotSupportedException
33     {
34         if (message instanceof String JavaDoc)
35         {
36             writer = new StringWriter JavaDoc();
37             writer.write((String JavaDoc)message);
38         }
39         else if (message instanceof StringWriter JavaDoc)
40         {
41             this.writer = (StringWriter JavaDoc)message;
42         }
43         else
44         {
45             throw new MessageTypeNotSupportedException(message, getClass());
46         }
47
48     }
49
50     /**
51      * Converts the message implementation into a String representation
52      *
53      * @param encoding The encoding to use when transforming the message (if
54      * necessary). The parameter is used when converting from a byte array
55      * @return String representation of the message payload
56      * @throws Exception Implementation may throw an endpoint specific exception
57      */

58     public String JavaDoc getPayloadAsString(String JavaDoc encoding) throws Exception JavaDoc
59     {
60         return writer.toString();
61     }
62
63     /**
64      * Converts the message implementation into a String representation
65      *
66      * @return String representation of the message
67      * @throws Exception Implemetation may throw an endpoint specific exception
68      */

69     public byte[] getPayloadAsBytes() throws Exception JavaDoc
70     {
71         return writer.toString().getBytes();
72     }
73
74     /**
75      * @return the current message
76      */

77     public Object JavaDoc getPayload()
78     {
79         return writer.toString();
80     }
81
82     public void write(String JavaDoc string)
83     {
84         writer.write(string);
85     }
86
87     public void write(String JavaDoc string, int offset, int len)
88     {
89         writer.write(string, offset, len);
90     }
91
92     public Writer JavaDoc getWriter()
93     {
94         return writer;
95     }
96
97     public void flush()
98     {
99         writer.flush();
100     }
101
102     public void close() throws IOException JavaDoc
103     {
104         writer.close();
105     }
106 }
107
Popular Tags