KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > command > ActiveMQObjectMessage


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq.command;
20
21
22
23
24 import org.apache.activemq.ActiveMQConnection;
25 import org.apache.activemq.util.ByteArrayInputStream;
26 import org.apache.activemq.util.ByteArrayOutputStream;
27 import org.apache.activemq.util.ByteSequence;
28 import org.apache.activemq.util.ClassLoadingAwareObjectInputStream;
29 import org.apache.activemq.util.JMSExceptionSupport;
30
31 import javax.jms.JMSException JavaDoc;
32 import javax.jms.ObjectMessage JavaDoc;
33 import java.io.DataInputStream JavaDoc;
34 import java.io.DataOutputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.ObjectOutputStream JavaDoc;
38 import java.io.OutputStream JavaDoc;
39 import java.io.Serializable JavaDoc;
40 import java.util.zip.DeflaterOutputStream JavaDoc;
41 import java.util.zip.InflaterInputStream JavaDoc;
42
43 /**
44  * An <CODE>ObjectMessage</CODE> object is used to send a message that contains a serializable object in the Java
45  * programming language ("Java object"). It inherits from the <CODE>Message</CODE> interface and adds a body containing
46  * a single reference to an object. Only <CODE>Serializable</CODE> Java objects can be used.
47  * <p/>
48  * <P>If a collection of Java objects must be sent, one of the <CODE>Collection</CODE> classes provided since JDK 1.2
49  * can be used.
50  * <p/>
51  * <P>When a client receives an <CODE>ObjectMessage</CODE>, it is in read-only mode. If a client attempts to write to
52  * the message at this point, a <CODE>MessageNotWriteableException</CODE> is thrown. If <CODE>clearBody</CODE> is
53  * called, the message can now be both read from and written to.
54  *
55  * @openwire:marshaller code="26"
56  * @see javax.jms.Session#createObjectMessage()
57  * @see javax.jms.Session#createObjectMessage(Serializable)
58  * @see javax.jms.BytesMessage
59  * @see javax.jms.MapMessage
60  * @see javax.jms.Message
61  * @see javax.jms.StreamMessage
62  * @see javax.jms.TextMessage
63  */

64 public class ActiveMQObjectMessage extends ActiveMQMessage implements ObjectMessage JavaDoc {
65     static final ClassLoader JavaDoc ACTIVEMQ_CLASSLOADER = ActiveMQObjectMessage.class.getClassLoader(); //TODO verify classloader
66
public static final byte DATA_STRUCTURE_TYPE = CommandTypes.ACTIVEMQ_OBJECT_MESSAGE;
67
68     protected transient Serializable JavaDoc object;
69
70     public Message copy() {
71         ActiveMQObjectMessage copy = new ActiveMQObjectMessage();
72         copy(copy);
73         return copy;
74     }
75
76     private void copy(ActiveMQObjectMessage copy) {
77         storeContent();
78         super.copy(copy);
79         copy.object=null;
80     }
81         
82     public void storeContent() {
83         ByteSequence bodyAsBytes = getContent();
84         if (bodyAsBytes == null && object != null) {
85             try {
86                 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
87                 OutputStream os = bytesOut;
88                 ActiveMQConnection connection = getConnection();
89                 if (connection!=null && connection.isUseCompression()) {
90                     compressed = true;
91                     os = new DeflaterOutputStream JavaDoc(os);
92                 }
93                 DataOutputStream JavaDoc dataOut = new DataOutputStream JavaDoc(os);
94                 ObjectOutputStream JavaDoc objOut = new ObjectOutputStream JavaDoc(dataOut);
95                 objOut.writeObject(object);
96                 objOut.flush();
97                 objOut.reset();
98                 objOut.close();
99                 setContent(bytesOut.toByteSequence());
100             } catch (IOException JavaDoc ioe) {
101                 throw new RuntimeException JavaDoc(ioe.getMessage(), ioe);
102             }
103         }
104     }
105
106     public byte getDataStructureType() {
107         return DATA_STRUCTURE_TYPE;
108     }
109
110     public String JavaDoc getJMSXMimeType() {
111         return "jms/object-message";
112     }
113
114
115     /**
116      * Clears out the message body. Clearing a message's body does not clear its header values or property entries.
117      * <p/>
118      * <P>If this message body was read-only, calling this method leaves the message body in the same state as an empty
119      * body in a newly created message.
120      *
121      * @throws JMSException if the JMS provider fails to clear the message body due to some internal error.
122      */

123
124     public void clearBody() throws JMSException JavaDoc {
125         super.clearBody();
126         this.object = null;
127     }
128
129     /**
130      * Sets the serializable object containing this message's data. It is important to note that an
131      * <CODE>ObjectMessage</CODE> contains a snapshot of the object at the time <CODE>setObject()</CODE> is called;
132      * subsequent modifications of the object will have no effect on the <CODE>ObjectMessage</CODE> body.
133      *
134      * @param newObject the message's data
135      * @throws JMSException if the JMS provider fails to set the object due to some internal error.
136      * @throws javax.jms.MessageFormatException
137      * if object serialization fails.
138      * @throws javax.jms.MessageNotWriteableException
139      * if the message is in read-only mode.
140      */

141
142     public void setObject(Serializable JavaDoc newObject) throws JMSException JavaDoc {
143         checkReadOnlyBody();
144         this.object = newObject;
145         setContent(null);
146         ActiveMQConnection connection = getConnection();
147         if( connection==null || !connection.isObjectMessageSerializationDefered() ) {
148             storeContent();
149         }
150     }
151
152
153     /**
154      * Gets the serializable object containing this message's data. The default value is null.
155      *
156      * @return the serializable object containing this message's data
157      * @throws JMSException
158      */

159     public Serializable JavaDoc getObject() throws JMSException JavaDoc {
160         if (object == null && getContent()!=null ) {
161             try {
162                 ByteSequence content = getContent();
163                 InputStream is = new ByteArrayInputStream(content);
164                 if( isCompressed() ) {
165                     is = new InflaterInputStream JavaDoc(is);
166                 }
167                 DataInputStream JavaDoc dataIn = new DataInputStream JavaDoc(is);
168                 ClassLoadingAwareObjectInputStream objIn = new ClassLoadingAwareObjectInputStream(dataIn);
169                 try {
170                     object = (Serializable JavaDoc) objIn.readObject();
171                 } catch (ClassNotFoundException JavaDoc ce) {
172                     throw new IOException JavaDoc(ce.getMessage());
173                 }
174                 dataIn.close();
175             } catch (IOException JavaDoc e) {
176                 throw JMSExceptionSupport.create("Failed to build body from bytes. Reason: " + e, e);
177             }
178         }
179         return this.object;
180     }
181
182     public void onMessageRolledBack() {
183         super.onMessageRolledBack();
184         
185         // lets force the object to be deserialized again - as we could have changed the object
186
object = null;
187     }
188
189     public String JavaDoc toString() {
190         try {
191             getObject();
192         } catch (JMSException JavaDoc e) {
193         }
194         return super.toString();
195     }
196 }
197
Popular Tags