KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > jms > message > JmsObjectMessage


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  * Copyright 2001 Dan Greff
20  */

21 package com.presumo.jms.message;
22
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.DataInput JavaDoc;
26 import java.io.DataOutput JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.io.Serializable JavaDoc;
31
32 import javax.jms.ObjectMessage JavaDoc;
33 import javax.jms.JMSException JavaDoc;
34 import javax.jms.MessageFormatException JavaDoc;
35 import javax.jms.MessageNotWriteableException JavaDoc;
36
37 /**
38  * Implementation of the JMS interface Object Message
39  *
40  * @see javax.jms.ObjectMessage
41  */

42 public final class JmsObjectMessage extends JmsMessage
43   implements ObjectMessage JavaDoc, java.io.Externalizable JavaDoc
44 {
45   //
46
// byte array containing the serialized user object
47
//
48
private byte [] data;
49
50     /////////////////////////////////////////////////////////////////////////
51
// Constructors //
52
/////////////////////////////////////////////////////////////////////////
53
public JmsObjectMessage()
54   {
55     super();
56   }
57   public JmsObjectMessage(String JavaDoc name)
58   {
59     super(name);
60   }
61
62   public JmsObjectMessage(byte [] msg, int offset, int length)
63     throws IOException JavaDoc
64   {
65     super(msg, offset, length);
66   }
67   
68     /////////////////////////////////////////////////////////////////////////
69
// Public Methods //
70
/////////////////////////////////////////////////////////////////////////
71

72   /**
73    * @see javax.jms.ObjectMessage#setObject(Serializable)
74    */

75   public void setObject(Serializable JavaDoc object) throws JMSException JavaDoc
76   {
77     checkWrite();
78
79     try {
80       
81       ByteArrayOutputStream JavaDoc byteStream = new ByteArrayOutputStream JavaDoc();
82       ObjectOutputStream JavaDoc objectStream = new ObjectOutputStream JavaDoc(byteStream);
83       objectStream.writeObject(object);
84       objectStream.flush();
85       objectStream.close();
86       this.data = byteStream.toByteArray();
87     
88     } catch (IOException JavaDoc e) {
89       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to serialize object.");
90       jmse.setLinkedException(e);
91       throw jmse;
92     }
93   }
94
95
96   /**
97    * @see javax.jms.ObjectMessage#getObject()
98    */

99   public Serializable JavaDoc getObject() throws JMSException JavaDoc
100   {
101     if (data == null || data.length == 0) {
102       return null;
103     }
104
105     Serializable JavaDoc retval = null;
106     try {
107          
108       ByteArrayInputStream JavaDoc byteStream = new ByteArrayInputStream JavaDoc(data);
109       ObjectInputStream JavaDoc objectStream = new ObjectInputStream JavaDoc(byteStream);
110       retval = (Serializable JavaDoc) objectStream.readObject();
111       objectStream.close();
112
113     } catch (IOException JavaDoc e) {
114       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to deserialize object.");
115       jmse.setLinkedException(e);
116       throw jmse;
117     } catch (ClassNotFoundException JavaDoc e) {
118       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to deserialize object.");
119       jmse.setLinkedException(e);
120       throw jmse;
121     }
122     return retval;
123   }
124
125   /**
126    * @see javax.jms.Message#clearBody()
127    */

128   public void clearBody() throws JMSException JavaDoc
129   {
130     checkWrite();
131     super.clearBody();
132     this.data = null;
133   }
134
135   
136   /**
137    */

138   public void unmarshal(DataInput JavaDoc in) throws IOException JavaDoc
139   {
140     super.unmarshal(in);
141     
142     int arraySize = in.readInt();
143     if (arraySize > 0) {
144       this.data = new byte[arraySize];
145       in.readFully(data, 0, arraySize);
146     }
147   }
148   
149   /**
150    */

151   public void marshal(DataOutput JavaDoc out) throws IOException JavaDoc
152   {
153     super.marshal(out);
154     
155     if (data == null || data.length == 0)
156       out.writeInt(0);
157     else {
158       out.writeInt(data.length);
159       out.write(data);
160     }
161   }
162
163   public String JavaDoc toString()
164   {
165     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
166     result.append(super.toString());
167     if (data == null) {
168       result.append(" No serialized object ");
169     } else {
170       result.append(" Serialized object size: " + data.length);
171     }
172     return result.toString();
173   }
174
175     ///////////////////////////////////////////////////////////////////////////
176
// Package Methods //
177
///////////////////////////////////////////////////////////////////////////
178

179
180   final byte getMarshalingID()
181   {
182     return MessageEncoder.OBJECT_MESSAGE;
183   }
184
185 }
186
187
Popular Tags