KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > jms > message > ObjectMessageImpl


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.jms.message;
8
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.io.ByteArrayOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InvalidClassException JavaDoc;
13 import java.io.NotSerializableException JavaDoc;
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.io.Serializable JavaDoc;
17 import javax.jms.JMSException JavaDoc;
18 import javax.jms.MessageFormatException JavaDoc;
19 import javax.jms.MessageNotWriteableException JavaDoc;
20 import javax.jms.ObjectMessage JavaDoc;
21
22 /**
23  * <p/>
24  * This class provides an implementation of the JMS ObjectMessage. An <CODE>
25  * ObjectMessage</CODE> object is used to send a message that contains a
26  * serializable object in the Java programming language ("Java object"). It
27  * inherits from the <CODE>Message</CODE> interface and adds a body
28  * containing a single reference to an object. Only <CODE>Serializable</CODE>
29  * Java objects can be used.
30  * </p>
31  *
32  * @author <a HREF="mailto:founder_chen@yahoo.com.cn">Peter.Cheng</a>
33  * @version Revision: 1.1 Date: 2002-12-08 11:25:10
34  */

35
36 public class ObjectMessageImpl
37         extends JMSMessage
38         implements ObjectMessage JavaDoc, Serializable JavaDoc {
39
40     private byte[] body;
41
42     /**
43      * Default constructor
44      */

45     public ObjectMessageImpl() {
46         super();
47     }
48
49     /**
50      * Sets the serializable object containing this message's data. It is
51      * important to note that an <CODE>ObjectMessage</CODE> contains a
52      * snapshot of the object at the time <CODE>setObject()</CODE> is called;
53      * subsequent modifications of the object will have no effect on the <CODE>
54      * ObjectMessage</CODE> body.
55      *
56      * @param object the message's data
57      * @throws javax.jms.JMSException if the JMS provider fails to set the object due to some
58      * internal error.
59      * @throws javax.jms.MessageFormatException
60      * if object serialization fails.
61      * @throws javax.jms.MessageNotWriteableException
62      * if the message is in read-only mode.
63      * @see javax.jms.ObjectMessage#setObject(java.io.Serializable)
64      */

65     public void setObject(Serializable JavaDoc object) throws JMSException JavaDoc {
66         if (isBodyModifiable()) {
67             try {
68                 ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
69                 ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
70                 oos.writeObject(object);
71                 oos.flush();
72                 oos.close();
73                 body = baos.toByteArray();
74             } catch (InvalidClassException JavaDoc e) {
75                 throw new MessageFormatException JavaDoc(e.getMessage());
76             } catch (NotSerializableException JavaDoc e) {
77                 throw new MessageFormatException JavaDoc(e.getMessage());
78             } catch (IOException JavaDoc e) {
79                 throw new MessageFormatException JavaDoc(e.getMessage());
80             }
81         } else {
82             throw new MessageNotWriteableException JavaDoc("ObjectMessage body read_only");
83         }
84     }
85
86     /**
87      * Gets the serializable object containing this message's data. The default
88      * value is null.
89      *
90      * @return the serializable object containing this message's data
91      * @throws javax.jms.JMSException if the JMS provider fails to get the object due to some
92      * internal error.
93      * @throws javax.jms.MessageFormatException
94      * if object deserialization fails.
95      * @see javax.jms.ObjectMessage#getObject()
96      */

97     public Serializable JavaDoc getObject() throws JMSException JavaDoc {
98         Serializable JavaDoc object = null;
99
100         if (body != null) {
101             try {
102                 ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(body);
103                 ObjectInputStream JavaDoc ins = new ObjectInputStream JavaDoc(bais);
104                 object = (Serializable JavaDoc) ins.readObject();
105                 ins.close();
106             } catch (ClassNotFoundException JavaDoc e) {
107                 throw new MessageFormatException JavaDoc(e.getMessage());
108             } catch (InvalidClassException JavaDoc e) {
109                 throw new MessageFormatException JavaDoc(e.getMessage());
110             } catch (IOException JavaDoc e) {
111                 throw new MessageFormatException JavaDoc(e.getMessage());
112             }
113         }
114         return object;
115     }
116
117     /**
118      * Sets the object reference in the message to null and toggles the message
119      * to read/write state.
120      *
121      * @throws javax.jms.JMSException
122      */

123     public void clearBody() throws JMSException JavaDoc {
124         super.clearBody();
125         body = null;
126     }
127 }
128
Popular Tags