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: DestinationHelper.java,v 1.3 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.Queue;
50 import javax.jms.QueueSession;
51 import javax.jms.Session;
52 import javax.jms.TemporaryQueue;
53 import javax.jms.TemporaryTopic;
54 import javax.jms.Topic;
55 import javax.jms.TopicSession;
56 import javax.jms.XAQueueSession;
57 import javax.jms.XATopicSession;
58 import javax.naming.NamingException;
59
60 import org.exolab.jmscts.provider.Administrator;
61
62
63 /***
64 * Helper for creating, destroying and comparing Destination objects
65 *
66 * @version $Revision: 1.3 $ $Date: 2004/01/31 13:44:24 $
67 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
68 */
69 public final class DestinationHelper {
70
71 /***
72 * Prevent construction of utility class
73 */
74 private DestinationHelper() {
75 }
76
77 /***
78 * Create a destination for the supplied name, using a test context
79 * If the context specifies a messaging behaviour, then that will
80 * used to determine if an administered or temporary destination will
81 * be created, otherwise an administered destination will be created.
82 *
83 * @param context the test context
84 * @param name the destination name
85 * @return the new destination
86 * @throws JMSException if the destination cannot be created
87 * @throws NamingException if the new administered destination cannot be
88 * located in JNDI
89 */
90 public static Destination create(TestContext context, String name)
91 throws JMSException, NamingException {
92 Destination result = null;
93 boolean isQueue = context.isQueueConnectionFactory()
94 || context.isXAQueueConnectionFactory();
95 MessagingBehaviour behaviour = context.getMessagingBehaviour();
96 if (behaviour == null || behaviour.getAdministered()) {
97 result = create(name, isQueue, context.getAdministrator());
98 } else {
99 result = create(context.getSession());
100 }
101 return result;
102 }
103
104 /***
105 * Create an administered destination for the supplied name.
106 *
107 * @param name the name of the destination
108 * @param queue true if the destination is a Queue
109 * @param admin the JMS provider administration interface
110 * @return the new destination
111 * @throws JMSException if the destination cannot be created
112 * @throws NamingException if the new administered destination cannot be
113 * be located in JNDI
114 */
115 public static Destination create(String name, boolean queue,
116 Administrator admin)
117 throws JMSException, NamingException {
118
119 admin.createDestination(name, queue);
120 return (Destination) admin.lookup(name);
121 }
122
123 /***
124 * Create a temporary destination
125 *
126 * @param session the session to create the destination with
127 * @return the new destination
128 * @throws JMSException if the destination cannot be created
129 */
130 public static Destination create(Session session) throws JMSException {
131 Destination result = null;
132 if (session instanceof XAQueueSession) {
133 session = ((XAQueueSession) session).getQueueSession();
134 } else if (session instanceof XATopicSession) {
135 session = ((XATopicSession) session).getTopicSession();
136 }
137
138 if (session instanceof QueueSession) {
139 result = ((QueueSession) session).createTemporaryQueue();
140 } else {
141 result = ((TopicSession) session).createTemporaryTopic();
142 }
143 return result;
144 }
145
146 /***
147 * Destroy a destination if it exists, using a test context
148 *
149 * @param context the test context
150 * @param destination the destination to destroy
151 * @throws JMSException if the destination cannot be destroyed
152 */
153 public static void destroy(TestContext context, Destination destination)
154 throws JMSException {
155 destroy(destination, context.getAdministrator());
156 }
157
158 /***
159 * Destroy the named destination, if it exists
160 *
161 * @param name the name of the destination
162 * @param admin the provider administration interface to remove the
163 * destination with, if the destination exists
164 * @throws JMSException if the destination cannot be destroyed
165 */
166 public static void destroy(String name, Administrator admin)
167 throws JMSException {
168 if (admin.destinationExists(name)) {
169 admin.destroyDestination(name);
170 }
171 }
172
173 /***
174 * Destroy a destination
175 *
176 * @param destination the destination to destroy
177 * @param admin the provider administration interface to remove the
178 * destination with, if the destination is administered and exists
179 * @throws JMSException if the destination cannot be destroyed
180 */
181 public static void destroy(Destination destination, Administrator admin)
182 throws JMSException {
183 if (destination instanceof TemporaryQueue) {
184 ((TemporaryQueue) destination).delete();
185 } else if (destination instanceof TemporaryTopic) {
186 ((TemporaryTopic) destination).delete();
187 } else {
188 destroy(getName(destination), admin);
189 }
190 }
191
192 /***
193 * Compares two destinations for equality
194 *
195 * @param a the first destination to compare
196 * @param b the second destination to compare
197 * @return true if the destinations are equal
198 * @throws JMSException if the destination names cannot be accessed
199 */
200 public static boolean equal(Destination a, Destination b)
201 throws JMSException {
202 boolean equal = false;
203 if (a instanceof Queue && b instanceof Queue) {
204 String nameA = ((Queue) a).getQueueName();
205 String nameB = ((Queue) b).getQueueName();
206 equal = nameA.equals(nameB);
207 } else if (a instanceof Topic && b instanceof Topic) {
208 String nameA = ((Topic) a).getTopicName();
209 String nameB = ((Topic) b).getTopicName();
210 equal = nameA.equals(nameB);
211 }
212 return equal;
213 }
214
215 /***
216 * Returns the name of a destination
217 *
218 * @param destination the destination
219 * @return the name of the destination
220 * @throws JMSException if the name cannot be accessed
221 */
222 public static String getName(Destination destination) throws JMSException {
223 String name = null;
224 if (destination instanceof Queue) {
225 name = ((Queue) destination).getQueueName();
226 } else {
227 name = ((Topic) destination).getTopicName();
228 }
229 return name;
230 }
231
232 }
This page was automatically generated by Maven