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 com.caucho.jms.JMSExceptionWrapper;
033: import com.caucho.vfs.*;
034: import com.caucho.hessian.io.*;
035:
036: import javax.jms.JMSException;
037: import javax.jms.ObjectMessage;
038: import java.io.*;
039:
040: /**
041: * An object message.
042: */
043: public class ObjectMessageImpl extends MessageImpl implements
044: ObjectMessage {
045: private TempStream _tempStream;
046:
047: public ObjectMessageImpl() {
048: }
049:
050: public ObjectMessageImpl(ObjectMessage msg) throws JMSException {
051: super (msg);
052:
053: setObject(msg.getObject());
054: }
055:
056: public ObjectMessageImpl(ObjectMessageImpl msg) {
057: super (msg);
058:
059: _tempStream = msg._tempStream;
060: }
061:
062: /**
063: * Returns the type enumeration.
064: */
065: @Override
066: public MessageType getType() {
067: return MessageType.OBJECT;
068: }
069:
070: /**
071: * Writes the object to the stream.
072: */
073: public void setObject(Serializable o) throws JMSException {
074: checkBodyWriteable();
075:
076: _tempStream = new TempStream();
077:
078: try {
079: OutputStream ws = new StreamImplOutputStream(_tempStream);
080: Hessian2Output out = new Hessian2Output(ws);
081: out.writeObject(o);
082: out.close();
083: ws.close();
084: } catch (Exception e) {
085: throw JMSExceptionWrapper.create(e);
086: }
087: }
088:
089: /**
090: * Reads the object from the stream.
091: */
092: public Serializable getObject() throws JMSException {
093: if (_tempStream == null)
094: return null;
095:
096: try {
097: ReadStream is = _tempStream.openReadAndSaveBuffer();
098: Hessian2Input in = new Hessian2Input(is);
099: Serializable object = (Serializable) in.readObject();
100: in.close();
101: is.close();
102:
103: return object;
104: } catch (Exception e) {
105: e.printStackTrace();
106: throw JMSExceptionWrapper.create(e);
107: }
108: }
109:
110: /**
111: * Clears the body
112: */
113: public void clearBody() throws JMSException {
114: super .clearBody();
115:
116: _tempStream = null;
117: }
118:
119: public MessageImpl copy() {
120: return new ObjectMessageImpl(this );
121: }
122:
123: /**
124: * Serialize the body to an input stream.
125: */
126: @Override
127: public InputStream bodyToInputStream() throws IOException {
128: if (_tempStream != null)
129: return _tempStream.openReadAndSaveBuffer();
130: else
131: return null;
132: }
133:
134: /**
135: * Read the body from an input stream.
136: */
137: @Override
138: public void readBody(InputStream is) throws IOException,
139: JMSException {
140: if (is == null)
141: return;
142:
143: _tempStream = new TempStream();
144: _tempStream.openWrite();
145:
146: WriteStream ws = new WriteStream(_tempStream);
147: ws.writeStream(is);
148: ws.close();
149: }
150: }
|