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: MessageHeader.java,v 1.18 2004/01/01 13:45:03 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * 02/26/2000   jimm    Created
47   */
48  
49  package org.exolab.jms.message;
50  
51  import java.io.Externalizable;
52  import java.io.IOException;
53  import java.io.ObjectInput;
54  import java.io.ObjectOutput;
55  
56  import javax.jms.DeliveryMode;
57  import javax.jms.Destination;
58  import javax.jms.JMSException;
59  
60  
61  /***
62   * This class implements message header fields for messages.
63   *
64   * @version     $Revision: 1.18 $ $Date: 2004/01/01 13:45:03 $
65   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
66   * @see         javax.jms.Message
67   */
68  class MessageHeader implements Externalizable, Cloneable {
69  
70      static final long serialVersionUID = 1;
71  
72      private DestinationImpl _replyTo = null;
73      private Timestamp _timestamp = null;
74      private CorrelationId _correlationId = null;
75      private boolean _redelivered = false;
76      private long _expiration = 0;
77      private Priority _priority = null;
78      private Type _type = null;
79      private DestinationImpl _destination = null;
80      private DeliveryModeImpl _mode = null;
81  
82  
83      /***
84       * A message identity the uniquely identifies the message.
85       * This is assigned when the message is sent.
86       */
87      private MessageId _id = null;
88  
89      /***
90       * The identity of the message when it was received, used for message 
91       * acknowledgement. This is required as <code>_id</code> changes when
92       * a message is republished, but the original identifier of the message
93       * must be used to acknowledge the message.
94       */
95      private transient String _ackId;
96  
97      /***
98       * if this message is being delivered as a result of a wildcard, this
99       * will contain the original wildcard name that the consumer subscribed
100      * with.
101      */
102     private String _wildcard = null;
103 
104     /***
105      * The unique id of the consumer endpoint that sent this message. This is
106      * used for sending back acks etc.
107      */
108     private String _consumerId = null;
109 
110     /***
111      * The session allocated identifer of the consumer
112      */
113     private long _clientid;
114 
115 
116     public MessageHeader() {
117     }
118 
119     /***
120      * Clone an instance of this object.
121      *
122      * @return a copy of this object
123      * @exception CloneNotSupportedException if object or attributes
124      * not cloneable
125      */
126     public Object clone() throws CloneNotSupportedException {
127         MessageHeader result = (MessageHeader) super.clone();
128         result._replyTo = _replyTo;
129         result._timestamp = _timestamp;
130         result._correlationId = _correlationId;
131         result._priority = _priority;
132         result._type = _type;
133         result._destination = _destination;
134         result._mode = _mode;
135         result._id = _id;
136         result._ackId = _ackId;
137         result._wildcard = (_wildcard == null ? null : _wildcard);
138         result._consumerId = _consumerId;
139         result._clientid = _clientid;
140         return result;
141     }
142 
143 
144     // Write external interfaces called via serialisation.
145     public void writeExternal(ObjectOutput out) throws IOException {
146         out.writeLong(serialVersionUID);
147         out.writeObject(_replyTo);
148         out.writeObject(_timestamp);
149         out.writeObject(_correlationId);
150         out.writeBoolean(_redelivered);
151         out.writeLong(_expiration);
152         out.writeObject(_priority);
153         out.writeObject(_type);
154         out.writeObject(_destination);
155         out.writeObject(_mode);
156         out.writeObject(_id);
157         out.writeObject(_wildcard);
158         out.writeObject(_consumerId);
159         out.writeLong(_clientid);
160     }
161 
162     public void readExternal(ObjectInput in)
163         throws IOException, ClassNotFoundException {
164         long version = in.readLong();
165         if (version == serialVersionUID) {
166             _replyTo = (DestinationImpl) in.readObject();
167             _timestamp = (Timestamp) in.readObject();
168             _correlationId = (CorrelationId) in.readObject();
169             _redelivered = in.readBoolean();
170             _expiration = in.readLong();
171             _priority = (Priority) in.readObject();
172             _type = (Type) in.readObject();
173             _destination = (DestinationImpl) in.readObject();
174             _mode = (DeliveryModeImpl) in.readObject();
175             _id = (MessageId) in.readObject();
176             _wildcard = (String) in.readObject();
177             _consumerId = (String) in.readObject();
178             _clientid = in.readLong();
179         } else {
180             throw new IOException("Incorrect version enountered: " +
181                 version + " This version = " +
182                 serialVersionUID);
183         }
184     }
185 
186     Destination getJMSReplyTo() throws JMSException {
187         return _replyTo;
188     }
189 
190     public void setJMSReplyTo(Destination replyTo) throws JMSException {
191         if (replyTo instanceof DestinationImpl) {
192             _replyTo = (DestinationImpl) replyTo;
193         } else {
194             throw new JMSException("Unknown Destination Type");
195         }
196     }
197 
198     void setJMSDestination(Destination destination) throws JMSException {
199         if (destination instanceof DestinationImpl) {
200             _destination = (DestinationImpl) destination;
201         } else {
202             throw new JMSException("Unknown Destination Type");
203         }
204     }
205 
206     public Destination getJMSDestination() throws JMSException {
207         return _destination;
208     }
209 
210     public void setJMSMessageID(String id) throws JMSException {
211         if (id != null) {
212             if (!id.startsWith(MessageId.PREFIX)) {
213                 throw new JMSException("Invalid JMSMessageID: " + id);
214             }
215             _id = new MessageId(id);
216         } else {
217             _id = null;
218         }
219     }
220 
221     public String getJMSMessageID() throws JMSException {
222         if (_id != null) {
223             return _id.toString();
224         } else {
225             Thread.currentThread().dumpStack();
226             throw new JMSException("No Internal Id set");
227         }
228     }
229 
230     /***
231      * Sets the identifier of the message for acknowledgement.
232      * This will typically be the same as that returned by 
233      * {@link #getJMSMessageID}, unless the message was republished after
234      * its receipt. If the message is republished, this method will return
235      * the original message identifier, whereas {@link #getJMSMessageID} will
236      * return that of the last publication.
237      *
238      * @param ackId the identifier of the message for acknowledgement
239      */
240     public void setAckMessageID(String id) {
241         _ackId = id;
242     }
243 
244     /***
245      * Returns the identifier of the message for acknowledgment.
246      *
247      * @return the identifier of the message for acknowledgment
248      */
249     public String getAckMessageID() {
250         return _ackId;
251     }
252 
253     public void setJMSTimestamp(long timestamp) throws JMSException {
254         _timestamp = new Timestamp(timestamp);
255     }
256 
257     public long getJMSTimestamp() throws JMSException {
258         if (_timestamp != null) {
259             return _timestamp.toLong();
260         } else {
261             throw new JMSException("No Timestamp set");
262         }
263     }
264 
265     public void setJMSCorrelationIDAsBytes(byte[] correlationID)
266         throws JMSException {
267         _correlationId = new CorrelationId(correlationID);
268     }
269 
270     public byte[] getJMSCorrelationIDAsBytes() throws JMSException {
271         return (_correlationId != null ? _correlationId.getBytes() : null);
272     }
273 
274     public void setJMSCorrelationID(String correlationID) throws JMSException {
275         if (correlationID != null) {
276             _correlationId = new CorrelationId(correlationID);
277         } else {
278             _correlationId = null;
279         }
280     }
281 
282     public String getJMSCorrelationID() throws JMSException {
283         return (_correlationId != null ? _correlationId.getString() : null);
284     }
285 
286     public void setJMSDeliveryMode(int mode) throws JMSException {
287         _mode = new DeliveryModeImpl(mode);
288     }
289 
290     public int getJMSDeliveryMode() throws JMSException {
291         if (_mode != null) {
292             return _mode.getDeliveryMode();
293         } else {
294             throw new JMSException("No Delivery Mode set");
295         }
296     }
297 
298     public boolean getJMSRedelivered() throws JMSException {
299         return _redelivered;
300     }
301 
302     public void setJMSRedelivered(boolean redelivered) throws JMSException {
303         _redelivered = redelivered;
304     }
305 
306     public void setJMSType(String type) throws JMSException {
307         if (type != null) {
308             _type = new Type(type);
309         } else {
310             _type = null;
311         }
312     }
313 
314     public String getJMSType() throws JMSException {
315         return (_type != null) ? _type.getType() : null;
316     }
317 
318     public void setJMSExpiration(long expiration) throws JMSException {
319         _expiration = expiration;
320     }
321 
322     public long getJMSExpiration() throws JMSException {
323         return _expiration;
324     }
325 
326     public void setJMSPriority(int priority) throws JMSException {
327         _priority = new Priority(priority);
328     }
329 
330     public int getJMSPriority() throws JMSException {
331         if (_priority != null) {
332             return _priority.getPriority();
333         } else {
334             return 0;
335         }
336     }
337 
338     /***
339      * Get the value of consumerId.
340      * @return value of consumerId.
341      */
342     public String getConsumerId() {
343         return _consumerId;
344     }
345 
346     /***
347      * Set the value of consumerId.
348      * @param v  Value to assign to consumerId.
349      */
350     public void setConsumerId(String v) {
351         _consumerId = v;
352     }
353 
354     /***
355      * Get the value of the client session consumer identifier
356      *
357      * @return the value of the client session consumer identifier
358      */
359     public long getClientId() {
360         return _clientid;
361     }
362 
363     /***
364      * Set the value of the client session consumer identifer
365      *
366      * @param clientId the client session consumer identifier
367      */
368     public void setClientId(long clientId) {
369         _clientid = clientId;
370     }
371 
372 
373     /***
374      * Return the message id for the object
375      *
376      * @return MessageId
377      */
378     public MessageId getMessageId() {
379         return _id;
380     }
381 
382     /***
383      * Return the wildcard value if there is one.
384      *
385      * @return String The wildcard string
386      */
387     public String getWildcard() {
388         return _wildcard;
389     }
390 
391     /***
392      * Set the wildcard string.
393      *
394      * @param wildcard The wildcard.
395      */
396     public void setWildcard(String wildcard) {
397         _wildcard = wildcard;
398     }
399 
400     /***
401      * Checks whether the message is persistent.
402      *
403      * @return boolean - true if it is
404      */
405     public boolean isPersistent() {
406         if (_mode != null) {
407             return _mode.isPersistent();
408         } else {
409             return false;
410         }
411     }
412 
413     //Expedited or normal
414     public boolean isPriorityExpedited() throws JMSException {
415         if (_priority != null) {
416             return _priority.isExpedited();
417         } else {
418             return false;
419         }
420     }
421 
422 } // End MessageHeader