incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chet...@apache.org
Subject svn commit: r1541482 - in /sling/trunk/launchpad/base/src: main/java/org/apache/sling/launchpad/app/Main.java test/java/org/apache/sling/launchpad/app/MainTest.java
Date Wed, 13 Nov 2013 11:51:03 GMT
Author: chetanm
Date: Wed Nov 13 11:51:02 2013
New Revision: 1541482

URL: http://svn.apache.org/r1541482
Log:
SLING-3231 - Not able to set multiple framework property via command line args

Fixed support for passing multiple options like -Dkey=value

Modified:
    sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/app/Main.java
    sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java

Modified: sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/app/Main.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/app/Main.java?rev=1541482&r1=1541481&r2=1541482&view=diff
==============================================================================
--- sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/app/Main.java (original)
+++ sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/app/Main.java Wed
Nov 13 11:51:02 2013
@@ -488,7 +488,7 @@ public class Main {
      * <li>Default value <code>sling</code></li>
      * </ol>
      *
-     * @param args The command line arguments
+     * @param commandLine The command line arguments
      * @return The value to use for sling.home
      */
     private static String getSlingHome(Map<String, String> commandLine) {
@@ -643,12 +643,32 @@ public class Main {
                 } else {
                     String key = String.valueOf(arg.charAt(1));
                     if (arg.length() > 2) {
-                        commandLine.put(key, arg.substring(2));
+                        final String val;
+                        final int indexOfEq = arg.indexOf('=');
+                        if (indexOfEq != -1) {
+                            //Handle case -Da=b
+                            key = arg.substring(1, indexOfEq);
+                            val = arg.substring(indexOfEq + 1);
+                        } else {
+                            val = arg.substring(2);
+                        }
+                        commandLine.put(key, val);
                     } else {
                         argc++;
                         if (argc < args.length
                             && (args[argc].equals("-") || !args[argc].startsWith("-")))
{
-                            commandLine.put(key, args[argc]);
+                            String val = args[argc];
+
+                            //Special handling for -D a=b
+                            if(key.equals("D")){
+                                final int indexOfEq = val.indexOf('=');
+                                if (indexOfEq != -1) {
+                                    //Handle case -D a=b. Add key as Da
+                                    key = "D" + val.substring(0, indexOfEq);
+                                    val = val.substring(indexOfEq + 1);
+                                }
+                            }
+                            commandLine.put(key, val);
                         } else {
                             commandLine.put(key, key);
                             argc--;
@@ -667,7 +687,7 @@ public class Main {
         if (args.remove("h") != null) {
             System.out.println("usage: "
                 + Main.class.getName()
-                + " [ start | stop | status ] [ -j adr ] [ -l loglevel ] [ -f logfile ] [
-c slinghome ] [ -i launchpadhome ] [ -a address ] [ -p port ] { -D n=v } [ -h ]");
+                + " [ start | stop | status ] [ -j adr ] [ -l loglevel ] [ -f logfile ] [
-c slinghome ] [ -i launchpadhome ] [ -a address ] [ -p port ] { -Dn=v } [ -h ]");
 
             System.out.println("    start         listen for control connection (uses -j)");
             System.out.println("    stop          terminate running Apache Sling (uses -j)");
@@ -681,7 +701,9 @@ public class Main {
             System.out.println("    -p port       the port to listen to (default 8080)");
             System.out.println("    -r path       the root servlet context path for the http
service (default is /)");
             System.out.println("    -n            don't install the shutdown hook");
-            System.out.println("    -D n=v        sets property n to value v");
+            System.out.println("    -Dn=v         sets property n to value v. Make sure to
use this option *after* " +
+                                                  "the jar filename. The JVM also has a -D
option which has a " +
+                                                  "different meaning");
             System.out.println("    -h            prints this usage message");
 
             return true;
@@ -704,7 +726,7 @@ public class Main {
         final HashMap<String, String> props = new HashMap<String, String>();
         boolean errorArg = false;
         for (Entry<String, String> arg : rawArgs.entrySet()) {
-            if (arg.getKey().length() == 1) {
+            if (arg.getKey().length() == 1 || arg.getKey().startsWith("D")) {
                 String value = arg.getValue();
                 switch (arg.getKey().charAt(0)) {
                     case 'j':
@@ -798,9 +820,15 @@ public class Main {
                             errorArg = true;
                             continue;
                         }
-                        String[] parts = value.split("=");
-                        int valueIdx = (parts.length > 1) ? 1 : 0;
-                        props.put(parts[0], parts[valueIdx]);
+                        if (arg.getKey().length() > 1) {
+                            //Dfoo=bar arg.key=Dfoo and arg.value=bar
+                            props.put(arg.getKey().substring(1), arg.getValue());
+                        } else {
+                            //D foo=bar arg.key=D and arg.value=foo=bar
+                            String[] parts = value.split("=");
+                            int valueIdx = (parts.length > 1) ? 1 : 0;
+                            props.put(parts[0], parts[valueIdx]);
+                        }
                         break;
 
                     default:

Modified: sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java?rev=1541482&r1=1541481&r2=1541482&view=diff
==============================================================================
--- sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java
(original)
+++ sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java
Wed Nov 13 11:51:02 2013
@@ -24,7 +24,6 @@ import java.util.HashMap;
 import java.util.Map;
 
 import junit.framework.TestCase;
-
 import org.apache.sling.launchpad.base.shared.SharedConstants;
 
 public class MainTest extends TestCase {
@@ -63,11 +62,11 @@ public class MainTest extends TestCase {
         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());
+                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());
+                String.valueOf(args[0].charAt(1)),
+                commandline.values().iterator().next());
     }
 
     public void test_parseCommandLine_single_arg_with_par() {
@@ -128,7 +127,7 @@ public class MainTest extends TestCase {
         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());
+                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"));
@@ -360,6 +359,24 @@ public class MainTest extends TestCase {
         assertEquals(Boolean.TRUE.toString(), props1.get("sling.shutdown.hook"));
     }
 
+    public void test_converCommandLineArgs_multi_D() {
+        String[] args = {"-Da1=b1", "-Da2=b2"};
+        Map<String, String> commandline = Main.parseCommandLine(args);
+        Map<String, String> props = Main.convertCommandLineArgs(commandline);
+        assertEquals(2, props.size());
+        assertEquals("b1", props.get("a1"));
+        assertEquals("b2", props.get("a2"));
+    }
+
+    public void test_converCommandLineArgs_multi_D_with_space() {
+        String[] args = {"-D", "a1=b1", "-D", "a2=b2"};
+        Map<String, String> commandline = Main.parseCommandLine(args);
+        Map<String, String> props = Main.convertCommandLineArgs(commandline);
+        assertEquals(2, props.size());
+        assertEquals("b1", props.get("a1"));
+        assertEquals("b2", props.get("a2"));
+    }
+
     public void test_converCommandLineArgs_D() {
         Map<String, String> props = Main.convertCommandLineArgs(new HashMap<String,
String>() {
             {



Mime
View raw message