KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > messagemgr > AbstractMessageHandle


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 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: AbstractMessageHandle.java,v 1.1 2005/03/18 03:58:38 tanderson Exp $
44  */

45 package org.exolab.jms.messagemgr;
46
47 import java.sql.Connection JavaDoc;
48 import javax.jms.JMSException JavaDoc;
49 import javax.jms.MessageConsumer JavaDoc;
50
51 import org.exolab.jms.client.JmsDestination;
52 import org.exolab.jms.message.MessageImpl;
53 import org.exolab.jms.persistence.PersistenceException;
54 import org.exolab.jms.server.ServerConnection;
55
56
57 /**
58  * Abstract implementation of the {@link MessageHandle} interface
59  *
60  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
61  * @version $Revision: 1.1 $ $Date: 2005/03/18 03:58:38 $
62  */

63 public abstract class AbstractMessageHandle implements MessageHandle {
64
65     /**
66      * The message reference.
67      */

68     private MessageRef _reference;
69
70     /**
71      * The message identifier.
72      */

73     private final String JavaDoc _messageId;
74
75     /**
76      * If <code>true</code>, indicates that the message associated with the
77      * handle has been delivered, but not acknowledged.
78      */

79     private boolean _delivered = false;
80
81     /**
82      * The message priority.
83      */

84     private final int _priority;
85
86     /**
87      * The time that the message was accepted by the server, in milliseconds.
88      */

89     private long _acceptedTime;
90
91     /**
92      * The message sequence number, assigned by the {@link MessageMgr}, used to
93      * help order the message. It also allows us to overcome the millisecond
94      * resolution problem of _acceptedTime, when ordering messages
95      */

96     private final long _sequenceNumber;
97
98     /**
99      * The time that the message expires, in milliseconds.
100      */

101     private long _expiryTime;
102
103     /**
104      * The destination that this handle belongs to.
105      */

106     private final JmsDestination _destination;
107
108     /**
109      * The identity of the {@link ConsumerEndpoint} associated with the message,
110      * or <code>-1</code> if it isn't associated with any consumer.
111      */

112     private final long _consumerId;
113
114     /**
115      * The identity of the {@link ServerConnection} associated with the message,
116      * or <code>-1</code> if it isn't associated with any connection.
117      */

118     private final long _connectionId;
119
120
121     /**
122      * Construct a new <code>AbstractMessageHandle</code>.
123      *
124      * @param reference the reference to the message
125      * @param message the message for which the handle is created
126      * @throws JMSException if the handle cannot be constructed
127      */

128     public AbstractMessageHandle(MessageRef reference, MessageImpl message)
129             throws JMSException JavaDoc {
130         if (reference == null) {
131             throw new IllegalArgumentException JavaDoc("Argument 'reference' is null");
132         }
133         if (message == null) {
134             throw new IllegalArgumentException JavaDoc("Argument 'message' is null");
135         }
136         _messageId = message.getMessageId().getId();
137         _delivered = message.getJMSRedelivered();
138         _priority = message.getJMSPriority();
139         _acceptedTime = message.getAcceptedTime();
140         _sequenceNumber = message.getSequenceNumber();
141         _expiryTime = message.getJMSExpiration();
142         _destination = (JmsDestination) message.getJMSDestination();
143         _consumerId = message.getConsumerId();
144         _connectionId = message.getConnectionId();
145         _reference = reference;
146     }
147
148     /**
149      * Construct a new <code>AbstractMessageHandle</code>.
150      *
151      * @param messageId the message identifier
152      * @param priority the message priority
153      * @param acceptedTime the time the message was accepted by the server
154      * @param sequenceNumber the message sequence number
155      * @param expiryTime the time that the message will expire
156      */

157     public AbstractMessageHandle(String JavaDoc messageId, int priority,
158                                  long acceptedTime, long sequenceNumber,
159                                  long expiryTime, JmsDestination destination) {
160         if (messageId == null) {
161             throw new IllegalArgumentException JavaDoc("Argument 'messageId' is null");
162         }
163         if (destination == null) {
164             throw new IllegalArgumentException JavaDoc(
165                     "Argument 'destination' is null");
166         }
167         _messageId = messageId;
168         _priority = priority;
169         _acceptedTime = acceptedTime;
170         _sequenceNumber = sequenceNumber;
171         _expiryTime = expiryTime;
172         _destination = destination;
173         _consumerId = -1;
174         _connectionId = -1;
175     }
176
177     /**
178      * Returns the message identifier.
179      *
180      * @return the message identifier
181      */

182     public String JavaDoc getMessageId() {
183         return _messageId;
184     }
185
186     /**
187      * Returns the message associated with this handle.
188      *
189      * @return the associated message, or <code>null</code> if the handle is no
190      * longer valid
191      * @throws JMSException for any error
192      */

193     public MessageImpl getMessage() throws JMSException JavaDoc {
194         if (_reference == null) {
195             throw new JMSException JavaDoc("Cannot get message with identifier="
196                                    + _messageId + ": MessageRef null");
197         }
198         return _reference.getMessage();
199     }
200
201
202     /**
203      * Indicates if a message has been delivered to a {@link MessageConsumer},
204      * but not acknowledged.
205      *
206      * @param delivered if <code>true</code> indicates that an attempt has been
207      * made to deliver the message
208      */

209     public void setDelivered(boolean delivered) {
210         _delivered = delivered;
211     }
212
213     /**
214      * Returns if an attempt has already been made to deliver the message.
215      *
216      * @return <code>true</code> if delivery has been attempted
217      */

218     public boolean getDelivered() {
219         return _delivered;
220     }
221
222     /**
223      * Returns the priority of the message.
224      *
225      * @return the message priority
226      */

227     public int getPriority() {
228         return _priority;
229     }
230
231     /**
232      * Returns the time that the corresponding message was accepted, in
233      * milliseconds.
234      *
235      * @return the time that the corresponding message was accepted
236      */

237     public long getAcceptedTime() {
238         return _acceptedTime;
239     }
240
241     /**
242      * Returns the time that the message expires.
243      *
244      * @return the expiry time
245      */

246     public long getExpiryTime() {
247         return _expiryTime;
248     }
249
250     /**
251      * Determines if the message has expired.
252      *
253      * @return <code>true</code> if the message has expired, otherwise
254      * <code>false</code>
255      */

256     public boolean hasExpired() {
257         return (_expiryTime != 0 && _expiryTime <= System.currentTimeMillis());
258     }
259
260     /**
261      * Returns the handle's sequence number.
262      *
263      * @return the sequence number
264      */

265     public long getSequenceNumber() {
266         return _sequenceNumber;
267     }
268
269     /**
270      * Returns the message destination.
271      *
272      * @return the message destination
273      */

274     public JmsDestination getDestination() {
275         return _destination;
276     }
277
278     /**
279      * Returns the consumer identity associated with the message.
280      *
281      * @return the consumer identity associated with the message, or *
282      * <code>-1</code> if the message isn't associated with a consumer
283      */

284     public long getConsumerId() {
285         return _consumerId;
286     }
287
288     /**
289      * Returns the persistent identity of the the consumer endpoint that owns
290      * this handle. If it is set, then a consumer owns it exclusively, otherwise
291      * the handle may be shared across a number of consumers.
292      *
293      * @return <code>null</code>
294      */

295     public String JavaDoc getConsumerPersistentId() {
296         return null;
297     }
298
299     /**
300      * Returns the connection identity associated with this handle.
301      *
302      * @return the connection identity associated with this handle, or
303      * <code>-1</code> if this isn't associated with a connection
304      */

305     public long getConnectionId() {
306         return _connectionId;
307     }
308
309     /**
310      * Determines if the handle is persistent.
311      *
312      * @return <code>false</code>
313      */

314     public boolean isPersistent() {
315         return false;
316     }
317
318     /**
319      * Indicates whether some other object is "equal to" this one.
320      *
321      * @param object the reference object with which to compare.
322      * @return <code>true</code> if <code>object</code> is a MessageHandle, and
323      * has the same {@link #getMessageId()
324      */

325     public boolean equals(Object JavaDoc object) {
326         boolean result = (object instanceof MessageHandle);
327         if (result) {
328             result = _messageId.equals(((MessageHandle) object).getMessageId());
329         }
330         return result;
331     }
332
333     /**
334      * Returns a hash code value for the object.
335      *
336      * @return a hash code value for this object
337      */

338     public int hashCode() {
339         return _messageId.hashCode();
340     }
341
342     /**
343      * Return a stringified version of the handle.
344      *
345      * @return a stringified version of the handle
346      */

347     public String JavaDoc toString() {
348         return "MessageHandle : " + _priority + ":" + getAcceptedTime() +
349                 ":" + getSequenceNumber() + ":" + _messageId;
350     }
351
352     /**
353      * Destroy this handle. If this is the last handle to reference the message,
354      * also destroys the message.
355      *
356      * @throws JMSException for any error
357      */

358     public void destroy() throws JMSException JavaDoc {
359         getMessageRef().dereference();
360     }
361
362     /**
363      * Destroy this handle. If this is the last handle to reference the message,
364      * also destroys the message.
365      *
366      * @param connection the connection to use
367      * @throws JMSException for any error
368      * @throws PersistenceException for any persistence error
369      */

370     public void destroy(Connection JavaDoc connection) throws JMSException JavaDoc,
371             PersistenceException {
372         getMessageRef().dereference();
373     }
374
375     /**
376      * Returns the message reference.
377      *
378      * @return the message reference, or <code>null</code> if none has been set
379      */

380     public MessageRef getMessageRef() {
381         return _reference;
382     }
383
384     /**
385      * Sets the message reference.
386      *
387      * @param reference the reference to the message
388      */

389     protected void setMessageRef(MessageRef reference) {
390         _reference = reference;
391     }
392
393 }
394
Popular Tags