X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.agent%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcli%2FSlcMain.java;h=fd6a402093ff21cd411996b79a464a97089a2278;hb=62fb595fc62eb935f582aecab5fb94dfe9bb5169;hp=6184c5a7630a27b0b6dc1cca3099d92a5915ae48;hpb=48d35a213a2b0d792b11b5df276158159105043b;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/SlcMain.java b/org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/SlcMain.java index 6184c5a76..fd6a40209 100644 --- a/org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/SlcMain.java +++ b/org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/SlcMain.java @@ -11,7 +11,10 @@ import org.apache.commons.cli.Option; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.argeo.slc.core.SlcException; +import org.argeo.slc.logging.Log4jUtils; import org.springframework.core.io.FileSystemResource; public class SlcMain { @@ -19,8 +22,9 @@ public class SlcMain { single, agent } - public final static String MODE_SINGLE = "single"; - public final static String MODE_AGENT = "agent"; + private static Log log = null; + + private final static String BOOTSTRAP_LOG4J_CONFIG = "org/argeo/slc/cli/bootstrapLog4j.properties"; private final static Option modeOpt = OptionBuilder.withLongOpt("mode") .withArgName("mode").hasArg().isRequired().withDescription( @@ -53,6 +57,7 @@ public class SlcMain { File script = null; try { + CommandLineParser clParser = new GnuParser(); CommandLine cl = clParser.parse(options, args); @@ -63,7 +68,6 @@ public class SlcMain { } catch (IllegalArgumentException e) { throw new SlcException("Unrecognized mode '" + modeStr + "'", e); } - System.out.println("Mode: " + mode); // Script if (mode.equals(Mode.single)) { @@ -73,7 +77,6 @@ public class SlcMain { + "'"); script = (File) cl.getOptionObject(scriptOpt.getOpt()); } - System.out.println("Script: " + script.getAbsolutePath()); // Properties if (cl.hasOption(propertyOpt.getOpt())) { @@ -81,16 +84,30 @@ public class SlcMain { addProperty(properties, property); } } - System.out.print("Properties: " + properties); + } catch (ParseException e) { System.err.println("Problem with command line arguments. " + e.getMessage()); - printUsage(); + badExit(); } catch (SlcException e) { System.err.println(e.getMessage()); - printUsage(); + badExit(); + } catch (Exception e) { + System.err.println("Unexpected exception when bootstrapping."); + e.printStackTrace(); + badExit(); } + // Initializes logging and log arguments + initLogging(properties); + if (log.isDebugEnabled()) { + log.debug("Mode: " + mode); + log.debug("User properties: " + properties); + if (script != null) + log.debug("Script: " + script.getAbsolutePath()); + } + + // Execution if (mode.equals(Mode.single)) { DefaultSlcRuntime runtime = new DefaultSlcRuntime(); runtime.executeScript(new FileSystemResource(script), properties, @@ -127,4 +144,24 @@ public class SlcMain { } } + + private static void initLogging(Properties userProperties) { + System.setProperty("log4j.defaultInitOverride", "true"); + + // Add log4j user properties to System properties + for (String key : userProperties.stringPropertyNames()) { + if (key.startsWith("log4j.")) { + System.setProperty(key, userProperties.getProperty(key)); + } + } + Log4jUtils.initLog4j(System.getProperty("log4j.configuration", + "classpath:" + BOOTSTRAP_LOG4J_CONFIG)); + log = LogFactory.getLog(SlcMain.class); + + } + + private static void badExit() { + printUsage(); + System.exit(1); + } }