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 2001-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: MessageHandleFactory.java,v 1.12 2003/08/17 01:32:24 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * 3/1/2001     jima    Created
47   */
48  package org.exolab.jms.messagemgr;
49  
50  import java.sql.Connection;
51  
52  import javax.jms.DeliveryMode;
53  import javax.jms.JMSException;
54  import javax.jms.Message;
55  import javax.transaction.TransactionManager;
56  
57  import org.exolab.jms.client.JmsDestination;
58  import org.exolab.jms.client.JmsQueue;
59  import org.exolab.jms.message.MessageHandle;
60  import org.exolab.jms.message.MessageImpl;
61  import org.exolab.jms.persistence.DatabaseService;
62  import org.exolab.jms.persistence.PersistenceException;
63  
64  
65  /***
66   * This class defines a number of static methods that are responsible
67   * for creatimg message handles
68   *
69   * @version     $Revision: 1.12 $ $Date: 2003/08/17 01:32:24 $
70   * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
71   */
72  public class MessageHandleFactory {
73  
74      /***
75       * Retrieve a {@link TransientMessageHandle} given the specified message,
76       * irrespective of whether it is a persistent or non-persistent message.
77       *
78       * @param message the message
79       * @return the message handle
80       * @throws JMSException if the handle can't be created
81       */
82      public static MessageHandle getHandle(MessageImpl message)
83          throws JMSException {
84  
85          MessageHandle handle = null;
86  
87          if (message.isPersistent()) {
88              handle = new PersistentMessageHandle(message);
89          } else {
90              handle = new TransientMessageHandle(message);
91          }
92  
93          return handle;
94      }
95  
96      /***
97       * Retrieve a {@link org.exolab.jms.message.MessageHandle} from the 
98       * specified QueueConsumerEndpoint and message. It will create a 
99       * PersistentMessageHandle if the message is persistent or a 
100      * TransientMessageHandle otherwise.
101      *
102      * @param queue the queue destination cache
103      * @param message the message
104      * @return the message handle
105      * @throws JMSException if the handle can't be created
106      */
107     public static MessageHandle getHandle(QueueDestinationCache queue,
108                                           MessageImpl message)
109         throws JMSException {
110 
111         MessageHandle handle = null;
112         if (message.getJMSDeliveryMode() == DeliveryMode.PERSISTENT) {
113             PersistentMessageHandle phandle =
114                 new PersistentMessageHandle(message);
115             // set the consumer and destination identity
116             phandle.setConsumerName(queue.getDestination().getName());
117             phandle.setDestination(queue.getDestination());
118 
119             handle = phandle;
120         } else {
121             handle = new TransientMessageHandle(message);
122         }
123 
124         return handle;
125     }
126 
127     /***
128      * Use the message, destination and optionally, the consumer name to
129      * retrieve a message handle.
130      *
131      * @param destination  the message destination
132      * @param name the consumer name, can be null for JmsQueue type
133      * @param message the message
134      * @return the message handle
135      * @throws JMSException if the handle can't be created
136      */
137     public static MessageHandle getHandle(JmsDestination dest, String name,
138                                           MessageImpl message)
139         throws JMSException {
140 
141         MessageHandle handle = null;
142         if (message.getJMSDeliveryMode() == DeliveryMode.PERSISTENT) {
143             PersistentMessageHandle phandle =
144                 new PersistentMessageHandle(message);
145             // set the consumer and destination identity
146             phandle.setDestination(dest);
147             if (dest instanceof JmsQueue) {
148                 phandle.setConsumerName(dest.getName());
149             } else {
150                 phandle.setConsumerName(name);
151             }
152             handle = phandle;
153         } else {
154             handle = new TransientMessageHandle(message);
155         }
156         return handle;
157     }
158 
159     /***
160      * Retrieve a message handle for the specified {@link ConsumerEndpoint}
161      * and message.
162      *
163      * @param consumer the consumer endpoint
164      * @param message the message
165      * @return the message handle
166      * @throws JMSException if the handle can't be created
167      */
168     public static MessageHandle getHandle(ConsumerEndpoint consumer,
169                                           MessageImpl message)
170         throws JMSException {
171 
172         MessageHandle handle = null;
173         if (message.getJMSDeliveryMode() == DeliveryMode.PERSISTENT) {
174             if (consumer instanceof DurableConsumerEndpoint) {
175                 PersistentMessageHandle phandle =
176                     new PersistentMessageHandle(message);
177                 DurableConsumerEndpoint durable =
178                     (DurableConsumerEndpoint) consumer;
179 
180                 // set the consumer and destination identity
181                 phandle.setConsumerName(durable.getName());
182                 phandle.setDestination(
183                     (JmsDestination) message.getJMSDestination());
184 
185                 handle = phandle;
186             } else {
187                 handle = new TransientMessageHandle(message);
188             }
189         } else {
190             handle = new TransientMessageHandle(message);
191         }
192         return handle;
193     }
194 
195 
196     /***
197      * Create a transient handle for a message belonging to a queue.
198      *
199      * @param queue the queue destination cache
200      * @param message the message
201      * @return the message handle
202      * @throws JMSException if the handle can't be created
203      */
204     public static MessageHandle createHandle(QueueDestinationCache queue,
205                                              MessageImpl message)
206         throws JMSException {
207 
208         return new TransientMessageHandle(message);
209     }
210 
211     /***
212      * Create a transient message handle for a durable consumer
213      *
214      * @param durable the durable consumer endpoint
215      * @param message the message
216      * @return the message handle
217      * @throws JMSException if the handle can't be created
218      */
219     public static MessageHandle createHandle(DurableConsumerEndpoint durable,
220                                              MessageImpl message)
221         throws JMSException {
222 
223         return new TransientMessageHandle(message);
224     }
225 
226     /***
227      * Create a transient message handle.
228      *
229      * @param destination the message destination
230      * @param name the consumer name, can be null for JmsQueue type
231      * @param message the message
232      * @return the message handle
233      * @throws JMSException if the handle can't be created
234      */
235     public static MessageHandle createHandle(JmsDestination dest, String name,
236                                              MessageImpl message)
237         throws JMSException {
238 
239         return new TransientMessageHandle(message);
240     }
241 
242     /***
243      * Create a {@link PersistentMessageHandle} from the specified
244      * {@link TransientMessageHandle}. It does not fill in all the fields of a
245      * persistent message handle.
246      *
247      * @param handle  the transient message handle
248      * @return the persistent message handle
249      * @throws JMSException if the handle can't be created
250      */
251     public static MessageHandle createPersistentHandle(
252         TransientMessageHandle handle) throws JMSException {
253 
254         PersistentMessageHandle phandle = new PersistentMessageHandle();
255         phandle.setMessageId(handle.getMessageId());
256         phandle.setPriority(handle.getPriority());
257         phandle.setDelivered(handle.getDelivered());
258         phandle.setDestination(handle.getDestination());
259         phandle.setAcceptedTime(handle.getAcceptedTime());
260         phandle.setExpiryTime(handle.getExpiryTime());
261         phandle.setSequenceNumber(handle.getSequenceNumber());
262         phandle.setClientId(handle.getClientId());
263 
264         return phandle;
265     }
266 
267     /***
268      * Create a persistent message handle belonging to a queue.
269      *
270      * @param connection connection to the database
271      * @param queue queue destination cache
272      * @param message the message
273      * @return the message handle
274      * @throws JMSException if the handle can't be created
275      * @throws PersistenceException if the handle cannot be made persistent
276      */
277     public static MessageHandle createPersistentHandle(
278         Connection connection, QueueDestinationCache queue, 
279         MessageImpl message) throws JMSException, PersistenceException {
280 
281         PersistentMessageHandle handle = new PersistentMessageHandle(message);
282 
283         // fill in the persistent related particulars
284         handle.setConsumerName(queue.getDestination().getName());
285         handle.setDestination(queue.getDestination());
286 
287         // make the handle persistent
288         DatabaseService.getAdapter().addMessageHandle(connection, handle);
289 
290         return handle;
291     }
292 
293     /***
294      * Create a persistent handle for a message belonging to a particular
295      * durable consumer
296      *
297      * @param connection the connection to use.
298      * @param durable durable consumer endpoint
299      * @param message the message
300      * @return the message handle
301      * @throws JMSException if the handle can't be created
302      * @throws PersistenceException if the handle cannot be made persistent
303      */
304     public static MessageHandle createPersistentHandle(
305         Connection connection, DurableConsumerEndpoint durable, 
306         MessageImpl message) throws JMSException, PersistenceException {
307 
308         PersistentMessageHandle handle = new PersistentMessageHandle(message);
309 
310         // set the consumer and destination identity
311         handle.setConsumerName(durable.getName());
312         handle.setDestination(durable.getDestination());
313 
314         DatabaseService.getAdapter().addMessageHandle(connection, handle);
315 
316         return handle;
317     }
318 
319     /***
320      * Create a persistent message handle from the destination and consumer
321      * name
322      *
323      * @param connection the connection to use
324      * @param destination the message destination
325      * @param name the consumer name, can be null for JmsQueue type
326      * @param message the message
327      * @return the message handle
328      * @throws JMSException if the handle can't be created
329      * @throws PersistenceException if the handle cannot be made persistent
330      */
331     public static MessageHandle createPersistentHandle(Connection connection, 
332                                                        JmsDestination dest, 
333                                                        String name, 
334                                                        MessageImpl message) 
335         throws JMSException, PersistenceException {
336 
337         PersistentMessageHandle handle = new PersistentMessageHandle(message);
338 
339         // set the consumer and destination identity
340         handle.setDestination(dest);
341         if (dest instanceof JmsQueue) {
342             handle.setConsumerName(dest.getName());
343         } else {
344             handle.setConsumerName(name);
345         }
346 
347         DatabaseService.getAdapter().addMessageHandle(connection, handle);
348         return handle;
349     }
350 
351     /***
352      * Destroy the specified persistent handle.
353      *
354      * @param connection the connection to use
355      * @param handle the persistent handle to destroy
356      * @throws PersistenceExcetpion if there is a persistence-related error
357      */
358     public static void destroyPersistentHandle(Connection connection,
359                                                PersistentMessageHandle handle)
360         throws PersistenceException {
361         DatabaseService.getAdapter().removeMessageHandle(connection, handle);
362 
363         // notify the message manager
364         MessageMgr.instance().handleDestroyed(handle);
365     }
366 
367     /***
368      * Destroy a persistent handle associated with a destination, consumer
369      * name and message
370      *
371      * @param connection the connection to use
372      * @param destination the destination assoicated with the message
373      * @param name the name of the consumer
374      * @param message the message
375      * @throws JMSException if the handle can't be created
376      * @throws PersistentException if there is a persistence related error
377      */
378     public static void destroyPersistentHandle(Connection connection,
379                                                JmsDestination destination,
380                                                String name,
381                                                MessageImpl message)
382         throws JMSException, PersistenceException {
383 
384         if (message.isPersistent()) {
385             PersistentMessageHandle handle =
386                 (PersistentMessageHandle) getHandle(destination, name,
387                     message);
388             destroyPersistentHandle(connection, handle);
389         }
390     }
391 
392     /***
393      * Update the state of the persistent handle
394      *
395      * @param connection the connection to use
396      * @param handle the handle to update
397      * @throws PersistentException if there is a persistence related error
398      */
399     public static void updatePersistentHandle(Connection connection,
400                                               PersistentMessageHandle handle)
401         throws PersistenceException {
402         DatabaseService.getAdapter().updateMessageHandle(connection, handle);
403     }
404 
405 } //-- MessageHandleFactory
406