]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/SlcMain.java
Improve auto-detection of Spring beans.
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / cli / SlcMain.java
index 6184c5a7630a27b0b6dc1cca3099d92a5915ae48..10636f3d4191815a8a97d102dce1e10d8598828e 100644 (file)
@@ -1,6 +1,5 @@
 package org.argeo.slc.cli;
 
-import java.io.File;
 import java.util.Properties;
 
 import org.apache.commons.cli.CommandLine;
@@ -11,16 +10,19 @@ 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.springframework.core.io.FileSystemResource;
+import org.argeo.slc.logging.Log4jUtils;
 
 public class SlcMain {
        public enum Mode {
                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(
@@ -33,8 +35,17 @@ public class SlcMain {
                                        "use value for given property").create('p');
 
        private final static Option scriptOpt = OptionBuilder.withLongOpt("script")
-                       .withArgName("script").hasArg().withType(File.class)
-                       .withDescription("SLC script to execute").create('s');
+                       .withArgName("script").hasArg().withDescription(
+                                       "SLC script to execute").create('s');
+
+       private final static Option targetsOpt = OptionBuilder.withLongOpt(
+                       "targets").withArgName("targets").hasArg().withDescription(
+                       "Targets to execute").create('t');
+
+       private final static Option runtimeOpt = OptionBuilder.withLongOpt(
+                       "runtime").withArgName("runtime").hasArg().withDescription(
+                       "Runtime to use, either a full path or relative to slc app conf dir: "
+                                       + "<conf dir>/runtime/<runtime>/.xml").create('r');
 
        private final static Options options;
 
@@ -44,15 +55,20 @@ public class SlcMain {
                options = new Options();
                options.addOption(modeOpt);
                options.addOption(scriptOpt);
+               options.addOption(targetsOpt);
                options.addOption(propertyOpt);
+               options.addOption(runtimeOpt);
        }
 
        public static void main(String[] args) {
                Mode mode = null;
                Properties properties = new Properties();
-               File script = null;
+               String script = null;
+               String targets = null;
+               String runtimeStr = null;
 
                try {
+
                        CommandLineParser clParser = new GnuParser();
                        CommandLine cl = clParser.parse(options, args);
 
@@ -63,7 +79,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)) {
@@ -71,9 +86,12 @@ public class SlcMain {
                                        throw new SlcException("Mode " + Mode.single
                                                        + " requires option '" + scriptOpt.getLongOpt()
                                                        + "'");
-                               script = (File) cl.getOptionObject(scriptOpt.getOpt());
+                               script = cl.getOptionValue(scriptOpt.getOpt());
+
+                               // Targets
+                               if (cl.hasOption(targetsOpt.getOpt()))
+                                       targets = cl.getOptionValue(targetsOpt.getOpt());
                        }
-                       System.out.println("Script: " + script.getAbsolutePath());
 
                        // Properties
                        if (cl.hasOption(propertyOpt.getOpt())) {
@@ -81,20 +99,44 @@ public class SlcMain {
                                        addProperty(properties, property);
                                }
                        }
-                       System.out.print("Properties: " + properties);
+
+                       // Runtime
+                       if (cl.hasOption(runtimeOpt.getOpt())) {
+                               runtimeStr = cl.getOptionValue(runtimeOpt.getOpt());
+                       } else {
+                               runtimeStr = "default";
+                       }
+
                } 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("Runtime: " + runtimeStr);
+                       log.debug("User properties: " + properties);
+                       if (script != null)
+                               log.debug("Script: " + script);
+                       if (targets != null)
+                               log.debug("Targets: " + targets);
                }
 
+               // Execution
                if (mode.equals(Mode.single)) {
                        DefaultSlcRuntime runtime = new DefaultSlcRuntime();
-                       runtime.executeScript(new FileSystemResource(script), properties,
-                                       null);
+                       runtime.executeScript(runtimeStr, script, targets, properties,
+                                       null, null);
                }
        }
 
@@ -127,4 +169,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);
+       }
 }