This patch adds commons-cli to project dependencies in project.xml.  CLI
is used in main() to parse the command line, and print usage and version
information.  -h, and -? print usage information.  -b allows a user to specify 
a different Boot URL, -n allows a user to specify the default domain for the
MBean server.

Index: project.xml
===================================================================
RCS file: /home/cvspublic/incubator-geronimo/project.xml,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 project.xml
--- project.xml	10 Aug 2003 20:09:43 -0000	1.1.1.1
+++ project.xml	11 Aug 2003 05:22:48 -0000
@@ -174,6 +174,12 @@
 
     <dependencies>
         <dependency>
+            <id>commons-cli</id>
+            <version>1.0</version>
+            <url>http://jakarta.apache.org/commons/cli/</url>
+        </dependency>
+
+        <dependency>
             <id>commons-logging</id>
             <version>1.0.3</version>
             <url>http://jakarta.apache.org/commons/logging/</url>
Index: modules/core/src/java/org/apache/geronimo/Main.java
===================================================================
RCS file: /home/cvspublic/incubator-geronimo/modules/core/src/java/org/apache/geronimo/Main.java,v
retrieving revision 1.1
diff -u -r1.1 Main.java
--- modules/core/src/java/org/apache/geronimo/Main.java	10 Aug 2003 20:42:53 -0000	1.1
+++ modules/core/src/java/org/apache/geronimo/Main.java	11 Aug 2003 05:22:49 -0000
@@ -58,6 +58,7 @@
 import java.net.URL;
 import java.net.MalformedURLException;
 import java.util.Iterator;
+import java.util.ResourceBundle;
 import java.util.Set;
 import javax.management.MBeanServer;
 import javax.management.MBeanServerFactory;
@@ -65,6 +66,11 @@
 import javax.management.ObjectName;
 import javax.management.loading.MLet;
 
+import org.apache.commons.cli.BasicParser;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
 import org.apache.log4j.Logger;
 
 /**
@@ -75,7 +81,8 @@
  */
 public class Main implements Runnable {
     private static final Logger log = Logger.getLogger("Geronimo");
-
+    private static ResourceBundle messages;
+    
     private String domainName;
     private URL bootURL;
 
@@ -83,7 +90,7 @@
     private ObjectName serverName;
     private ObjectName bootMLetName;
     private Set bootedMBeans;
-
+    
     public Main(String domainName, URL url) {
         this.domainName = domainName;
         bootURL = url;
@@ -221,14 +228,67 @@
      * @param args command line arguments
      */
     public static void main(String[] args) {
+
+        // Default name of MBean Server
+        String optDomainName = "default";
+        String optMletURL = "file:server/default/config/boot.mlet";
+
+        // Retrieve some messages
+        messages = ResourceBundle.getBundle( Main.class.getName() );
+        
+        // Build CLI Options
+        Options options = new Options();
+        options.addOption( "b", "boot", true, messages.getString("boot.describe"));
+        options.addOption( "n", "name", true, messages.getString("name.describe"));
+        options.addOption( "v", "version", false, messages.getString("version.describe"));
+        options.addOption( "h", "help", false, messages.getString("help.describe"));
+        options.addOption( "?", "help", false, messages.getString("help.describe"));
+
+        // Parse the Command Line
+        BasicParser parser = new BasicParser();
+        CommandLine line = null;
+        String parseMessage = "";
+        try {
+            line = parser.parse( options, args );
+        } catch (ParseException e1) {
+            parseMessage = e1.getMessage();
+        }
+
+        // If there is no command line, or a parse exception occured, or the help cmd line option was passed in, print usage
+        if( line == null || !parseMessage.equals("") || line.hasOption('h') || line.hasOption('?')) {
+            System.out.println( parseMessage );
+            (new HelpFormatter()).printHelp(100, messages.getString("usage"), 
+                                                                        messages.getString("header"), 
+                                                                        options, 
+                                                                        messages.getString("footer"));
+            System.exit(0);
+        }
+        
+        // Print version information
+        if( line.hasOption('v')) {
+            System.out.println( messages.getString("product") + " " + messages.getString("version"));
+            System.exit(0);
+        }
+
+        // If option boot present, set URL
+        if( line.hasOption('b')) {
+            optMletURL = line.getOptionValue('b');
+        }
+        
+        // If option name present, set serverName
+        if( line.hasOption('n')) {
+            optDomainName = line.getOptionValue('n');
+        }
+        
+        // Create URL for Mlet config file and Create instance of Main
         URL url;
         try {
-            url = new URL("file:server/default/config/boot.mlet");
+            url = new URL(optMletURL);
         } catch (MalformedURLException e) {
             e.printStackTrace();
             return;
         }
-        Main main = new Main(args[0], url); // @todo you should not need to explicitly name the MBean Server
+        Main main = new Main(optDomainName, url);
 
         ThreadGroup group = new ThreadGroup("Geronimo");
         Thread mainThread = new Thread(group, main, "Main-Thread");
@@ -251,4 +311,6 @@
             }
         }
     }
+    
+    
 }
Index: modules/core/src/java/org/apache/geronimo/Main.properties
===================================================================
RCS file: modules/core/src/java/org/apache/geronimo/Main.properties
diff -N modules/core/src/java/org/apache/geronimo/Main.properties
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/core/src/java/org/apache/geronimo/Main.properties	11 Aug 2003 05:22:49 -0000
@@ -0,0 +1,9 @@
+usage=java <vm_args> org.apache.geronimo.Main [-b <mlet url>] [-h] [-n server-name]
+header=Apache Geronimo
+footer=Copyright (c) 2003 Apache Software Foundation.
+boot.describe=specify an alternate boot URL
+help.describe=list available command line options
+name.describe=specify the name the default domain name of the MBean server
+version.describe=show version number
+version=0.1
+product=Apache Geronimo

Reply via email to