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-2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: PersistentMessage.java,v 1.9 2003/08/17 01:32:25 tanderson Exp $
44   *
45   * Date         Author  Changes
46   */
47  
48  package org.exolab.jms.persistence;
49  
50  import java.io.ByteArrayInputStream;
51  import java.io.ByteArrayOutputStream;
52  import java.io.IOException;
53  import java.io.ObjectInput;
54  import java.io.ObjectInputStream;
55  import java.io.ObjectOutput;
56  import java.io.ObjectOutputStream;
57  
58  import javax.jms.JMSException;
59  
60  import org.exolab.core.foundation.PersistentObject;
61  import org.exolab.jms.message.DestinationImpl;
62  import org.exolab.jms.message.MessageImpl;
63  
64  
65  /***
66   * A wrapper to the Message Object to assist in persistency.
67   *
68   *
69   * @version     $Revision: 1.9 $ $Date: 2003/08/17 01:32:25 $
70   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
71   */
72  public class PersistentMessage extends PersistentObject {
73  
74      // Used for serialization
75      static final long serialVersionUID = 2;
76  
77      // The message destination
78      private String destination_;
79  
80      // The message id.
81      private String messageId_;
82  
83      // The message type, i.e. BytesMessage, TextMessage, StreamMessage etc.
84      private String messageType_;
85  
86      // The message priority.
87      private int priority_;
88  
89      // The timestamp when the message was received.
90      private long timeStamp_;
91  
92      // The message expiry time
93      private long expiryTime_;
94  
95      // The message holder.
96      private MessageImpl message_;
97  
98      // Indicates whether the message has been processed by the provider
99      private boolean processed_;
100 
101 
102     /***
103      * Default constructor.
104      *
105      * @param MessageImpl	the message to persist
106      *
107      */
108     public PersistentMessage() {
109         super();
110     }
111 
112 
113     /***
114      * Store the message to be persisted. And extract any required column
115      * details.
116      *
117      * @param MessageImpl	the message to persist
118      *
119      */
120     public PersistentMessage(MessageImpl message) throws JMSException {
121         super();
122         message_ = message;
123         destination_ =
124             ((DestinationImpl) message.getJMSDestination()).getDestination();
125         messageId_ = message.getMessageId().getId();
126         messageType_ = message.getJMSType();
127         priority_ = message.getJMSPriority();
128         expiryTime_ = message.getJMSExpiration();
129         timeStamp_ = message.getAcceptedTime();
130         processed_ = message.getProcessed();
131     }
132 
133     /***
134      * Set the message held by this persistent object
135      *
136      * @param message - the message held
137      */
138     public void setMessage(MessageImpl message) {
139         message_ = message;
140     }
141 
142     /***
143      * return the held message from the persistent object.
144      *
145      * @return MessageImpl the held message.
146      *
147      */
148     public MessageImpl getMessage() {
149         return message_;
150     }
151 
152 
153     /***
154      * Get the message as a serialized blob.
155      *
156      * @return String The serialized message.
157      *
158      */
159     public byte[] getMessageBlob() {
160         try {
161             ByteArrayOutputStream bout = new ByteArrayOutputStream();
162             ObjectOutputStream tmp = new ObjectOutputStream(bout);
163             tmp.writeObject(message_);
164             tmp.close();
165             return bout.toByteArray();
166         } catch (Exception err) {
167             System.err.println
168                 ("Error PersistentMessage: Failed to serialize message\n"
169                 + err);
170             return null;
171         }
172     }
173 
174 
175     /***
176      * Set the message from a serialized blob
177      *
178      * @param blob The serialized message.
179      *
180      */
181     public void setMessageBlob(byte[] blob) {
182         try {
183             if (blob != null) {
184                 ByteArrayInputStream bin =
185                     new ByteArrayInputStream(blob);
186                 ObjectInputStream tmpin = new ObjectInputStream(bin);
187                 message_ = (MessageImpl) tmpin.readObject();
188                 tmpin.close();
189             } else {
190                 message_ = null;
191             }
192         } catch (Exception err) {
193             System.err.println
194                 ("Error PersistentMessage: Failed to de-serialize message\n"
195                 + err);
196             err.printStackTrace();
197         }
198     }
199 
200     /***
201      * Get the message destination
202      *
203      * @return String The destination of the message
204      *
205      */
206     public String getDestination() {
207         return destination_;
208     }
209 
210     /***
211      * Set the message destination
212      *
213      * @param destination The destination of the message
214      *
215      */
216     public void setDestination(String destination) {
217         destination_ = destination;
218     }
219 
220     /***
221      * Get the message id
222      *
223      * @return the message id
224      *
225      */
226     public String getMessageId() {
227         return messageId_;
228     }
229 
230     /***
231      * Set the message id
232      *
233      * @param id The message id
234      *
235      */
236     public void setMessageId(String id) {
237         messageId_ = id;
238     }
239 
240     /***
241      * Get the message type
242      *
243      * @return String The message type
244      *
245      */
246     public String getMessageType() {
247         return messageType_;
248     }
249 
250     /***
251      * Set the message type
252      *
253      * @param messageType The message type
254      *
255      */
256     public void setMessageType(String messageType) {
257         messageType_ = messageType;
258     }
259 
260     /***
261      * Get the message priority
262      *
263      * @return int The priority
264      *
265      */
266     public int getPriority() {
267         return priority_;
268     }
269 
270     /***
271      * Set the message priority
272      *
273      * @param priority The message priority to set
274      *
275      */
276     public void setPriority(int priority) {
277         priority_ = priority;
278     }
279 
280     /***
281      * Get the time the message was received by the MessageMgr
282      *
283      * @return long The message creation time in secs since epoc
284      *
285      */
286     public long getJMSTimeStamp() {
287         return timeStamp_;
288     }
289 
290     /***
291      * Set the time the message was received by the MessageMgr
292      *
293      * @param timeStamp The message creation time in secs since epoc
294      *
295      */
296     public void setJMSTimeStamp(long time) {
297         timeStamp_ = time;
298     }
299 
300     /***
301      * Retrieve the message expiry time
302      *
303      * @return long - the time that the underlying message expires
304      *
305      */
306     public long getExpiryTime() {
307         return expiryTime_;
308     }
309 
310     /***
311      * Set the message expiry time.
312      *
313      * @param time - message expiry time in milliseconds
314      *
315      */
316     public void setExpiryTime(long time) {
317         expiryTime_ = time;
318     }
319 
320     /***
321      * Set the state of the processed flag
322      *
323      * @param value - true if message has been processed
324      */
325     public void setProcessed(boolean value) {
326         processed_ = value;
327     }
328 
329     /***
330      * Return the state of the processed flag
331      *
332      * @return boolean - true if the message has been processed
333      */
334     public boolean getProcessed() {
335         return processed_;
336     }
337 
338     // implementation of Externalizable.writeExternal
339     public void writeExternal(ObjectOutput stream) throws IOException {
340         stream.writeLong(serialVersionUID);
341         stream.writeObject(destination_);
342         stream.writeUTF(messageId_);
343         stream.writeObject(messageType_);
344         stream.writeInt(priority_);
345         stream.writeLong(timeStamp_);
346         stream.writeLong(expiryTime_);
347         stream.writeBoolean(processed_);
348         stream.writeObject(message_);
349         super.writeExternal(stream);
350     }
351 
352     // implementation of Externalizable.writeExternal
353     public void readExternal(ObjectInput stream)
354         throws IOException, ClassNotFoundException {
355         long version = stream.readLong();
356         if (version == serialVersionUID) {
357             destination_ = (String) stream.readObject();
358             messageId_ = stream.readUTF();
359             messageType_ = (String) stream.readObject();
360             priority_ = stream.readInt();
361             timeStamp_ = stream.readLong();
362             expiryTime_ = stream.readLong();
363             processed_ = stream.readBoolean();
364             message_ = (MessageImpl) stream.readObject();
365         } else {
366             throw new IOException("PersistentMessage with version "
367                 + version + " is not supported.");
368         }
369         super.readExternal(stream);
370 
371     }
372 } //-- PersistentMessage
373