View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2000-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: ObjectMessageImpl.java,v 1.2 2006/06/10 23:21:32 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * 02/26/2000   jimm    Created
47   */
48  
49  package org.exolab.jms.message;
50  
51  import javax.jms.JMSException;
52  import javax.jms.MessageFormatException;
53  import javax.jms.MessageNotWriteableException;
54  import javax.jms.ObjectMessage;
55  import java.io.ByteArrayInputStream;
56  import java.io.ByteArrayOutputStream;
57  import java.io.IOException;
58  import java.io.ObjectInput;
59  import java.io.ObjectInputStream;
60  import java.io.ObjectOutput;
61  import java.io.ObjectOutputStream;
62  import java.io.ObjectStreamClass;
63  import java.io.Serializable;
64  import java.io.InputStream;
65  import java.io.StreamCorruptedException;
66  
67  
68  /***
69   * This class implements the <code>javax.jms.ObjectMessage</code> interface.
70   * <p/>
71   * An ObjectMessage is used to send a message that contains a serializable
72   * Java object. It inherits from <code>Message</code> and adds a body
73   * containing a single Java reference. Only <code>Serializable</code> Java
74   * objects can be used.
75   * <p/>
76   * If a collection of Java objects must be sent, one of the collection
77   * classes provided in JDK 1.2 can be used.
78   * <p/>
79   * When a client receives an ObjectMessage, it is in read-only mode. If a
80   * client attempts to write to the message at this point, a
81   * MessageNotWriteableException is thrown. If <code>clearBody</code> is
82   * called, the message can now be both read from and written to.
83   *
84   * @author <a href="mailto:mourikis@intalio.com">Jim Mourikis</a>
85   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
86   * @version $Revision: 1.2 $ $Date: 2006/06/10 23:21:32 $
87   */
88  public final class ObjectMessageImpl extends MessageImpl
89          implements ObjectMessage {
90  
91      /***
92       * Object version no. for serialization
93       */
94      static final long serialVersionUID = 1;
95  
96      /***
97       * The byte stream to store data
98       */
99      private byte[] _bytes = null;
100 
101     /***
102      * Construct a new ObjectMessage
103      *
104      * @throws JMSException if the message type can't be set
105      */
106     public ObjectMessageImpl() throws JMSException {
107         setJMSType("ObjectMessage");
108     }
109 
110     /***
111      * Clone an instance of this object
112      *
113      * @return a copy of this object
114      * @throws CloneNotSupportedException if object or attributes aren't
115      *                                    cloneable
116      */
117     public final Object clone() throws CloneNotSupportedException {
118         ObjectMessageImpl result = (ObjectMessageImpl) super.clone();
119         if (_bytes != null) {
120             result._bytes = new byte[_bytes.length];
121             System.arraycopy(_bytes, 0, result._bytes, 0, _bytes.length);
122         }
123         return result;
124     }
125 
126     /***
127      * Serialize out this message's data
128      *
129      * @param out the stream to serialize out to
130      * @throws IOException if any I/O exceptions occurr
131      */
132     public final void writeExternal(ObjectOutput out) throws IOException {
133         super.writeExternal(out);
134         out.writeLong(serialVersionUID);
135         if (_bytes != null) {
136             out.writeInt(_bytes.length);
137             out.write(_bytes);
138         } else {
139             out.writeInt(0);
140         }
141     }
142 
143     /***
144      * Serialize in this message's data
145      *
146      * @param in the stream to serialize in from
147      * @throws ClassNotFoundException if the class for an object being
148      *                                restored cannot be found.
149      * @throws IOException            if any I/O exceptions occur
150      */
151     public final void readExternal(ObjectInput in)
152             throws ClassNotFoundException, IOException {
153         super.readExternal(in);
154         long version = in.readLong();
155         if (version == serialVersionUID) {
156             int length = in.readInt();
157             if (length != 0) {
158                 _bytes = new byte[length];
159                 in.readFully(_bytes);
160             } else {
161                 _bytes = null;
162             }
163         } else {
164             throw new IOException(
165                     "Incorrect version enountered: " + version +
166                             ". This version = " + serialVersionUID);
167         }
168     }
169 
170     /***
171      * Set the serializable object containing this message's data.
172      * It is important to note that an <code>ObjectMessage</code>
173      * contains a snapshot of the object at the time <code>setObject()</code>
174      * is called - subsequent modifications of the object will have no
175      * affect on the <code>ObjectMessage</code> body.
176      *
177      * @param object the message's data
178      * @throws MessageFormatException       if object serialization fails
179      * @throws MessageNotWriteableException if the message is read-only
180      */
181     public final void setObject(Serializable object)
182             throws MessageFormatException, MessageNotWriteableException {
183         checkWrite();
184 
185         try {
186             ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
187             ObjectOutputStream out = new ObjectOutputStream(byteOut);
188 
189             out.writeObject(object);
190             out.flush();
191             _bytes = byteOut.toByteArray();
192             out.close();
193         } catch (IOException exception) {
194             MessageFormatException error = new MessageFormatException(
195                     exception.getMessage());
196             error.setLinkedException(exception);
197             throw error;
198         }
199     }
200 
201     /***
202      * Get the serializable object containing this message's data. The
203      * default value is null.
204      *
205      * @return the serializable object containing this message's data
206      * @throws MessageFormatException if object deserialization fails
207      */
208     public final Serializable getObject() throws MessageFormatException {
209         Serializable result = null;
210         if (_bytes != null) {
211             try {
212                 ByteArrayInputStream byteIn =
213                         new ByteArrayInputStream(_bytes);
214                 ObjectInputStream in = new ObjectStream(byteIn);
215 
216                 result = (Serializable) in.readObject();
217                 in.close();
218             } catch (IOException exception) {
219                 MessageFormatException error =
220                         new MessageFormatException(exception.getMessage());
221                 error.setLinkedException(exception);
222                 throw error;
223             } catch (ClassNotFoundException exception) {
224                 MessageFormatException error =
225                         new MessageFormatException(exception.getMessage());
226                 error.setLinkedException(exception);
227                 throw error;
228             }
229         }
230         return result;
231     }
232 
233     /***
234      * Clear out the message body. Clearing a message's body does not clear
235      * its header values or property entries.
236      * If this message body was read-only, calling this method leaves the
237      * message body is in the same state as an empty body in a newly created
238      * message
239      */
240     public final void clearBody() throws JMSException {
241         super.clearBody();
242         _bytes = null;
243     }
244 
245     /***
246      * <code>ObjectInputStream</code> implementation that supports loading
247      * classes from the context class loader.
248      */
249     private class ObjectStream extends ObjectInputStream {
250 
251         public ObjectStream(InputStream inputStream)
252                 throws IOException, StreamCorruptedException {
253             super(inputStream);
254         }
255 
256         protected Class resolveClass(ObjectStreamClass desc)
257                 throws IOException, ClassNotFoundException {
258             ClassLoader loader = Thread.currentThread().getContextClassLoader();
259             if (loader != null) {
260                 try {
261                     return loader.loadClass(desc.getName());
262                 } catch (ClassNotFoundException ignore) {
263                     // no-op
264                 }
265             }
266             return super.resolveClass(desc);
267         }
268     }
269 
270 }