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: ConnectionHelper.java,v 1.4 2004/01/31 13:44:24 tanderson Exp $
44   */
45  package org.exolab.jmscts.core;
46  
47  import javax.jms.ConnectionConsumer;
48  import javax.jms.Destination;
49  import javax.jms.JMSException;
50  import javax.jms.Queue;
51  import javax.jms.QueueConnection;
52  import javax.jms.ServerSessionPool;
53  import javax.jms.Session;
54  import javax.jms.Topic;
55  import javax.jms.TopicConnection;
56  import javax.jms.XAQueueConnection;
57  import javax.jms.XATopicConnection;
58  
59  
60  /***
61   * Helper for performing connection operations
62   *
63   * @version     $Revision: 1.4 $ $Date: 2004/01/31 13:44:24 $
64   * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
65   */
66  public final class ConnectionHelper {
67  
68      /***
69       * Prevent construction of utility class
70       */
71      private ConnectionHelper() {
72      }
73  
74      /***
75       * Create a session
76       *
77       * @param context the test context
78       * @param type the message acknowledgement type
79       * @return a new session
80       * @throws JMSException if any of the JMS operations fail
81       */
82      public static Session createSession(TestContext context, AckType type)
83          throws JMSException {
84  
85          if (context == null) {
86              throw new IllegalArgumentException("Argument 'context' is null");
87          }
88          if (type == null) {
89              throw new IllegalArgumentException("Argument 'type' is null");
90          }
91          if (context.getConnection() == null) {
92              throw new IllegalArgumentException(
93                  "Argument 'context' is not a connection context");
94          }
95  
96          Session result = null;
97          if (context.isQueueConnectionFactory()) {
98              QueueConnection connection =
99                  (QueueConnection) context.getConnection();
100             result = connection.createQueueSession(type.getTransacted(),
101                                                    type.getAcknowledgeMode());
102         } else if (context.isTopicConnectionFactory()) {
103             TopicConnection connection =
104                 (TopicConnection) context.getConnection();
105             result = connection.createTopicSession(type.getTransacted(),
106                                                    type.getAcknowledgeMode());
107         } else if (context.isXAQueueConnectionFactory()) {
108             XAQueueConnection connection =
109                 (XAQueueConnection) context.getConnection();
110             result = connection.createXAQueueSession();
111         } else {
112             XATopicConnection connection =
113                 (XATopicConnection) context.getConnection();
114             result = connection.createXATopicSession();
115         }
116         return result;
117     }
118 
119     /***
120      * Create a session of with the same behaviour as that of the supplied
121      * context
122      *
123      * @param context the test context
124      * @return a new session
125      * @throws JMSException if any of the JMS operations fail
126      */
127     public static Session createSession(TestContext context)
128         throws JMSException {
129 
130         if (context == null) {
131             throw new IllegalArgumentException("Argument 'context' is null");
132         }
133         if (context.getSession() == null) {
134             throw new IllegalArgumentException(
135                 "Argument 'context' is not a session context");
136         }
137         return createSession(context, context.getAckType());
138     }
139 
140     /***
141      * Create a connection consumer
142      *
143      * @param context the test context
144      * @param destination the destination to access
145      * @param name if non-null, and the connection is a TopicConnection, then
146      * a durable connection consumer will be returned, otherwise the argument
147      * is ignored.
148      * @param selector the message selector. May be <code>null</code>
149      * @param pool the server session pool to associate with the consumer
150      * @param maxMessages the maximum number of messages that can be assigned
151      * to a server session at one time
152      * @return the new connection consumer
153      * @throws JMSException if any of the JMS operations fail
154      */
155     public static ConnectionConsumer createConnectionConsumer(
156         TestContext context, Destination destination, String name,
157         String selector, ServerSessionPool pool, int maxMessages)
158         throws JMSException {
159 
160         ConnectionConsumer result;
161 
162         if (context == null) {
163             throw new IllegalArgumentException("Argument 'context' is null");
164         }
165         if (pool == null) {
166             throw new IllegalArgumentException("Argument 'pool' is null");
167         }
168         if (context.getConnection() == null) {
169             throw new IllegalArgumentException(
170                 "Argument 'context' is not a connection context");
171         }
172 
173         if (context.isQueue()) {
174             QueueConnection connection =
175                 (QueueConnection) context.getConnection();
176             result = connection.createConnectionConsumer(
177                 (Queue) destination, selector, pool, maxMessages);
178         } else {
179             TopicConnection connection =
180                 (TopicConnection) context.getConnection();
181             if (name == null) {
182                 result = connection.createConnectionConsumer(
183                     (Topic) destination, selector, pool, maxMessages);
184             } else {
185                 result = connection.createDurableConnectionConsumer(
186                     (Topic) destination, name, selector, pool, maxMessages);
187             }
188         }
189         return result;
190     }
191 
192 }
This page was automatically generated by Maven