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