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 tma@netspace.net.au.
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 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: BasicObjectMessage.java,v 1.2 2004/02/02 03:49:55 tanderson Exp $
44   */
45  
46  package org.exolab.jmscts.jms.message;
47  
48  import java.io.ByteArrayInputStream;
49  import java.io.ByteArrayOutputStream;
50  import java.io.IOException;
51  import java.io.ObjectInputStream;
52  import java.io.ObjectOutputStream;
53  import java.io.Serializable;
54  
55  import javax.jms.JMSException;
56  import javax.jms.MessageFormatException;
57  import javax.jms.MessageNotWriteableException;
58  import javax.jms.ObjectMessage;
59  
60  
61  /***
62   * This class provides a basic implementation of the javax.jms.ObjectMessage
63   * interface.
64   *
65   * @version     $Revision: 1.2 $ $Date: 2004/02/02 03:49:55 $
66   * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
67   * @see         javax.jms.ObjectMessage
68   */
69  public class BasicObjectMessage extends BasicMessage implements ObjectMessage {
70  
71      /***
72       * The byte stream to store data
73       */
74      private byte[] _bytes = null;
75  
76      /***
77       * Construct a new <code>BasicObjectMessage</code>
78       */
79      public BasicObjectMessage() {
80      }
81  
82      /***
83       * Set the serializable object containing this message's data.
84       * It is important to note that an <code>ObjectMessage</code>
85       * contains a snapshot of the object at the time <code>setObject()</code>
86       * is called - subsequent modifications of the object will have no
87       * affect on the <code>ObjectMessage</code> body.
88       *
89       * @param object the message's data
90       * @throws MessageFormatException if object serialization fails
91       * @throws MessageNotWriteableException if the message is read-only
92       */
93      public final void setObject(Serializable object)
94          throws MessageFormatException, MessageNotWriteableException {
95          checkWrite();
96  
97          try {
98              ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
99              ObjectOutputStream out = new ObjectOutputStream(byteOut);
100 
101             out.writeObject(object);
102             out.flush();
103             _bytes = byteOut.toByteArray();
104             out.close();
105         } catch (IOException exception) {
106             MessageFormatException error = new MessageFormatException(
107                 exception.getMessage());
108             error.setLinkedException(exception);
109             throw error;
110         }
111     }
112 
113     /***
114      * Get the serializable object containing this message's data. The
115      * default value is null.
116      *
117      * @return the serializable object containing this message's data
118      *
119      * @throws MessageFormatException if object deserialization fails
120      */
121     public final Serializable getObject() throws MessageFormatException {
122         Serializable result = null;
123         if (_bytes != null) {
124             try {
125                 ByteArrayInputStream byteIn =
126                     new ByteArrayInputStream(_bytes);
127                 ObjectInputStream in = new ObjectInputStream(byteIn);
128 
129                 result = (Serializable) in.readObject();
130                 in.close();
131             } catch (IOException exception) {
132                 MessageFormatException error =
133                     new MessageFormatException(exception.getMessage());
134                 error.setLinkedException(exception);
135                 throw error;
136             } catch (ClassNotFoundException exception) {
137                 MessageFormatException error =
138                     new MessageFormatException(exception.getMessage());
139                 error.setLinkedException(exception);
140                 throw error;
141             }
142         }
143         return result;
144     }
145 
146     /***
147      * Clear out the message body. Clearing a message's body does not clear
148      * its header values or property entries.
149      * If this message body was read-only, calling this method leaves the
150      * message body is in the same state as an empty body in a newly created
151      * message
152      *
153      * @throws JMSException for any error
154      */
155     public final void clearBody() throws JMSException {
156         super.clearBody();
157         _bytes = null;
158     }
159 
160 }
This page was automatically generated by Maven