001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jms.message;
031:
032: import java.io.*;
033:
034: import javax.jms.JMSException;
035: import javax.jms.TextMessage;
036:
037: import com.caucho.vfs.*;
038: import com.caucho.hessian.io.*;
039:
040: /**
041: * A text message.
042: */
043: public class TextMessageImpl extends MessageImpl implements TextMessage {
044: private String _text;
045:
046: public TextMessageImpl() {
047: }
048:
049: public TextMessageImpl(TextMessage msg) throws JMSException {
050: super (msg);
051:
052: _text = msg.getText();
053: }
054:
055: public TextMessageImpl(TextMessageImpl msg) {
056: super (msg);
057:
058: _text = msg._text;
059: }
060:
061: /**
062: * Returns the type enumeration.
063: */
064: @Override
065: public MessageType getType() {
066: return MessageType.TEXT;
067: }
068:
069: /**
070: * Returns the message text.
071: */
072: public String getText() throws JMSException {
073: return _text;
074: }
075:
076: /**
077: * Returns the message text.
078: */
079: public void setText(String text) throws JMSException {
080: checkBodyWriteable();
081:
082: _text = text;
083: }
084:
085: /**
086: * Clears the body.
087: */
088: public void clearBody() throws JMSException {
089: super .clearBody();
090:
091: _text = null;
092: }
093:
094: public MessageImpl copy() {
095: return new TextMessageImpl(this );
096: }
097:
098: protected void copy(TextMessageImpl newMsg) {
099: super .copy(newMsg);
100:
101: newMsg._text = _text;
102: }
103:
104: /**
105: * Serialize the body to an input stream.
106: */
107: @Override
108: public InputStream bodyToInputStream() throws IOException {
109: if (_text == null)
110: return null;
111:
112: TempStream body = new TempStream();
113: body.openWrite();
114:
115: WriteStream ws = new WriteStream(body);
116:
117: writeBody(ws);
118:
119: ws.close();
120:
121: return body.openRead();
122: }
123:
124: /**
125: * Serialize the body to an output stream.
126: */
127: @Override
128: public void writeBody(OutputStream os) throws IOException {
129: if (_text == null)
130: return;
131:
132: Hessian2Output out = new Hessian2Output(os);
133:
134: out.writeString(_text);
135:
136: out.close();
137: }
138:
139: /**
140: * Read the body from an input stream.
141: */
142: @Override
143: public void readBody(InputStream is) throws IOException,
144: JMSException {
145: if (is == null)
146: return;
147:
148: Hessian2Input in = new Hessian2Input(is);
149:
150: _text = in.readString();
151:
152: in.close();
153: }
154: }
|