Update of /cvsroot/jmscts/jmscts/src/java/org/exolab/jmscts/tools
In directory sc8-pr-cvs1:/tmp/cvs-serv23697/src/java/org/exolab/jmscts/tools
Added Files:
MessagingCommand.java MessagingTool.java
Log Message:
created
--- NEW FILE: MessagingCommand.java ---
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@....
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2003 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: MessagingCommand.java,v 1.1 2003/12/28 14:03:13 tanderson Exp $
*/
package org.exolab.jmscts.tools;
import java.util.Enumeration;
import java.util.Iterator;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Topic;
import javax.jms.TopicConnectionFactory;
import javax.jms.XAQueueConnectionFactory;
import javax.jms.XATopicConnectionFactory;
import javax.naming.NamingException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.log4j.Category;
import org.apache.log4j.xml.DOMConfigurator;
import org.exolab.jmscts.core.AckType;
import org.exolab.jmscts.core.DeliveryType;
import org.exolab.jmscts.core.MessageCreator;
import org.exolab.jmscts.core.MessagingBehaviour;
import org.exolab.jmscts.core.MessagingHelper;
import org.exolab.jmscts.core.TestContext;
import org.exolab.jmscts.core.TestContextHelper;
import org.exolab.jmscts.core.TestStatistics;
import org.exolab.jmscts.provider.Administrator;
import org.exolab.jmscts.provider.Configuration;
import org.exolab.jmscts.provider.Provider;
import org.exolab.jmscts.provider.ProviderLoader;
/**
* Messaging tool command parser
*
* @version $Revision: 1.1 $ $Date: 2003/12/28 14:03:13 $
* @author <a href="mailto:tma@... Anderson</a>
*/
public abstract class MessagingCommand {
/**
* The short name for the provider configuration argument
*/
protected static String CONFIG = "c";
/**
* The long name for the provider configuration argument
*/
protected static String CONFIG_LONG = "config";
/**
* The short name for the factory argument
*/
protected static String FACTORY = "f";
/**
* The long name for the factory argument
*/
protected static String FACTORY_LONG = "factory";
/**
* The short name for the destination argument
*/
protected static String DESTINATION = "d";
/**
* The long name for the destination argument
*/
protected static String DESTINATION_LONG = "destination";
/**
* The short name for the count argument
*/
protected static String COUNT = "c";
/**
* The long name for the count argument
*/
protected static String COUNT_LONG = "count";
/**
* The short name for the verbose argument
*/
protected static String VERBOSE = "v";
/**
* The long name for the verbose argument
*/
protected static String VERBOSE_LONG = "verbose";
/**
* Construct a new <code>MessagingCommand</code>
*/
public MessagingCommand() {
// configure the logger
DOMConfigurator.configure(getHome() + "/config/log4j.xml");
}
/**
* Invoke the command
*
* @param args the command line arguments
* @throws Exception for any error
*/
public void invoke(String[] args) throws Exception {
// parse the command line
Options options = getOptions();
CommandLineParser parser = new PosixParser();
CommandLine commands = null;
try {
commands = parser.parse(options, args);
} catch (ParseException exception) {
usage(options, exception.getMessage());
}
MessagingTool tool = create();
parse(tool, commands);
tool.invoke();
}
/**
* Create the messaging tool
*
* @return the messaging tool
*/
protected abstract MessagingTool create();
/**
* Parse the command line, populating the tool
*
* @param tool the messaging tool
* @param commands the command line
* @throws ParseException if the command line cannot be parsed
*/
protected void parse(MessagingTool tool, CommandLine commands)
throws ParseException {
String config = commands.getOptionValue(
"config", getHome() + "/config/providers.xml");
tool.setConfig(config);
tool.setConnectionFactory(commands.getOptionValue(FACTORY));
tool.setDestination(commands.getOptionValue(DESTINATION));
tool.setCount(Integer.parseInt(commands.getOptionValue(COUNT)));
if (commands.hasOption(VERBOSE)) {
tool.setVerbose(true);
}
}
/**
* Returns the command line arguments
*/
protected Options getOptions() {
Option config = OptionBuilder.withArgName("path")
.hasArg()
.withLongOpt(CONFIG_LONG)
.withDescription("the provider configuration path")
.create(CONFIG);
Option factory = OptionBuilder.withArgName("name")
.hasArg()
.withLongOpt(FACTORY_LONG)
.withDescription("the connection factory name")
.create(FACTORY);
Option destination = OptionBuilder.withArgName("name")
.hasArg()
.isRequired()
.withLongOpt(DESTINATION_LONG)
.withDescription("the destination name")
.create(DESTINATION);
Option count = OptionBuilder.withArgName("number")
.hasArg()
.isRequired()
.withLongOpt(COUNT_LONG)
.withDescription("the number of messages to process")
.create(COUNT);
Option verbose = OptionBuilder.withDescription("use verbose logging")
.withLongOpt(VERBOSE_LONG)
.create(VERBOSE);
Options options = new Options();
options.addOption(config);
options.addOption(factory);
options.addOption(destination);
options.addOption(count);
options.addOption(verbose);
return options;
}
/**
* Returns the command usage
*
* @return the command usage
*/
protected abstract String getUsage();
/**
* Prints the usage for the command, and exits with an error code
*
* @param options the command line options
* @param message message to display before the usage. May be
* <code>null</code>
*/
protected void usage(Options options, String message) {
if (message != null) {
System.err.println(message);
}
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(getUsage(), options);
System.exit(1);
}
/**
* Returns the value of the <code>jmscts.home</code> system property,
* defaulting to the value of <code>user.dir</code> if its not set
*
* @return the value of the <code>jmscts.home</code> system property
*/
protected String getHome() {
return System.getProperty("jmscts.home",
System.getProperty("user.dir"));
}
}
--- NEW FILE: MessagingTool.java ---
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@....
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2003 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: MessagingTool.java,v 1.1 2003/12/28 14:03:13 tanderson Exp $
*/
package org.exolab.jmscts.tools;
import java.util.Enumeration;
import java.util.Iterator;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Topic;
import javax.jms.TopicConnectionFactory;
import javax.jms.XAQueueConnectionFactory;
import javax.jms.XATopicConnectionFactory;
import javax.naming.NamingException;
import org.apache.log4j.Category;
import org.apache.log4j.xml.DOMConfigurator;
import org.exolab.jmscts.core.AckType;
import org.exolab.jmscts.core.DeliveryType;
import org.exolab.jmscts.core.MessageCreator;
import org.exolab.jmscts.core.MessagingBehaviour;
import org.exolab.jmscts.core.MessagingHelper;
import org.exolab.jmscts.core.TestContext;
import org.exolab.jmscts.core.TestContextHelper;
import org.exolab.jmscts.core.TestStatistics;
import org.exolab.jmscts.provider.Administrator;
import org.exolab.jmscts.provider.Configuration;
import org.exolab.jmscts.provider.Provider;
import org.exolab.jmscts.provider.ProviderLoader;
/**
* Messaging tool helper
*
* @version $Revision: 1.1 $ $Date: 2003/12/28 14:03:13 $
* @author <a href="mailto:tma@... Anderson</a>
*/
public abstract class MessagingTool {
/**
* The configuration path
*/
private String _path;
/**
* The connection factory name
*/
private String _factoryName;
/**
* The destination name
*/
private String _destName;
/**
* The provider
*/
private ProviderLoader _provider;
/**
* The provider administrator
*/
private Administrator _admin;
/**
* The connection factory test context
*/
private TestContext _context;
/**
* The destination to send/receive/browse messages
*/
private Destination _destination;
/**
* The number of messages to process
*/
private int _count;
/**
* If <code>true</code>, log message properties
*/
private boolean _verbose = false;
/**
* The logger
*/
private static final Category log =
Category.getInstance(MessagingTool.class);
/**
* Set the configuration
*
* @param path the path to the provider configuration
*/
public void setConfig(String path) {
_path = path;
}
/**
* Set the connection factory name
*
* @param name the connection factory name
*/
public void setConnectionFactory(String name) {
_factoryName = name;
}
/**
* Set the destination name
*
* @param name the destination name
*/
public void setDestination(String name) {
_destName = name;
}
/**
* Sets number of messages to process
*
* @param count the number of messages to process
*/
public void setCount(int count) {
_count = count;
}
/**
* Enable/disable verbose logging of messages
*
* @param verbose if <code>true</code>, log message properties
*/
public void setVerbose(boolean verbose) {
_verbose = verbose;
}
/**
* Invoke the messaging tool
*
* @throws Exception for any error
*/
public void invoke() throws Exception {
initialise();
doInvoke();
}
/**
* Invoke the messaging tool
*
* @throws Exception for any error
*/
protected abstract void doInvoke() throws Exception;
/**
* Initialise the tool
*
* @throws Exception for any error
*/
protected void initialise() throws Exception {
// load the provider configuration
Configuration config = Configuration.read(_path);
Iterator iterator = config.getProviders().iterator();
_provider = (ProviderLoader) iterator.next();
_provider.getProvider().initialise(false);
_admin = _provider.getProvider().getAdministrator();
_destination = (Destination) _admin.lookup(_destName);
Class factoryClass = null;
ConnectionFactory factory = null;
if (_factoryName == null) {
factory = getDefaultConnectionFactory(_destination);
factoryClass = getDefaultConnectionFactoryType(_destination);
} else {
factory = getConnectionFactory(_factoryName);
factoryClass = getConnectionFactoryType(factory);
}
TestContext rootContext = new TestContext(new TestStatistics());
TestContext providerContext = new TestContext(rootContext, _admin);
_context = new TestContext(providerContext, factory, factoryClass);
}
/**
* Returns the connection factory test context
*/
protected TestContext getContext() {
return _context;
}
/**
* Returns the destination
*
* @return the destination
*/
protected Destination getDestination() {
return _destination;
}
/**
* Returns the number of messages to process
*
* @return the number of messages to process
*/
protected int getCount() {
return _count;
}
/**
* Log a message
*
* @param message the message to log
*/
protected void log(Message message) {
String messageId = null;
String mode = null;
StringBuffer properties = new StringBuffer();
try {
messageId = message.getJMSMessageID();
if (message.getJMSDeliveryMode() == DeliveryMode.PERSISTENT) {
mode = "PERSISTENT";
} else {
mode = "NON_PERSISTENT";
}
} catch (JMSException exception) {
log.error(exception);
}
System.out.println(messageId + " " + mode);
if (_verbose) {
try {
Enumeration iterator = message.getPropertyNames();
while (iterator.hasMoreElements()) {
String name = (String) iterator.nextElement();
Object object = message.getObjectProperty(name);
System.out.println(" " + name + "=" + object);
}
} catch (JMSException exception) {
log.error(exception);
}
}
}
/**
* Returns the connection factory for the specified name
*
* @param name the connection factory name.
* @return the connection factory
* @throws NamingException if the connection factory cannot be located
*/
private ConnectionFactory getConnectionFactory(String name)
throws NamingException {
return (ConnectionFactory) _admin.lookup(name);
}
/**
* Returns the type of the connection factory
*
* @param factory the connection factory
* @return the type of the connection factory
*/
private Class getConnectionFactoryType(ConnectionFactory factory) {
Class result;
if (factory instanceof XAQueueConnectionFactory) {
result = XAQueueConnectionFactory.class;
} else if (factory instanceof QueueConnectionFactory) {
result = QueueConnectionFactory.class;
} else if (factory instanceof XATopicConnectionFactory) {
result = XATopicConnectionFactory.class;
} else if (factory instanceof TopicConnectionFactory) {
result = TopicConnectionFactory.class;
} else {
throw new IllegalArgumentException(
"Unsupported connection factory: " + factory);
}
return result;
}
/**
* Returns the default connection factory for the supplied destination
*
* @param destination the destination
* @return the default connection factory
* @throws NamingException if the connection factory cannot be located
*/
private ConnectionFactory getDefaultConnectionFactory(
Destination destination) throws NamingException {
String name;
if (destination instanceof Queue) {
name = _admin.getQueueConnectionFactory();
} else if (destination instanceof Topic) {
name = _admin.getTopicConnectionFactory();
} else {
throw new IllegalArgumentException("Unsupported destination: "
+ destination);
}
return (ConnectionFactory) getConnectionFactory(name);
}
/**
* Returns the type of the default connection factory for the supplied
* destination
*
* @param destination the destination
*/
private Class getDefaultConnectionFactoryType(Destination destination) {
Class result;
if (destination instanceof Queue) {
result = QueueConnectionFactory.class;
} else if (destination instanceof Topic) {
result = TopicConnectionFactory.class;
} else {
throw new IllegalArgumentException("Unsupported destination: "
+ destination);
}
return result;
}
}
|