Hello Keelers, I'm attaching a patch I hope others will find useful. Also, I
wanted to point anyone interested to a Keel/Avalon Classification service
that makes use of Naive-Bayes type algorithms to classify text at
http://www.tempeststrings.com/naiban/index.shtml

-Patch to KeelOpenJMSServer: I've attached a patch to KeelOpenJMSServer to
fix a bug in the parsing of its args whereby it did not recognize jndi
parameters (jndiname, jndihost, etc...). I needed the bug fixed for a
project I'm currently working on that makes extensive use of Jakarta Commons
libraries. Since I already had Commons CLI code in my project, I removed
KeelOpenJMSServer's dependency on realityforge.cli and replaced it with a
dependency on Jakarta Commons' CLI package.


-Eric Simmerman
Index: KeelOpenJMSServer.java
===================================================================
RCS file: 
/cvsroot/keel/comm-openjms/src/java/org/keel/comm/openjms/servers/KeelOpenJMSServer.java,v
retrieving revision 1.5
diff -u -r1.5 KeelOpenJMSServer.java
--- KeelOpenJMSServer.java      3 Jul 2003 16:58:30 -0000       1.5
+++ KeelOpenJMSServer.java      16 Sep 2003 03:24:55 -0000
@@ -8,7 +8,9 @@
  */
 package org.keel.comm.openjms.servers;
 
+import java.io.IOException;
 import java.io.PrintStream;
+import java.net.MalformedURLException;
 import java.util.Hashtable;
 import java.util.List;
 
@@ -24,13 +26,15 @@
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 
-import org.realityforge.cli.CLArgsParser;
-import org.realityforge.cli.CLOption;
-import org.realityforge.cli.CLOptionDescriptor;
-import org.realityforge.cli.CLUtil;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+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.exolab.jms.jndi.JndiConstants;
 import org.exolab.jms.jndi.rmi.RmiJndiInitialContextFactory;
-import org.exolab.jms.util.CommandLine;
 import org.keel.servers.KeelAbstractServer;
 import org.keel.servers.jms.KeelJmsServer;
 import org.keel.services.model.ModelException;
@@ -50,7 +54,9 @@
  */
 public class KeelOpenJMSServer extends KeelAbstractServer {
 
-    private String[] args = null;
+    private CommandLine commandLine;
+
+         private String[] args = null;
 
     private boolean initialized = false;
 
@@ -71,38 +77,100 @@
     private Queue queue = null;
     private QueueReceiver receiver = null;
 
-    private static final int HELP_OPT = 'h';
-    private static final int VERSION_OPT = 'v';
-    private static final int ACKMODE_OPT = 'a';
-    
-    /**
-    *  Define the understood options. Each CLOptionDescriptor contains:
-    * - The "long" version of the option. Eg, "help" means that "--help" will
-    * be recognised.
-    * - The option flags, governing the option's argument(s).
-    * - The "short" version of the option. Eg, 'h' means that "-h" will be
-    * recognised.
-    * - A description of the option.
-    */
-    private static final CLOptionDescriptor[] options =
-        new CLOptionDescriptor[] {
-            new CLOptionDescriptor(
-                "help",
-                CLOptionDescriptor.ARGUMENT_DISALLOWED,
-                HELP_OPT,
-                "display help"),
-            new CLOptionDescriptor(
-                "version",
-                CLOptionDescriptor.ARGUMENT_DISALLOWED,
-                VERSION_OPT,
-                "display version information"),
-            new CLOptionDescriptor(
-                "ackmode",
-                CLOptionDescriptor.ARGUMENT_REQUIRED,
-                ACKMODE_OPT,
-                "Ack Mode")};
-
-    private CommandLine commandLine = null;
+    private static final String HELP_OPT = "?";
+    private static final String VERSION_OPT = "v";
+    private static final String ACKMODE_OPT = "a";
+    private static final String MODE_OPT = "m";    
+    private static final String URL_OPT = "u";    
+    private static final String SECURE_OPT = "s";    
+    private static final String PERS_MODE = "P";  
+      
+    private static final String JNDI_PORT = "p";
+    private static final String JNDI_HOST = "h";        
+    private static final String JNDI_NAME = "n";
+    private static final String COUNT_OPT = "c";    
+    private static final String TO_OPT = "t";    
+  
+    /** command-line option configuration. */
+    private static Options options = null;
+
+    /** Configure the option set. */
+    static
+    {
+      options = new Options();
+            
+      options.addOption( OptionBuilder
+                         .withLongOpt( "timeout" )
+                         .hasArgs(1)
+                         .withArgName("secods")
+                         .withDescription( "seconds to wait before exiting" )
+                         .create( TO_OPT ) );            
+            
+      options.addOption( OptionBuilder
+                         .withLongOpt( "count" )
+                         .hasArgs(1)
+                         .withArgName("num")
+                         .withDescription( "number of messages to wait for" )
+                         .create( COUNT_OPT ) );            
+            
+      options.addOption( OptionBuilder
+                         .withLongOpt( "jndiname" )
+                         .hasArgs(1)
+                         .withArgName("name")
+                         .withDescription( "name of the  jndi server" )
+                         .create( JNDI_NAME ) );            
+            
+      options.addOption( OptionBuilder
+                   .withLongOpt( "jndihost" )
+                   .hasArgs(1)
+                   .withArgName("host")
+                   .withDescription( "host where jndi server runs" )
+                   .create( JNDI_HOST ) );
+
+      options.addOption( OptionBuilder
+                   .withLongOpt( "jndiport" )
+                   .hasArgs(1)
+                   .withArgName("port")
+                   .withDescription( "port where the jndi server runs" )
+                   .create( JNDI_PORT ) );                                            
                   
+                                         
+      options.addOption( OptionBuilder
+                   .withLongOpt( "persistent" )
+                   .withDescription( "specifies persistent delivery mode" )
+                   .hasArgs(1)
+                   .create( PERS_MODE ) );                                         
+                                     
+      options.addOption( OptionBuilder
+                   .withLongOpt( "ackmode" )
+                  .withArgName("auto | [client] | dups")                  
+                  .hasArgs(1)                   
+                   .withDescription( "Initiate JMS Daemon (only applicable to JMS 
communication mode)" )                                 
+                   .create( ACKMODE_OPT ) );                                     
+                                 
+      options.addOption( OptionBuilder
+                   .withLongOpt( "secure" )
+                   .withDescription( "Use HTTPS instead of HTTP with HTTP mode")      
             
+                   .create( SECURE_OPT ) );                                 
+                                                                                      
     
+      options.addOption( OptionBuilder
+                   .withLongOpt( "url" )
+                   .hasArgs(1)
+                   .withArgName("url")
+                   .withDescription( "Specify the URL used for HTTP mode" )           
                      
+                   .create( URL_OPT ) );
+            
+      options.addOption( OptionBuilder
+                   .withLongOpt( "help" )
+                   .withDescription( "Display help information" )
+                   .create( HELP_OPT ) );                     
+                     
+      options.addOption( OptionBuilder
+                   .withLongOpt( "mode" )
+                   .withArgName("ipc | [rmi] | http")
+                   .hasArgs(1)
+                   .withDescription( "Connect using ipc, http or rmi mode" )
+                   .create( MODE_OPT ) );                                             
                                                                       
+    }
 
     /**
      * @see java.lang.Runnable#run()
@@ -139,121 +207,48 @@
 
     public void parseArgs() throws RuntimeException {
 
-        // Parse the arguments
-        CLArgsParser parser = new CLArgsParser(args, options);
-
-        if (null != parser.getErrorString()) {
-            getLogger().error("Error: " + parser.getErrorString());
-            return;
-        }
-
-        // Get a list of parsed options
-        List clOptions = parser.getArguments();
-        int size = clOptions.size();
-
-        for (int i = 1; i < size; i++) {
-            CLOption option = (CLOption) clOptions.get(i);
-
-            if (option == null) {
-                getLogger().warn("Option " + i + " was null!");
-                getLogger().warn("Id was " + option.getDescriptor().getId());
-                throw new RuntimeException(
-                    "Error parsing options: option " + i + " was null");
-            }
-
-            CLOptionDescriptor descriptor = option.getDescriptor();
-            if (descriptor == null) {
-                getLogger().warn("Option " + i + " had no descriptor!");
-                getLogger().warn("Id was " + option.getDescriptor().getId());
-                throw new RuntimeException(
-                    "Error parsing options: option " + i + "had no descriptor");
-            }
+      boolean done = false;
 
-            switch (descriptor.getId()) {
-                case CLOption.TEXT_ARGUMENT :
-                    System.out.println("Unknown arg: " + option.getArgument());
-                    break;
-
-                case HELP_OPT :
-                    System.err.println("Help");
-                    usage();
-                    break;
-
-                case ACKMODE_OPT :
-                    String amode = option.getArgument();
-
-                    if (amode.equals("auto")) {
-                        ackMode = Session.AUTO_ACKNOWLEDGE;
-                    } else if (amode.equals("dups")) {
-                        ackMode = Session.DUPS_OK_ACKNOWLEDGE;
-                    } else if (!amode.equals("client")) {
-                        // ignore all ack modes, to test no acking
-                        ackMode = -1;
-                    }
-
-                case VERSION_OPT :
-                    printVersion();
-                    break;
-            }
-        }
-    }
-
-    private CommandLine getCommandLine() {
-        if (commandLine == null) {
-            commandLine = new CommandLine(args);
-        }
-        return commandLine;
+      try
+      {
+          parse( args );
+      }
+      catch ( Exception e )
+      {
+          displayHelp();          
+          throw new RuntimeException(e);
+      }
+
+      if ( getCli().hasOption( HELP_OPT ) ) {
+          displayHelp();       
+      }
+
+      if ( getCli().hasOption( ACKMODE_OPT ) )
+      {                
+        String amode = getCli().getOptionValue( ACKMODE_OPT );
+        if (amode.equals("auto")) {
+            ackMode = Session.AUTO_ACKNOWLEDGE;
+        } else if (amode.equals("dups")) {
+            ackMode = Session.DUPS_OK_ACKNOWLEDGE;
+        } else if (!amode.equals("client")) {
+            // ignore all ack modes, to test no acking
+            ackMode = -1;
+        }        
+      }      
+      if ( getCli().hasOption( JNDI_PORT ) ) {
+        this.setJndiPort( getCli().getOptionValue(JNDI_PORT) );                
+      }
+      if ( getCli().hasOption( JNDI_HOST ) ) {
+        this.setJndiHost( getCli().getOptionValue(JNDI_HOST) );                
+      }
+      if ( getCli().hasOption( JNDI_NAME ) ) {
+        this.setJndiName( getCli().getOptionValue(JNDI_NAME) );                
+      }      
+      if ( getCli().hasOption( MODE_OPT ) ) {
+        this.setJndiMode( getCli().getOptionValue(MODE_OPT) );                
+      }                  
     }
 
-    static protected void usage() {
-        PrintStream out = System.out;
-
-        String lSep = System.getProperty("line.separator");
-        StringBuffer msg = new StringBuffer();
-        msg
-            
.append("------------------------------------------------------------------------ ")
-            .append(lSep);
-        msg.append("Keel JMS Server").append(lSep);
-        msg
-            .append("Usage: java " + KeelServer.class.getName() + " [options]")
-            .append(lSep)
-            .append(lSep);
-        msg.append("Options: ").append(lSep);
-        msg.append(CLUtil.describeOptions(options).toString());
-        System.out.println(msg.toString());
-        System.exit(0);
-
-        out.println(
-            "usage: java " + KeelServer.class.getName() + " [options]\n");
-        out.println("options:");
-        //out.println("  -topic <name>      topic to subscribe to.\n");
-        //out.println("  -name <consumer>   durable consumer name.\n");
-        out.println(
-            "  -mode <ipc | rmi | http>  "
-                + "connect using ipc, http or rmi mode. "
-                + "Defaults to 'rmi'.\n");
-        out.println(
-            "  -url <url> only used for http mode, the client passes\n"
-                + "              this url to the server, to allow the server\n"
-                + "              to invoke the clients servlet at the\n"
-                + "              specfified url. Defaults to "
-                + "'http://localhost:8080'.\n");
-        out.println(
-            "  -secure only used for http mode. Connect using https.\n"
-                + "   requires JSSE jars, https enabled Web server etc.\n");
-        out.println(
-            "  -ackmode <auto | client | dups>\n"
-                + "                     message acknowledgement mode. "
-                + "Defaults to 'client'.\n");
-        out.println(
-            "  -persistent        " + "specifies persistent delivery mode.\n");
-        out.println("  -jndiport <num>    port where the jndi server runs.\n");
-        out.println("  -jndihost <host>   host where jndi server runs.\n");
-        out.println("  -jndiname <name>   name of the jndi server\n");
-        out.println("  -count <num>       number of messages to wait for.\n");
-        out.println("  -timeout <seconds> seconds to wait before exiting.\n");
-        out.println("  -help              displays this screen.\n");
-    }
 
     private static void printVersion() {
         System.out.println("0.1");
@@ -409,7 +404,7 @@
 
             String queue_name = "keel.request";
             try {
-                if (getCommandLine().exists("persistent")) {
+                if (getCli().hasOption("persistent")) {
                     queue = (Queue) getNamingContext().lookup(queue_name);
                 } else {
                     queue = qSession.createQueue(queue_name);
@@ -425,7 +420,7 @@
             }
 
             try {
-                if (getCommandLine().exists("selector")) {
+                if (getCli().hasOption("selector")) {
                     receiver = qSession.createReceiver(queue, "JMSPriority=1");
                 } else {
                     receiver = qSession.createReceiver(queue);
@@ -459,8 +454,8 @@
      */
     protected String getJndiHost() {
         if (jndiHost == null) {
-            if (getCommandLine().exists("jndihost")) {
-                jndiHost = getCommandLine().value("jndihost");
+            if (getCli().hasOption("jndihost")) {
+                jndiHost = getCli().getOptionValue("jndihost");
             } else {
                 jndiHost = "localhost";
             }
@@ -474,8 +469,8 @@
      */
     protected String getJndiMode() {
         if (jndiMode == null) {
-            if (getCommandLine().exists("mode")) {
-                jndiMode = getCommandLine().value("mode");
+            if (getCli().hasOption("mode")) {
+                jndiMode = getCli().getOptionValue("mode");
             } else {
                 jndiMode = "rmi";
             }
@@ -489,8 +484,8 @@
      */
     protected String getJndiName() {
         if (jndiName == null) {
-            if (getCommandLine().exists("jndiname")) {
-                jndiName = getCommandLine().value("jndiname");
+            if (getCli().hasOption("jndiname")) {
+                jndiName = getCli().getOptionValue("jndiname");
             } else {
                 jndiName = "JndiServer";
             }
@@ -504,14 +499,14 @@
      */
     protected String getJndiPort() {
         if (jndiPort == null) {
-            if (getCommandLine().exists("jndiport")) {
-                jndiPort = getCommandLine().value("jndiport");
+            if (getCli().hasOption("jndiport")) {
+                jndiPort = getCli().getOptionValue("jndiport");
             } else {
                 if (getJndiMode().equals("ipc")
                     || getJndiMode().equals("tcp")) {
                     jndiPort = "3035";
                 } else if (getJndiMode().equals("http")) {
-                    if (getCommandLine().exists("secure")) {
+                    if (getCli().hasOption("secure")) {
                         jndiPort = "8443";
                     } else {
                         jndiPort = "8080";
@@ -534,7 +529,7 @@
             if (getJndiMode().equals("ipc") || getJndiMode().equals("tcp")) {
                 jndiType = "tcp://";
             } else if (getJndiMode().equals("http")) {
-                if (getCommandLine().exists("secure")) {
+                if (getCli().hasOption("secure")) {
                     jndiType = "https://";;
                 } else {
                     jndiType = "http://";;
@@ -557,7 +552,7 @@
                 jndiModeType =
                     "org.exolab.jms.jndi.mipc.IpcJndiInitialContextFactory";
             } else if (getJndiMode().equals("http")) {
-                if (getCommandLine().exists("secure")) {
+                if (getCli().hasOption("secure")) {
                     jndiModeType =
                         "org.exolab.jms.jndi.http.SslHttpJndiInitialContextFactory";
                 } else {
@@ -577,8 +572,8 @@
      */
     protected String getJndiURL() {
         if (jndiURL == null) {
-            if (getCommandLine().exists("url")) {
-                jndiURL = getCommandLine().value("url");
+            if (getCli().hasOption("url")) {
+                jndiURL = getCli().getOptionValue("url");
             } else {
                 jndiURL = getJndiType() + "localhost:" + getJndiPort();
             }
@@ -646,5 +641,48 @@
     protected boolean isInitialized() {
         return initialized;
     }
+    
+  /** Display usage information based upon current
+   *  command-line option configuration.
+   */
+  public static void displayHelp()
+  {
+      HelpFormatter formatter = new HelpFormatter();        
+
+      System.out.println("");
+      formatter.printHelp( "java "+KeelServer.class.getName()+" [options]",
+                           "\nOptions:",
+                           options,
+                           "\n" );
+  }
+
+  public void parse( String[] args )
+    throws ParseException, IOException, MalformedURLException, Exception
+  {
+    CommandLineParser parser = new PosixParser();            
+    setCli( parser.parse( options, args ) );     
+  }  
+
+  /**
+   * Set the cli parser.
+   *
+   * @param commandLine The command line parser.
+   */
+  protected void setCli( CommandLine commandLine )
+  {
+      this.commandLine = commandLine;
+  }
+  
+  /**
+   * Get the CLI parser.
+   *
+   * @return CommandLine The command line parser.
+   */
+  protected CommandLine getCli()
+  {
+      return this.commandLine;
+  }
+
+    
 }
 

Reply via email to