View Javadoc
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: MessagingCommand.java,v 1.2 2004/02/03 21:52:12 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.tools; 46 47 import org.apache.commons.cli.CommandLine; 48 import org.apache.commons.cli.CommandLineParser; 49 import org.apache.commons.cli.HelpFormatter; 50 import org.apache.commons.cli.Option; 51 import org.apache.commons.cli.OptionBuilder; 52 import org.apache.commons.cli.Options; 53 import org.apache.commons.cli.ParseException; 54 import org.apache.commons.cli.PosixParser; 55 import org.apache.log4j.xml.DOMConfigurator; 56 57 58 /*** 59 * Messaging tool command parser 60 * 61 * @version $Revision: 1.2 $ $Date: 2004/02/03 21:52:12 $ 62 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 63 */ 64 public abstract class MessagingCommand { 65 66 /*** 67 * The short name for the provider configuration argument 68 */ 69 protected static final String CONFIG = "c"; 70 71 /*** 72 * The long name for the provider configuration argument 73 */ 74 protected static final String CONFIG_LONG = "config"; 75 76 /*** 77 * The short name for the factory argument 78 */ 79 protected static final String FACTORY = "f"; 80 81 /*** 82 * The long name for the factory argument 83 */ 84 protected static final String FACTORY_LONG = "factory"; 85 86 /*** 87 * The short name for the destination argument 88 */ 89 protected static final String DESTINATION = "d"; 90 91 /*** 92 * The long name for the destination argument 93 */ 94 protected static final String DESTINATION_LONG = "destination"; 95 96 /*** 97 * The short name for the count argument 98 */ 99 protected static final String COUNT = "c"; 100 101 /*** 102 * The long name for the count argument 103 */ 104 protected static final String COUNT_LONG = "count"; 105 106 /*** 107 * The short name for the verbose argument 108 */ 109 protected static final String VERBOSE = "v"; 110 111 /*** 112 * The long name for the verbose argument 113 */ 114 protected static final String VERBOSE_LONG = "verbose"; 115 116 117 /*** 118 * Construct a new <code>MessagingCommand</code> 119 */ 120 public MessagingCommand() { 121 // configure the logger 122 DOMConfigurator.configure(getHome() + "/config/log4j.xml"); 123 } 124 125 /*** 126 * Invoke the command 127 * 128 * @param args the command line arguments 129 * @throws Exception for any error 130 */ 131 public void invoke(String[] args) throws Exception { 132 // parse the command line 133 Options options = getOptions(); 134 CommandLineParser parser = new PosixParser(); 135 CommandLine commands = null; 136 137 try { 138 commands = parser.parse(options, args); 139 } catch (ParseException exception) { 140 usage(options, exception.getMessage()); 141 } 142 143 MessagingTool tool = create(); 144 parse(tool, commands); 145 146 tool.invoke(); 147 } 148 149 /*** 150 * Create the messaging tool 151 * 152 * @return the messaging tool 153 */ 154 protected abstract MessagingTool create(); 155 156 /*** 157 * Parse the command line, populating the tool 158 * 159 * @param tool the messaging tool 160 * @param commands the command line 161 * @throws ParseException if the command line cannot be parsed 162 */ 163 protected void parse(MessagingTool tool, CommandLine commands) 164 throws ParseException { 165 166 String config = commands.getOptionValue( 167 "config", getHome() + "/config/providers.xml"); 168 169 tool.setConfig(config); 170 tool.setConnectionFactory(commands.getOptionValue(FACTORY)); 171 tool.setDestination(commands.getOptionValue(DESTINATION)); 172 tool.setCount(Integer.parseInt(commands.getOptionValue(COUNT))); 173 if (commands.hasOption(VERBOSE)) { 174 tool.setVerbose(true); 175 } 176 } 177 178 /*** 179 * Returns the command line arguments 180 * 181 * @return the command line arguments 182 */ 183 protected Options getOptions() { 184 Option config = OptionBuilder.withArgName("path") 185 .hasArg() 186 .withLongOpt(CONFIG_LONG) 187 .withDescription("the provider configuration path") 188 .create(CONFIG); 189 190 Option factory = OptionBuilder.withArgName("name") 191 .hasArg() 192 .withLongOpt(FACTORY_LONG) 193 .withDescription("the connection factory name") 194 .create(FACTORY); 195 196 Option destination = OptionBuilder.withArgName("name") 197 .hasArg() 198 .isRequired() 199 .withLongOpt(DESTINATION_LONG) 200 .withDescription("the destination name") 201 .create(DESTINATION); 202 203 Option count = OptionBuilder.withArgName("number") 204 .hasArg() 205 .isRequired() 206 .withLongOpt(COUNT_LONG) 207 .withDescription("the number of messages to process") 208 .create(COUNT); 209 210 Option verbose = OptionBuilder.withDescription("use verbose logging") 211 .withLongOpt(VERBOSE_LONG) 212 .create(VERBOSE); 213 214 Options options = new Options(); 215 216 options.addOption(config); 217 options.addOption(factory); 218 options.addOption(destination); 219 options.addOption(count); 220 options.addOption(verbose); 221 return options; 222 } 223 224 /*** 225 * Returns the command usage 226 * 227 * @return the command usage 228 */ 229 protected abstract String getUsage(); 230 231 /*** 232 * Prints the usage for the command, and exits with an error code 233 * 234 * @param options the command line options 235 * @param message message to display before the usage. May be 236 * <code>null</code> 237 */ 238 protected void usage(Options options, String message) { 239 if (message != null) { 240 System.err.println(message); 241 } 242 243 HelpFormatter formatter = new HelpFormatter(); 244 formatter.printHelp(getUsage(), options); 245 System.exit(1); 246 } 247 248 /*** 249 * Returns the value of the <code>jmscts.home</code> system property, 250 * defaulting to the value of <code>user.dir</code> if its not set 251 * 252 * @return the value of the <code>jmscts.home</code> system property 253 */ 254 protected String getHome() { 255 return System.getProperty("jmscts.home", 256 System.getProperty("user.dir")); 257 } 258 259 } 260

This page was automatically generated by Maven