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 tma@netspace.net.au.
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-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: SessionHelper.java,v 1.2 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import javax.jms.Destination;
48 import javax.jms.JMSException;
49 import javax.jms.MessageConsumer;
50 import javax.jms.MessageProducer;
51 import javax.jms.Queue;
52 import javax.jms.QueueSender;
53 import javax.jms.QueueSession;
54 import javax.jms.Session;
55 import javax.jms.Topic;
56 import javax.jms.TopicSession;
57 import javax.jms.TopicPublisher;
58 import javax.jms.XAQueueSession;
59 import javax.jms.XATopicSession;
60
61
62 /***
63 * Helper class for performing session operations
64 *
65 * @version $Revision: 1.2 $ $Date: 2004/01/31 13:44:24 $
66 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
67 */
68 public final class SessionHelper {
69
70 /***
71 * Seed used to uniquely identify subscribers
72 */
73 private static int _subscriberSeed = 0;
74
75
76 /***
77 * Prevent construction of utility class
78 */
79 private SessionHelper() {
80 }
81
82 /***
83 * Create a message consumer for the supplied session and destination.
84 * The name argument only applies to topic destinations.
85 *
86 * @param session the session
87 * @param destination the destination to consume messages from
88 * @param name if non-null, and the session is a TopicSession, then a
89 * durable subscriber will be returned, otherwise the argument is ignored.
90 * @return a new consumer
91 * @throws JMSException if any of the JMS operations fail
92 */
93 public static MessageConsumer createConsumer(
94 Session session, Destination destination, String name)
95 throws JMSException {
96
97 MessageConsumer result = null;
98 if (session instanceof XAQueueSession) {
99 session = ((XAQueueSession) session).getQueueSession();
100 } else if (session instanceof XATopicSession) {
101 session = ((XATopicSession) session).getTopicSession();
102 }
103
104 if (session instanceof QueueSession) {
105 Queue queue = (Queue) destination;
106 result = ((QueueSession) session).createReceiver(queue);
107 } else {
108 Topic topic = (Topic) destination;
109 if (name != null) {
110 result = ((TopicSession) session).createDurableSubscriber(
111 topic, name);
112 } else {
113 result = ((TopicSession) session).createSubscriber(topic);
114 }
115 }
116 return result;
117 }
118
119 /***
120 * Create a message consumer for the supplied session and destination and
121 * selector.
122 * The name argument only applies to topic destinations.
123 *
124 * @param session the session
125 * @param destination the destination to consume messages from
126 * @param name if non-null, and the session is a TopicSession, then a
127 * durable subscriber will be returned, otherwise the argument is ignored.
128 * @param selector the message selector
129 * @param noLocal if the session is a TopicSession, then the subscriber
130 * will be created with the supplied noLocal flag, otherwise the argument
131 * is ignored.
132 * @return a new consumer
133 * @throws JMSException if any of the JMS operations fail
134 */
135 public static MessageConsumer createConsumer(
136 Session session, Destination destination, String name,
137 String selector, boolean noLocal) throws JMSException {
138
139 MessageConsumer result = null;
140 if (session instanceof XAQueueSession) {
141 session = ((XAQueueSession) session).getQueueSession();
142 } else if (session instanceof XATopicSession) {
143 session = ((XATopicSession) session).getTopicSession();
144 }
145
146 if (session instanceof QueueSession) {
147 Queue queue = (Queue) destination;
148 result = ((QueueSession) session).createReceiver(queue, selector);
149 } else {
150 Topic topic = (Topic) destination;
151 if (name != null) {
152 result = ((TopicSession) session).createDurableSubscriber(
153 topic, name, selector, noLocal);
154 } else {
155 result = ((TopicSession) session).createSubscriber(
156 topic, selector, noLocal);
157 }
158 }
159 return result;
160 }
161
162 /***
163 * Create a message producer for the supplied test session and destination
164 *
165 * @param session the test session
166 * @param destination the destination to send messages to
167 * @return a new producer
168 * @throws JMSException if any of the JMS operations fail
169 */
170 public static MessageProducer createProducer(Session session,
171 Destination destination)
172 throws JMSException {
173 MessageProducer result = null;
174 if (session instanceof XAQueueSession) {
175 session = ((XAQueueSession) session).getQueueSession();
176 } else if (session instanceof XATopicSession) {
177 session = ((XATopicSession) session).getTopicSession();
178 }
179
180 if (session instanceof QueueSession) {
181 Queue queue = (Queue) destination;
182 result = ((QueueSession) session).createSender(queue);
183 } else {
184 Topic topic = (Topic) destination;
185 result = ((TopicSession) session).createPublisher(topic);
186 }
187 return result;
188 }
189
190 /***
191 * Create a message consumer for the supplied test context and destination
192 *
193 * @param context the test context
194 * @param destination the destination to consume messages from
195 * @param name if non-null, and the session is a TopicSession, then a
196 * durable subscriber will be returned, otherwise the argument is ignored.
197 * @return a new consumer
198 * @throws JMSException if any of the JMS operations fail
199 */
200 public static MessageConsumer createConsumer(TestContext context,
201 Destination destination,
202 String name)
203 throws JMSException {
204
205 return createConsumer(context.getSession(), destination, name);
206 }
207
208 /***
209 * Create a message producer for the supplied test context and destination
210 *
211 * @param context the test context
212 * @param destination the destination to send messages to
213 * @return a new producer
214 * @throws JMSException if any of the JMS operations fail
215 */
216 public static MessageProducer createProducer(TestContext context,
217 Destination destination)
218 throws JMSException {
219
220 return createProducer(context.getSession(), destination);
221 }
222
223 /***
224 * Create a {@link MessageReceiver} for the supplied session, destination,
225 * and messaging behaviour
226 *
227 * @param session the session
228 * @param destination the destination to consume messages from
229 * @param behaviour the messaging behaviour
230 * @return a new message receiver
231 * @throws JMSException if any of the JMS operations fail
232 */
233 public static MessageReceiver createReceiver(Session session,
234 Destination destination,
235 MessagingBehaviour behaviour)
236 throws JMSException {
237
238 MessageReceiver result = null;
239
240 if (behaviour.getReceiptType().equals(ReceiptType.BROWSER)) {
241 result = new MessageBrowser((QueueSession) session,
242 (Queue) destination);
243 } else {
244 String name = null;
245 if (behaviour.getDurable()) {
246 name = getSubscriberName();
247 }
248 MessageConsumer consumer =
249 createConsumer(session, destination, name);
250 if (behaviour.getReceiptType().equals(ReceiptType.SYNCHRONOUS)) {
251 result = new SynchronousReceiver(session, consumer, name);
252 } else {
253 result = new AsynchronousReceiver(session, consumer, name);
254 }
255 }
256 return result;
257 }
258
259 /***
260 * Create a {@link MessageReceiver} for the supplied session, destination,
261 * messaging behaviour, and selector
262 *
263 * @param session the session
264 * @param destination the destination to consume messages from
265 * @param behaviour the messaging behaviour
266 * @param selector the message selector
267 * @param noLocal if the session is a TopicSession, then the subscriber
268 * will be created with the supplied noLocal flag, otherwise the argument
269 * is ignored.
270 * @return a new message receiver
271 * @throws JMSException if any of the JMS operations fail
272 */
273 public static MessageReceiver createReceiver(Session session,
274 Destination destination,
275 MessagingBehaviour behaviour,
276 String selector,
277 boolean noLocal)
278 throws JMSException {
279 return createReceiver(session, destination, behaviour, null, selector,
280 noLocal);
281 }
282
283 /***
284 * Create a {@link MessageReceiver} for the supplied session, destination,
285 * messaging behaviour, and selector
286 *
287 * @param session the session
288 * @param destination the destination to consume messages from
289 * @param behaviour the messaging behaviour
290 * @param name if non-null, and the session is a TopicSession, then a
291 * durable subscriber will be returned, otherwise the argument is ignored.
292 * If null, and the session is a TopicSession and the behaviour is for
293 * durable subscribers, then a name will be automatically allocated.
294 * @param selector the message selector
295 * @param noLocal if the session is a TopicSession, then the subscriber
296 * will be created with the supplied noLocal flag, otherwise the argument
297 * is ignored.
298 * @return a new message receiver
299 * @throws JMSException if any of the JMS operations fail
300 */
301 public static MessageReceiver createReceiver(Session session,
302 Destination destination,
303 MessagingBehaviour behaviour,
304 String name,
305 String selector,
306 boolean noLocal)
307 throws JMSException {
308
309 MessageReceiver result = null;
310
311 if (behaviour.getReceiptType().equals(ReceiptType.BROWSER)) {
312 result = new MessageBrowser((QueueSession) session,
313 (Queue) destination, selector);
314 } else {
315 if (behaviour.getDurable() && name == null) {
316 name = getSubscriberName();
317 }
318 MessageConsumer consumer = createConsumer(session, destination,
319 name, selector, noLocal);
320 if (behaviour.getReceiptType().equals(ReceiptType.SYNCHRONOUS)) {
321 result = new SynchronousReceiver(session, consumer, name);
322 } else {
323 result = new AsynchronousReceiver(session, consumer, name);
324 }
325 }
326 return result;
327 }
328
329 /***
330 * Create a {@link MessageReceiver} for the supplied test context and
331 * destination
332 *
333 * @param context the test context
334 * @param destination the destination to consume messages from
335 * @return a new message receiver
336 * @throws JMSException if any of the JMS operations fail
337 */
338 public static MessageReceiver createReceiver(TestContext context,
339 Destination destination)
340 throws JMSException {
341
342 return createReceiver(context.getSession(), destination,
343 context.getMessagingBehaviour());
344 }
345
346 /***
347 * Create a {@link MessageReceiver} for the supplied test context,
348 * destination, selector, and noLocal flag
349 *
350 * @param context the test context
351 * @param destination the destination to consume messages from
352 * @param selector the message selector
353 * @param noLocal if the session is a TopicSession, then the subscriber
354 * will be created with the supplied noLocal flag, otherwise the argument
355 * is ignored.
356 * @return a new message receiver
357 * @throws JMSException if any of the JMS operations fail
358 */
359 public static MessageReceiver createReceiver(TestContext context,
360 Destination destination,
361 String selector,
362 boolean noLocal)
363 throws JMSException {
364
365 return createReceiver(context.getSession(), destination,
366 context.getMessagingBehaviour(), selector,
367 noLocal);
368 }
369
370 /***
371 * Create a {@link MessageReceiver} for the supplied test context,
372 * destination, name, selector, and noLocal flag
373 *
374 * @param context the test context
375 * @param destination the destination to consume messages from
376 * @param name if non-null, and the session is a TopicSession, then a
377 * durable subscriber will be returned, otherwise the argument is ignored.
378 * If null, and the session is a TopicSession and the behaviour is for
379 * durable subscribers, then a name will be automatically allocated.
380 * @param selector the message selector
381 * @param noLocal if the session is a TopicSession, then the subscriber
382 * will be created with the supplied noLocal flag, otherwise the argument
383 * is ignored.
384 * @return a new message receiver
385 * @throws JMSException if any of the JMS operations fail
386 */
387 public static MessageReceiver createReceiver(TestContext context,
388 Destination destination,
389 String name,
390 String selector,
391 boolean noLocal)
392 throws JMSException {
393 return createReceiver(context.getSession(), destination,
394 context.getMessagingBehaviour(), name, selector,
395 noLocal);
396 }
397
398 /***
399 * Create a {@link MessageSender} for the supplied session, destination
400 * and behaviour
401 *
402 * @param session the sesssion
403 * @param destination the destination to send messages to
404 * @param behaviour the messaging behaviour
405 * @return a new message sender
406 * @throws JMSException if any of the JMS operations fail
407 */
408 public static MessageSender createSender(Session session,
409 Destination destination,
410 MessagingBehaviour behaviour)
411 throws JMSException {
412
413 MessageSender result = null;
414 MessageProducer producer = createProducer(session, destination);
415 if (session instanceof XAQueueSession) {
416 session = ((XAQueueSession) session).getQueueSession();
417 } else if (session instanceof XATopicSession) {
418 session = ((XATopicSession) session).getTopicSession();
419 }
420
421 if (session instanceof QueueSession) {
422 result = new QueueMessageSender((QueueSender) producer, behaviour);
423 } else {
424 result = new TopicMessageSender((TopicPublisher) producer,
425 behaviour);
426 }
427 return result;
428 }
429
430 /***
431 * Create a {@link MessageSender} for the supplied test context
432 *
433 * @param context the test context
434 * @param destination the destination to send messages to
435 * @return a new message sender
436 * @throws JMSException if any of the JMS operations fail
437 */
438 public static MessageSender createSender(TestContext context,
439 Destination destination)
440 throws JMSException {
441
442 return createSender(context.getSession(), destination,
443 context.getMessagingBehaviour());
444 }
445
446 /***
447 * Return a unique name for a durable subscriber
448 *
449 * @return a unique name
450 */
451 public static synchronized String getSubscriberName() {
452 return "subscriber" + ++_subscriberSeed;
453 }
454
455 }
This page was automatically generated by Maven