You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2009/05/05 09:32:44 UTC

svn commit: r771608 - in /incubator/sling/trunk/launchpad/base: ./ src/main/java/org/apache/sling/launchpad/app/ src/main/java/org/apache/sling/launchpad/base/app/ src/main/java/org/apache/sling/launchpad/base/shared/ src/main/java/org/apache/sling/lau...

Author: fmeschbe
Date: Tue May  5 07:32:43 2009
New Revision: 771608

URL: http://svn.apache.org/viewvc?rev=771608&view=rev
Log:
SLING-953 Preparse command line in the Main class

Added:
    incubator/sling/trunk/launchpad/base/src/test/
    incubator/sling/trunk/launchpad/base/src/test/java/
    incubator/sling/trunk/launchpad/base/src/test/java/org/
    incubator/sling/trunk/launchpad/base/src/test/java/org/apache/
    incubator/sling/trunk/launchpad/base/src/test/java/org/apache/sling/
    incubator/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/
    incubator/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/
    incubator/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java   (with props)
Modified:
    incubator/sling/trunk/launchpad/base/pom.xml
    incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/app/Main.java
    incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/app/MainDelegate.java
    incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/Launcher.java
    incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/webapp/SlingServletDelegate.java

Modified: incubator/sling/trunk/launchpad/base/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/base/pom.xml?rev=771608&r1=771607&r2=771608&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/base/pom.xml (original)
+++ incubator/sling/trunk/launchpad/base/pom.xml Tue May  5 07:32:43 2009
@@ -214,6 +214,10 @@
             <version>1.0.0-v20070606</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
     </dependencies>
 
 </project>

Modified: incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/app/Main.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/app/Main.java?rev=771608&r1=771607&r2=771608&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/app/Main.java (original)
+++ incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/app/Main.java Tue May  5 07:32:43 2009
@@ -24,6 +24,8 @@
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.apache.sling.launchpad.base.shared.Launcher;
 import org.apache.sling.launchpad.base.shared.Loader;
@@ -49,12 +51,12 @@
     // The name of the environment variable to consult to find out
     // about sling.home
     private static final String ENV_SLING_HOME = "SLING_HOME";
-
+    
     public static void main(String[] args) {
         new Main(args);
     }
 
-    private final String[] commandLineArgs;
+    private final Map<String, String> commandLineArgs;
 
     private final String slingHome;
 
@@ -65,13 +67,12 @@
         // set the thread name
         super("Sling Terminator");
 
+        this.commandLineArgs = parseCommandLine(args);
+
         // sling.home from the command line or system properties, else default
-        String slingHome = getSlingHome(args);
+        this.slingHome = getSlingHome(commandLineArgs);
         info("Starting Sling in " + slingHome, null);
 
-        this.commandLineArgs = args;
-        this.slingHome = slingHome;
-
         Runtime.getRuntime().addShutdownHook(this);
 
         // ensure up-to-date launcher jar
@@ -209,6 +210,58 @@
     }
 
     /**
+     * Parses the command line arguments into a map of strings indexed by
+     * strings. This method suppports single character option names only at the
+     * moment. Each pair of an option name and its value is stored into the
+     * map. If a single dash '-' character is encountered the rest of the command
+     * line are interpreted as option names and are stored in the map unmodified
+     * as entries with the same key and value.
+     * <table>
+     * <tr><th>Command Line</th><th>Mapping</th></tr>
+     * <tr><td>x</td><td>x -> x</td></tr>
+     * <tr><td>-y z</td><td>y -> z</td></tr>
+     * <tr><td>-yz</td><td>y -> z</td></tr>
+     * <tr><td>-y -z</td><td>y -> y, z -> z</td></tr>
+     * <tr><td>-y x - -z a</td><td>y -> x, -z -> -z, a -> a</td></tr>
+     * </table>
+     * 
+     * @param args The command line to parse
+     * 
+     * @return The map of command line options and their values
+     */
+    static Map<String, String> parseCommandLine(String[] args) {
+        Map<String, String> commandLine = new HashMap<String, String>();
+        boolean readUnparsed = false;
+        for (int argc = 0; args != null && argc < args.length; argc++) {
+            String arg = args[argc];
+            
+            if (readUnparsed) {
+                commandLine.put(arg, arg);
+            } else if (arg.startsWith("-")) {
+                if (arg.length() == 1) {
+                   readUnparsed = true;
+                } else {
+                    String key = String.valueOf(arg.charAt(1));
+                    if (arg.length() > 2) {
+                        commandLine.put(key, arg.substring(2));
+                    } else {
+                        argc++;
+                        if (argc < args.length && !args[argc].startsWith("-")) {
+                            commandLine.put(key, args[argc]);
+                        } else {
+                            commandLine.put(key, key);
+                            argc--;
+                        }
+                    }
+                }
+            } else {
+                commandLine.put(arg, arg);
+            }
+        }
+        return commandLine;
+    }
+    
+    /**
      * Define the sling.home parameter implementing the algorithme defined on
      * the wiki page to find the setting according to this algorithm:
      * <ol>
@@ -221,34 +274,33 @@
      * @param args The command line arguments
      * @return The value to use for sling.home
      */
-    private static String getSlingHome(String[] args) {
+    private static String getSlingHome(Map<String, String> commandLine) {
         String source = null;
-        String slingHome = null;
 
-        for (int argc = 0; argc < args.length; argc++) {
-            String arg = args[argc];
-            if (arg.startsWith("-") && arg.length() == 2
-                && arg.charAt(1) == 'c') {
-                argc++;
-                if (argc < args.length) {
-                    source = "command line";
-                    slingHome = args[argc];
-                }
-                break;
-            }
-        }
+        String slingHome = commandLine.get("c");
+        if (slingHome != null) {
+            
+            source = "command line";
 
-        if (slingHome == null) {
+        } else {
+            
             slingHome = System.getProperty(SharedConstants.SLING_HOME);
             if (slingHome != null) {
+                
                 source = "system property sling.home";
+                
             } else {
+                
                 slingHome = System.getenv(ENV_SLING_HOME);
                 if (slingHome != null) {
+                    
                     source = "environment variable SLING_HOME";
+                    
                 } else {
+                    
                     source = "default";
                     slingHome = SharedConstants.SLING_HOME_DEFAULT;
+                    
                 }
             }
         }

Modified: incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/app/MainDelegate.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/app/MainDelegate.java?rev=771608&r1=771607&r2=771608&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/app/MainDelegate.java (original)
+++ incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/app/MainDelegate.java Tue May  5 07:32:43 2009
@@ -24,6 +24,7 @@
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import org.apache.felix.framework.Logger;
 import org.apache.sling.launchpad.base.impl.ClassLoaderResourceProvider;
@@ -111,7 +112,7 @@
         this.notifiable = notifiable;
     }
 
-    public void setCommandLine(String[] args) {
+    public void setCommandLine(Map<String, String> args) {
         commandLine = new HashMap<String, String>();
         commandLine.put(PROP_PORT, DEFAULT_PORT);
         parseCommandLine(args, commandLine);
@@ -127,7 +128,7 @@
 
         // parse the command line (exit in case of failure)
         if (commandLine == null) {
-            setCommandLine(new String[0]);
+            setCommandLine(new HashMap<String, String>());
         }
 
         // if sling.home was set on the command line, set it in the properties
@@ -209,24 +210,15 @@
      * Parses the command line in <code>args</code> and sets appropriate Sling
      * configuration options in the <code>props</code> map.
      */
-    private static void parseCommandLine(String[] args,
+    private static void parseCommandLine(Map<String, String> args,
             Map<String, String> props) {
-        for (int argc = 0; argc < args.length; argc++) {
-            String arg = args[argc];
-            if (arg.startsWith("-")) {
-
-                // require at least another character naming the option
-                if (arg.length() != 2) {
-                    usage("Missing option name", 1);
-                }
-
-                // option argument is following the current option
-                argc++;
-                String value = argc < args.length ? args[argc] : null;
 
-                switch (arg.charAt(1)) {
+        for (Entry<String, String> arg : args.entrySet()) {
+            if (arg.getKey().length() == 1) {
+                String value = arg.getValue();
+                switch (arg.getKey().charAt(0)) {
                     case 'l':
-                        if (value == null) {
+                        if (value == arg.getKey()) {
                             usage("Missing log level value", 1);
                             continue;
                         }
@@ -243,7 +235,7 @@
                         break;
 
                     case 'f':
-                        if (value == null) {
+                        if (value == arg.getKey()) {
                             usage("Missing log file value", 1);
                             continue;
                         } else if ("-".equals(value)) {
@@ -253,7 +245,7 @@
                         break;
 
                     case 'c':
-                        if (value == null) {
+                        if (value == arg.getKey()) {
                             usage("Missing directory value", 1);
                             continue;
                         }
@@ -261,7 +253,7 @@
                         break;
 
                     case 'p':
-                        if (value == null) {
+                        if (value == arg.getKey()) {
                             usage("Missing port value", 1);
                             continue;
                         }
@@ -275,7 +267,7 @@
                         break;
 
                     case 'a':
-                        if (value == null) {
+                        if (value == arg.getKey()) {
                             usage("Missing address value", 1);
                             continue;
                         }
@@ -289,6 +281,8 @@
                         usage("Unrecognized option " + arg, 1);
                         break;
                 }
+            } else {
+                usage("Unrecognized option " + arg, 1);
             }
         }
     }

Modified: incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/Launcher.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/Launcher.java?rev=771608&r1=771607&r2=771608&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/Launcher.java (original)
+++ incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/shared/Launcher.java Tue May  5 07:32:43 2009
@@ -18,6 +18,8 @@
  */
 package org.apache.sling.launchpad.base.shared;
 
+import java.util.Map;
+
 /**
  * The <code>Launcher</code> interface is implemented by the delegate classes
  * inside the Launcher JAR and are used by the actual Main class or servlet to
@@ -40,7 +42,7 @@
     /**
      * The commandline provided from the standalone launch case.
      */
-    public void setCommandLine(String[] args);
+    public void setCommandLine(Map<String, String> args);
 
     /**
      * Starts the framework and returns <code>true</code> if successfull.

Modified: incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/webapp/SlingServletDelegate.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/webapp/SlingServletDelegate.java?rev=771608&r1=771607&r2=771608&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/webapp/SlingServletDelegate.java (original)
+++ incubator/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/webapp/SlingServletDelegate.java Tue May  5 07:32:43 2009
@@ -158,7 +158,7 @@
         this.notifiable = notifiable;
     }
 
-    public void setCommandLine(String[] args) {
+    public void setCommandLine(Map<String, String> args) {
         // ignore this for now
     }
 

Added: incubator/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java?rev=771608&view=auto
==============================================================================
--- incubator/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java (added)
+++ incubator/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java Tue May  5 07:32:43 2009
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.launchpad.app;
+
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+public class MainTest extends TestCase {
+
+    public void test_parseCommandLine_null_args() {
+        String[] args = null;
+        Map<String, String> commandline = Main.parseCommandLine(args);
+        assertNotNull("commandline map must not be null", commandline);
+        assertTrue("commandline map must be empty", commandline.isEmpty());
+    }
+
+    public void test_parseCommandLine_empty_args() {
+        String[] args = {};
+        Map<String, String> commandline = Main.parseCommandLine(args);
+        assertNotNull("commandline map must not be null", commandline);
+        assertTrue("commandline map must be empty", commandline.isEmpty());
+    }
+    
+    public void test_parseCommandLine_single_dash() {
+        String[] args = { "-" };
+        Map<String, String> commandline = Main.parseCommandLine(args);
+        assertNotNull("commandline map must not be null", commandline);
+        assertTrue("commandline map must be empty", commandline.isEmpty());
+    }
+
+    public void test_parseCommandLine_single_arg_no_par() {
+        String[] args = { "-a" };
+        Map<String, String> commandline = Main.parseCommandLine(args);
+        assertNotNull("commandline map must not be null", commandline);
+        assertEquals("commandline map must have one entry", 1, commandline.size());
+        assertEquals("single argument must be " + args[0].charAt(1), String.valueOf(args[0].charAt(1)), commandline.keySet().iterator().next());
+        assertEquals("single argument value must be " + args[0].charAt(1), String.valueOf(args[0].charAt(1)), commandline.values().iterator().next());
+    }
+    
+    public void test_parseCommandLine_single_arg_with_par() {
+        String[] args = { "-a", "value" };
+        Map<String, String> commandline = Main.parseCommandLine(args);
+        assertNotNull("commandline map must not be null", commandline);
+        assertEquals("commandline map must have one entry", 1, commandline.size());
+        assertEquals("single argument must be " + args[0].charAt(1), String.valueOf(args[0].charAt(1)), commandline.keySet().iterator().next());
+        assertEquals("single argument value must be " + args[1], args[1], commandline.values().iterator().next());
+    }
+
+    public void test_parseCommandLine_two_args_no_par() {
+        String[] args = { "-a", "-b" };
+        Map<String, String> commandline = Main.parseCommandLine(args);
+        assertNotNull("commandline map must not be null", commandline);
+        assertEquals("commandline map must have two entries", 2, commandline.size());
+        assertEquals("argument a must a", "a", commandline.get("a"));
+        assertEquals("argument b must b", "b", commandline.get("b"));
+    }
+
+    public void test_parseCommandLine_two_args_first_par() {
+        String[] args = { "-a", "apar", "-b" };
+        Map<String, String> commandline = Main.parseCommandLine(args);
+        assertNotNull("commandline map must not be null", commandline);
+        assertEquals("commandline map must have two entries", 2, commandline.size());
+        assertEquals("argument a must apar", "apar", commandline.get("a"));
+        assertEquals("argument b must b", "b", commandline.get("b"));
+    }
+
+    public void test_parseCommandLine_two_args_second_par() {
+        String[] args = { "-a", "-b", "bpar" };
+        Map<String, String> commandline = Main.parseCommandLine(args);
+        assertNotNull("commandline map must not be null", commandline);
+        assertEquals("commandline map must have two entries", 2, commandline.size());
+        assertEquals("argument a must a", "a", commandline.get("a"));
+        assertEquals("argument b must bpar", "bpar", commandline.get("b"));
+    }
+
+    public void test_parseCommandLine_two_args_all_par() {
+        String[] args = { "-a", "apar", "-b", "bpar" };
+        Map<String, String> commandline = Main.parseCommandLine(args);
+        assertNotNull("commandline map must not be null", commandline);
+        assertEquals("commandline map must have two entries", 2, commandline.size());
+        assertEquals("argument a must apar", "apar", commandline.get("a"));
+        assertEquals("argument b must bpar", "bpar", commandline.get("b"));
+    }
+
+    public void test_parseCommandLine_three_args_with_dash() {
+        String[] args = { "-a", "apar", "-", "-b", "bpar" };
+        Map<String, String> commandline = Main.parseCommandLine(args);
+        assertNotNull("commandline map must not be null", commandline);
+        assertEquals("commandline map must have three entries", 3, commandline.size());
+        assertEquals("argument a must apar", "apar", commandline.get("a"));
+        assertEquals("argument -b must -b", "-b", commandline.get("-b"));
+        assertEquals("argument bpar must bpar", "bpar", commandline.get("bpar"));
+    }
+}

Propchange: incubator/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url