Author: fmeschbe
Date: Tue May 5 12:34:09 2009
New Revision: 771696
URL: http://svn.apache.org/viewvc?rev=771696&view=rev
Log:
SLING-954 Consistent logging in MainDelagate, moved usage info (-h support) to
Main class and prevent inadvertent shutdown of MainDelegate
Modified:
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/test/java/org/apache/sling/launchpad/app/MainTest.java
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=771696&r1=771695&r2=771696&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 12:34:09 2009
@@ -69,6 +69,9 @@
this.commandLineArgs = parseCommandLine(args);
+ // support usage first
+ doHelp();
+
// check for control commands (might exit)
doControlCommand();
@@ -374,8 +377,30 @@
}
}
+ /** prints a simple usage plus optional error message and exists */
+ private void doHelp() {
+ if (commandLineArgs.remove("h") != null) {
+ System.out.println("usage: "
+ + Main.class.getName()
+ + " [ start | stop | status ] [ -j adr ] [ -l loglevel ] [ -f
logfile ] [ -c slinghome ] [ -a address ] [ -p port ] [ -h ]");
+
+ System.out.println(" start listen for control
connection (uses -j)");
+ System.out.println(" stop terminate running Sling
(uses -j)");
+ System.out.println(" start check whether Sling is
running (uses-j)");
+ System.out.println(" -j adr host and port to use for
control connection in the format '[host:]port' (default localhost:63000)");
+ System.out.println(" -l loglevel the initial loglevel (0..4,
FATAL, ERROR, WARN, INFO, DEBUG)");
+ System.out.println(" -f logfile the log file, \"-\" for
stdout (default logs/error.log)");
+ System.out.println(" -c slinghome the sling context directory
(default sling)");
+ System.out.println(" -a address the interfact to bind to
(use 0.0.0.0 for any) (not supported yet)");
+ System.out.println(" -p port the port to listen to
(default 8080)");
+ System.out.println(" -h prints this usage message");
+
+ System.exit(0);
+ }
+ }
+
private void doControlCommand() {
- String commandSocketSpec = commandLineArgs.get("j");
+ String commandSocketSpec = commandLineArgs.remove("j");
if ("j".equals(commandSocketSpec)) {
commandSocketSpec = null;
}
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=771696&r1=771695&r2=771696&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 12:34:09 2009
@@ -151,7 +151,7 @@
Logger logger = new Logger();
// Display port number on console, in case HttpService doesn't
- consoleInfo("HTTP server port: " + commandLine.get(PROP_PORT), null);
+ info("HTTP server port: " + commandLine.get(PROP_PORT), null);
// prevent tons of needless WARN from the framework
logger.setLogLevel(Logger.LOG_ERROR);
@@ -191,8 +191,7 @@
return true;
} catch (BundleException be) {
- log("Failed to Start OSGi framework");
- be.printStackTrace(System.err);
+ error("Failed to Start OSGi framework", be);
}
// we failed to start
@@ -219,7 +218,7 @@
switch (arg.getKey().charAt(0)) {
case 'l':
if (value == arg.getKey()) {
- usage("Missing log level value", 1);
+ terminate("Missing log level value", 1);
continue;
}
try {
@@ -236,7 +235,7 @@
case 'f':
if (value == arg.getKey()) {
- usage("Missing log file value", 1);
+ terminate("Missing log file value", 1);
continue;
} else if ("-".equals(value)) {
value = "";
@@ -246,7 +245,7 @@
case 'c':
if (value == arg.getKey()) {
- usage("Missing directory value", 1);
+ terminate("Missing directory value", 1);
continue;
}
props.put(SharedConstants.SLING_HOME, value);
@@ -254,7 +253,7 @@
case 'p':
if (value == arg.getKey()) {
- usage("Missing port value", 1);
+ terminate("Missing port value", 1);
continue;
}
try {
@@ -262,69 +261,35 @@
Integer.parseInt(value);
props.put(PROP_PORT, value);
} catch (RuntimeException e) {
- usage("Bad port: " + value, 1);
+ terminate("Bad port: " + value, 1);
}
break;
case 'a':
if (value == arg.getKey()) {
- usage("Missing address value", 1);
+ terminate("Missing address value", 1);
continue;
}
- log("Setting the address to bind to is not supported,
binding to 0.0.0.0");
+ info("Setting the address to bind to is not supported,
binding to 0.0.0.0", null);
break;
- case 'h':
- usage(null, 0);
-
default:
- usage("Unrecognized option " + arg, 1);
+ terminate("Unrecognized option " + arg.getKey(), 1);
break;
}
} else {
- usage("Unrecognized option " + arg, 1);
+ terminate("Unrecognized option " + arg.getKey(), 1);
}
}
}
- /** prints a simple usage plus optional error message and exists with code
*/
- private static void usage(String message, int code) {
- if (message != null) {
- log(message);
- log("");
- }
-
- log("usage: "
- + MainDelegate.class.getName()
- + " [ start | stop | status ] [ -j adr ] [ -l loglevel ] [ -f
logfile ] [ -c slinghome ] [ -a address ] [ -p port ] [ -h ]");
-
- log(" start listen for control connection (uses -j)");
- log(" stop terminate running Sling (uses -j)");
- log(" start check whether Sling is running (uses-j)");
- log(" -j adr host and port to use for control connection in
the format '[host:]port' (default localhost:63000)");
- log(" -l loglevel the initial loglevel (0..4, FATAL, ERROR, WARN,
INFO, DEBUG)");
- log(" -f logfile the log file, \"-\" for stdout (default
logs/error.log)");
- log(" -c slinghome the sling context directory (default sling)");
- log(" -a address the interfact to bind to (use 0.0.0.0 for any)
(not supported yet)");
- log(" -p port the port to listen to (default 8080)");
- log(" -h prints this usage message");
-
- // exiting now
- System.exit(code);
- }
-
- /** Writes the message to stderr output */
- private static void log(String message) {
- System.err.println(message);
- }
-
/** Converts the loglevel code to a loglevel string name */
private static String toLogLevel(int level) {
if (level >= 0 && level < logLevels.length) {
return logLevels[level];
}
- usage("Bad log level: " + level, 1);
+ terminate("Bad log level: " + level, 1);
return null;
}
@@ -338,7 +303,7 @@
}
}
- usage("Bad log level: " + level, 1);
+ terminate("Bad log level: " + level, 1);
return null;
}
@@ -355,11 +320,25 @@
// ---------- console logging
+ /** prints a simple usage plus optional error message and exists with code
*/
+ private static void terminate(String message, int code) {
+ if (message != null) {
+ error(message + " (use -h for more information)", null);
+ }
+
+ System.exit(code);
+ }
+
// emit an informational message to standard out
- private static void consoleInfo(String message, Throwable t) {
+ static void info(String message, Throwable t) {
log(System.out, "*INFO*", message, t);
}
+ // emit an error message to standard err
+ static void error(String message, Throwable t) {
+ log(System.err, "*ERROR*", message, t);
+ }
+
private static final DateFormat fmt = new SimpleDateFormat("dd.MM.yyyy
HH:mm:ss.SSS ");
// helper method to format the message on the correct output channel
Modified:
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=771696&r1=771695&r2=771696&view=diff
==============================================================================
---
incubator/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java
(original)
+++
incubator/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/app/MainTest.java
Tue May 5 12:34:09 2009
@@ -108,4 +108,12 @@
assertEquals("argument -b must -b", "-b", commandline.get("-b"));
assertEquals("argument bpar must bpar", "bpar",
commandline.get("bpar"));
}
+
+ public void test_parseCommandLine_single_arg_with_dash_par() {
+ String[] args = { "-a", "-" };
+ Map<String, String> commandline = Main.parseCommandLine(args);
+ assertNotNull("commandline map must not be null", commandline);
+ assertEquals("commandline map must have three entries", 1,
commandline.size());
+ assertEquals("argument a must -", "-", commandline.get("a"));
+ }
}