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 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: MessagingTool.java,v 1.2 2004/02/03 21:52:12 tanderson Exp $
44 */
45 package org.exolab.jmscts.tools;
46
47 import java.util.Enumeration;
48 import java.util.Iterator;
49
50 import javax.jms.ConnectionFactory;
51 import javax.jms.DeliveryMode;
52 import javax.jms.Destination;
53 import javax.jms.JMSException;
54 import javax.jms.Message;
55 import javax.jms.Queue;
56 import javax.jms.QueueConnectionFactory;
57 import javax.jms.Topic;
58 import javax.jms.TopicConnectionFactory;
59 import javax.jms.XAQueueConnectionFactory;
60 import javax.jms.XATopicConnectionFactory;
61 import javax.naming.NamingException;
62
63 import org.apache.log4j.Category;
64
65 import org.exolab.jmscts.core.TestContext;
66 import org.exolab.jmscts.core.TestStatistics;
67 import org.exolab.jmscts.provider.Administrator;
68 import org.exolab.jmscts.provider.Configuration;
69 import org.exolab.jmscts.provider.ProviderLoader;
70
71
72 /***
73 * Messaging tool helper
74 *
75 * @version $Revision: 1.2 $ $Date: 2004/02/03 21:52:12 $
76 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
77 */
78 public abstract class MessagingTool {
79
80 /***
81 * The configuration path
82 */
83 private String _path;
84
85 /***
86 * The connection factory name
87 */
88 private String _factoryName;
89
90 /***
91 * The destination name
92 */
93 private String _destName;
94
95 /***
96 * The provider
97 */
98 private ProviderLoader _provider;
99
100 /***
101 * The provider administrator
102 */
103 private Administrator _admin;
104
105 /***
106 * The connection factory test context
107 */
108 private TestContext _context;
109
110 /***
111 * The destination to send/receive/browse messages
112 */
113 private Destination _destination;
114
115 /***
116 * The number of messages to process
117 */
118 private int _count;
119
120 /***
121 * If <code>true</code>, log message properties
122 */
123 private boolean _verbose = false;
124
125 /***
126 * The logger
127 */
128 private static final Category log =
129 Category.getInstance(MessagingTool.class);
130
131
132 /***
133 * Set the configuration
134 *
135 * @param path the path to the provider configuration
136 */
137 public void setConfig(String path) {
138 _path = path;
139 }
140
141 /***
142 * Set the connection factory name
143 *
144 * @param name the connection factory name
145 */
146 public void setConnectionFactory(String name) {
147 _factoryName = name;
148 }
149
150 /***
151 * Set the destination name
152 *
153 * @param name the destination name
154 */
155 public void setDestination(String name) {
156 _destName = name;
157 }
158
159 /***
160 * Sets number of messages to process
161 *
162 * @param count the number of messages to process
163 */
164 public void setCount(int count) {
165 _count = count;
166 }
167
168 /***
169 * Enable/disable verbose logging of messages
170 *
171 * @param verbose if <code>true</code>, log message properties
172 */
173 public void setVerbose(boolean verbose) {
174 _verbose = verbose;
175 }
176
177 /***
178 * Invoke the messaging tool
179 *
180 * @throws Exception for any error
181 */
182 public void invoke() throws Exception {
183 initialise();
184 doInvoke();
185 }
186
187 /***
188 * Invoke the messaging tool
189 *
190 * @throws Exception for any error
191 */
192 protected abstract void doInvoke() throws Exception;
193
194 /***
195 * Initialise the tool
196 *
197 * @throws Exception for any error
198 */
199 protected void initialise() throws Exception {
200 // load the provider configuration
201 Configuration config = Configuration.read(_path);
202
203 Iterator iterator = config.getProviders().iterator();
204 _provider = (ProviderLoader) iterator.next();
205 _provider.getProvider().initialise(false);
206
207 _admin = _provider.getProvider().getAdministrator();
208 _destination = (Destination) _admin.lookup(_destName);
209
210 Class factoryClass = null;
211 ConnectionFactory factory = null;
212
213 if (_factoryName == null) {
214 factory = getDefaultConnectionFactory(_destination);
215 factoryClass = getDefaultConnectionFactoryType(_destination);
216 } else {
217 factory = getConnectionFactory(_factoryName);
218 factoryClass = getConnectionFactoryType(factory);
219 }
220
221 TestContext rootContext = new TestContext(new TestStatistics());
222 TestContext providerContext = new TestContext(rootContext, _admin);
223 _context = new TestContext(providerContext, factory, factoryClass);
224 }
225
226 /***
227 * Returns the connection factory test context
228 *
229 * @return the connection factory test context
230 */
231 protected TestContext getContext() {
232 return _context;
233 }
234
235 /***
236 * Returns the destination
237 *
238 * @return the destination
239 */
240 protected Destination getDestination() {
241 return _destination;
242 }
243
244 /***
245 * Returns the number of messages to process
246 *
247 * @return the number of messages to process
248 */
249 protected int getCount() {
250 return _count;
251 }
252
253 /***
254 * Log a message
255 *
256 * @param message the message to log
257 */
258 protected void log(Message message) {
259 String messageId = null;
260 String mode = null;
261 StringBuffer properties = new StringBuffer();
262 try {
263 messageId = message.getJMSMessageID();
264 if (message.getJMSDeliveryMode() == DeliveryMode.PERSISTENT) {
265 mode = "PERSISTENT";
266 } else {
267 mode = "NON_PERSISTENT";
268 }
269 } catch (JMSException exception) {
270 log.error(exception);
271 }
272 System.out.println(messageId + " " + mode);
273 if (_verbose) {
274 try {
275 Enumeration iterator = message.getPropertyNames();
276 while (iterator.hasMoreElements()) {
277 String name = (String) iterator.nextElement();
278 Object object = message.getObjectProperty(name);
279 System.out.println(" " + name + "=" + object);
280 }
281 } catch (JMSException exception) {
282 log.error(exception);
283 }
284 }
285 }
286
287 /***
288 * Returns the connection factory for the specified name
289 *
290 * @param name the connection factory name.
291 * @return the connection factory
292 * @throws NamingException if the connection factory cannot be located
293 */
294 private ConnectionFactory getConnectionFactory(String name)
295 throws NamingException {
296 return (ConnectionFactory) _admin.lookup(name);
297 }
298
299 /***
300 * Returns the type of the connection factory
301 *
302 * @param factory the connection factory
303 * @return the type of the connection factory
304 */
305 private Class getConnectionFactoryType(ConnectionFactory factory) {
306 Class result;
307 if (factory instanceof XAQueueConnectionFactory) {
308 result = XAQueueConnectionFactory.class;
309 } else if (factory instanceof QueueConnectionFactory) {
310 result = QueueConnectionFactory.class;
311 } else if (factory instanceof XATopicConnectionFactory) {
312 result = XATopicConnectionFactory.class;
313 } else if (factory instanceof TopicConnectionFactory) {
314 result = TopicConnectionFactory.class;
315 } else {
316 throw new IllegalArgumentException(
317 "Unsupported connection factory: " + factory);
318 }
319 return result;
320 }
321
322 /***
323 * Returns the default connection factory for the supplied destination
324 *
325 * @param destination the destination
326 * @return the default connection factory
327 * @throws NamingException if the connection factory cannot be located
328 */
329 private ConnectionFactory getDefaultConnectionFactory(
330 Destination destination) throws NamingException {
331
332 String name;
333 if (destination instanceof Queue) {
334 name = _admin.getQueueConnectionFactory();
335 } else if (destination instanceof Topic) {
336 name = _admin.getTopicConnectionFactory();
337 } else {
338 throw new IllegalArgumentException("Unsupported destination: "
339 + destination);
340 }
341
342 return (ConnectionFactory) getConnectionFactory(name);
343 }
344
345 /***
346 * Returns the type of the default connection factory for the supplied
347 * destination
348 *
349 * @param destination the destination
350 * @return the default connection factory type
351 */
352 private Class getDefaultConnectionFactoryType(Destination destination) {
353 Class result;
354 if (destination instanceof Queue) {
355 result = QueueConnectionFactory.class;
356 } else if (destination instanceof Topic) {
357 result = TopicConnectionFactory.class;
358 } else {
359 throw new IllegalArgumentException("Unsupported destination: "
360 + destination);
361 }
362 return result;
363 }
364 }
365
This page was automatically generated by Maven