KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > jms > MantaObjectMessage


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Nimo 23-MAR-2004.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.api.jms;
47
48 /**
49  * @author Nimo 23-MAR-2004
50  *
51  * An ObjectMessage object is used to send a message that contains a serializable
52  * object in the Java programming language ("Java object"). It inherits from the
53  * Message interface and adds a body containing a single reference to an object.
54  * Only Serializable Java objects can be used.
55  * If a collection of Java objects must be sent, one of the Collection classes
56  * provided since JDK 1.2 can be used.
57  * When a client receives an ObjectMessage, it is in read-only mode. If a client
58  * attempts to write to the message at this point, a MessageNotWriteableException
59  * is thrown. If clearBody is called, the message can now be both read from and
60  * written to.
61  */

62
63 import java.io.IOException JavaDoc;
64 import java.io.Serializable JavaDoc;
65 import javax.jms.JMSException JavaDoc;
66 import javax.jms.MessageFormatException JavaDoc;
67 import javax.jms.MessageNotWriteableException JavaDoc;
68
69 import javax.jms.ObjectMessage JavaDoc;
70 import java.io.ByteArrayInputStream JavaDoc;
71 import java.io.ByteArrayOutputStream JavaDoc;
72 import java.io.ObjectInputStream JavaDoc;
73 import java.io.ObjectOutputStream JavaDoc;
74
75 import org.mr.core.util.byteable.Byteable;
76 import org.mr.core.util.byteable.ByteableInputStream;
77 import org.mr.core.util.byteable.ByteableOutputStream;
78 import org.mr.core.util.byteable.ByteableRegistry;
79
80
81
82 public class MantaObjectMessage extends MantaMessage implements ObjectMessage JavaDoc{
83
84    /**
85      * Comment for <code>serialVersionUID</code>
86      */

87     private static final long serialVersionUID = 7209547801089501407L;
88 //This is the stream for the object as a stream of bytes.
89
private byte[] objectAsBytes = null;
90    //private byte[] backupObjectAsBytes;
91

92    /**
93     * only for makeCopy method use
94     */

95    MantaObjectMessage(){
96         
97     }
98    
99    /**
100     * This is the constructor for this message.
101     * @param sess - the session that created this message.
102     * @throws JMSException
103     */

104    MantaObjectMessage(MantaSession sess) throws JMSException JavaDoc {
105         creatingSession = sess;
106         flags = (flags & ONLY_MSG_TYPES) | OBJECT_MESSAGE;
107     }//MantaObjectMessage
108

109    /**
110     * A constructor using an object to put in the message.
111     *
112     * @param sess - the creating session
113     * @param obj - the object to serialize in the message.
114     *
115     * @throws JMSException
116     */

117    MantaObjectMessage(MantaSession sess, Serializable JavaDoc obj) throws JMSException JavaDoc {
118       this(sess);
119       this.setObject(obj);
120    }
121     /**
122      * Used to populate the object message with an object.
123      * This object must be Serializable.
124      */

125     public void setObject(Serializable JavaDoc object) throws JMSException JavaDoc{
126         if (!isWriteable())
127             throw new MessageNotWriteableException JavaDoc("MNJMS00015 : FAILED ON METHOD setObject(). MESSAGE IS NOT IN A WRITEABLE STATE.");
128         
129         makeObject(object);
130
131     }
132     
133     private void makeObject(Serializable JavaDoc object) throws JMSException JavaDoc{
134         try {
135            ByteArrayOutputStream JavaDoc byteOutput = new ByteArrayOutputStream JavaDoc();
136            ObjectOutputStream JavaDoc objectOutput = new ObjectOutputStream JavaDoc(byteOutput);
137            objectOutput.writeObject(object);
138            objectOutput.flush();
139            objectAsBytes = byteOutput.toByteArray();
140            objectOutput.close();
141         }
142         catch (IOException JavaDoc ioe) {
143             throw new MessageFormatException JavaDoc("MNJMS00016 : FAILED TO INSTANTIATE OBJECT ON METHOD makeObject(). ERROR TEXT : "+ioe.getMessage());
144            
145         }
146     }
147     
148     /**
149      * This returns the Serializable object that is the message's payload.
150      */

151     public Serializable JavaDoc getObject() throws JMSException JavaDoc{
152         Serializable JavaDoc returnObj = null;
153         //get the object from the bytes representaion and put it to a stream.
154
if (this.objectAsBytes!=null) {
155            try {
156               ByteArrayInputStream JavaDoc byteInput =
157                     new ByteArrayInputStream JavaDoc(objectAsBytes);
158               ObjectInputStream JavaDoc objectInput = new ObjectInputStream JavaDoc(byteInput);
159               returnObj = (Serializable JavaDoc) objectInput.readObject();
160               objectInput.close();
161            }
162            catch (Exception JavaDoc ie) {
163                throw new MessageFormatException JavaDoc("MNJMS00017 : FAILED ON METHOD getObject(). ERROR TEXT : "+ie.getMessage());
164               
165            }
166         }
167         
168         return returnObj;
169     }//getObject
170

171     
172     /**
173      * Clear out the message body. Needs to clear the object as well
174      */

175     public final void clearBody() throws JMSException JavaDoc {
176         if (objectAsBytes==null)
177             return;
178         
179         super.clearBody();
180 // if ((flags&ORIG_BODY_SAVED)==0) {
181
// backupObjectAsBytes = new byte[objectAsBytes.length];
182
// System.arraycopy(objectAsBytes,0,backupObjectAsBytes,0,objectAsBytes.length);
183
//
184
flags=flags|ORIG_BODY_SAVED;
185 // }
186
objectAsBytes = null;
187     }//clearBody
188

189     
190     //Byteable methods -----------------------------------------------------
191

192     public void toBytes(ByteableOutputStream out) throws IOException JavaDoc {
193        super.toBytes(out);
194        if (objectAsBytes==null)
195         out.writeInt(0);
196        else {
197         out.writeInt(this.objectAsBytes.length);
198         out.write(this.objectAsBytes);
199        }
200        out.flush();
201     }
202     
203     public Byteable createInstance(ByteableInputStream in) throws IOException JavaDoc {
204        
205        MantaObjectMessage com;
206        try {
207           
208           com = new MantaObjectMessage(null);
209           createHeadersAndProperties(com,in);
210           int byteArrayLength = in.readInt();
211           byte[] objectBytes=null;
212           if (byteArrayLength>0) {
213             objectBytes = new byte[byteArrayLength];
214             in.read(objectBytes);
215             ByteArrayInputStream JavaDoc baos = new ByteArrayInputStream JavaDoc(objectBytes);
216             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(baos);
217             Serializable JavaDoc theObj = (Serializable JavaDoc) ois.readObject();
218             com.makeObject(theObj);
219           }
220           else
221             com.objectAsBytes=null;
222           
223        }
224        catch (Exception JavaDoc ie) {
225            throw new IOException JavaDoc("MNJMS00FFC : METHOD createInstance() FAILED ON MantaObjectMessage. EXCEPTION TEXT : "+ie.getMessage());
226        }
227        
228        return com;
229     }
230     
231     public void registerToByteableRegistry() {
232        ByteableRegistry.registerByteableFactory(getByteableName() , this);
233     }
234     
235     public final static String JavaDoc BYTEABLENAME = "MantaObjectMsg";
236     
237     public String JavaDoc getByteableName() {
238        
239        return BYTEABLENAME;
240     }
241     
242     public static void register() throws JMSException JavaDoc{
243        MantaObjectMessage instance = new MantaObjectMessage(null);
244        instance.registerToByteableRegistry();
245     }
246     
247 // protected void retrieveOldBody() {
248
//
249
// if ((flags&ORIG_BODY_SAVED)>0) {
250
// //someone did actually cleared the body.
251
// if (backupObjectAsBytes==null)
252
// objectAsBytes=null;
253
// else {
254
// objectAsBytes = new byte[backupObjectAsBytes.length];
255
// System.arraycopy(backupObjectAsBytes,0,objectAsBytes,0,backupObjectAsBytes.length);
256
// }
257
// }
258
// //else - body was not touched.
259

260             
261          
262 // }
263

264     public MantaMessage makeCopy() throws JMSException JavaDoc{
265         MantaObjectMessage copy = new MantaObjectMessage();
266         initCopy(copy);
267         // The underlying byte message.
268
copy.objectAsBytes = objectAsBytes;
269         
270         return copy;
271    }
272 //-------------------Byteable methods
273

274 }//MantaObjectMessage
275
Popular Tags