001: /*
002: * $Id: WriterMessageAdapter.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport;
012:
013: import org.mule.api.ThreadSafeAccess;
014: import org.mule.api.transport.MessageTypeNotSupportedException;
015:
016: import java.io.IOException;
017: import java.io.StringWriter;
018: import java.io.Writer;
019:
020: /**
021: * <code>WriterMessageAdapter</code> wraps a java.io.StringWriter and allows meta
022: * information to be associated with the Writer.
023: */
024: public class WriterMessageAdapter extends AbstractMessageAdapter {
025: /**
026: * Serial version
027: */
028: private static final long serialVersionUID = -1065602752454818625L;
029:
030: private final StringWriter writer;
031:
032: public WriterMessageAdapter(Object message)
033: throws MessageTypeNotSupportedException {
034: if (message instanceof String) {
035: writer = new StringWriter();
036: writer.write((String) message);
037: } else if (message instanceof StringWriter) {
038: this .writer = (StringWriter) message;
039: } else {
040: throw new MessageTypeNotSupportedException(message,
041: getClass());
042: }
043: }
044:
045: protected WriterMessageAdapter(WriterMessageAdapter template) {
046: super (template);
047: writer = template.writer;
048: }
049:
050: /**
051: * Converts the message implementation into a String representation
052: *
053: * @param encoding The encoding to use when transforming the message (if
054: * necessary). The parameter is used when converting from a byte array
055: * @return String representation of the message payload
056: * @throws Exception Implementation may throw an endpoint specific exception
057: */
058: public String getPayloadAsString(String encoding) throws Exception {
059: return writer.toString();
060: }
061:
062: /**
063: * Converts the message implementation into a String representation
064: *
065: * @return String representation of the message
066: * @throws Exception Implemetation may throw an endpoint specific exception
067: */
068: public byte[] getPayloadAsBytes() throws Exception {
069: return writer.toString().getBytes();
070: }
071:
072: /**
073: * @return the current message
074: */
075: public Object getPayload() {
076: return writer.toString();
077: }
078:
079: public void write(String string) {
080: writer.write(string);
081: }
082:
083: public void write(String string, int offset, int len) {
084: writer.write(string, offset, len);
085: }
086:
087: public Writer getWriter() {
088: return writer;
089: }
090:
091: public void flush() {
092: writer.flush();
093: }
094:
095: public void close() throws IOException {
096: writer.close();
097: }
098:
099: public ThreadSafeAccess newThreadCopy() {
100: return new WriterMessageAdapter(this);
101: }
102:
103: }
|