]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/SlcMain.java
Integrate XML-based file exchanges.
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / cli / SlcMain.java
index 39ff6aa411ec11ec949e7a73dd83f487c2f21c8c..e46bc8434c07164252186f8015c7514ce151e2bc 100644 (file)
@@ -1,6 +1,8 @@
 package org.argeo.slc.cli;
 
-import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.util.Properties;
 
 import org.apache.commons.cli.CommandLine;
@@ -11,11 +13,12 @@ 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.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.ant.AntConstants;
 import org.argeo.slc.core.SlcException;
 import org.argeo.slc.logging.Log4jUtils;
-import org.springframework.core.io.FileSystemResource;
 
 public class SlcMain {
        public enum Mode {
@@ -27,7 +30,7 @@ public class SlcMain {
        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(
+                       .withArgName("mode").hasArg().withDescription(
                                        "SLC execution mode, one of: " + listModeValues()).create(
                                        'm');
 
@@ -36,9 +39,23 @@ public class SlcMain {
                        .withValueSeparator(',').withDescription(
                                        "use value for given property").create('p');
 
+       private final static Option propertiesOpt = OptionBuilder.withLongOpt(
+                       "properties").withArgName("properties file").hasArgs()
+                       .withValueSeparator(',').withDescription(
+                                       "load properties from file (-p has priority)").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;
 
@@ -48,13 +65,18 @@ public class SlcMain {
                options = new Options();
                options.addOption(modeOpt);
                options.addOption(scriptOpt);
+               options.addOption(targetsOpt);
                options.addOption(propertyOpt);
+               options.addOption(propertiesOpt);
+               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 {
 
@@ -63,10 +85,15 @@ public class SlcMain {
 
                        // Mode
                        String modeStr = cl.getOptionValue(modeOpt.getOpt());
-                       try {
-                               mode = Mode.valueOf(modeStr);
-                       } catch (IllegalArgumentException e) {
-                               throw new SlcException("Unrecognized mode '" + modeStr + "'", e);
+                       if (modeStr == null) {
+                               mode = Mode.single;
+                       } else {
+                               try {
+                                       mode = Mode.valueOf(modeStr);
+                               } catch (IllegalArgumentException e) {
+                                       throw new SlcException("Unrecognized mode '" + modeStr
+                                                       + "'", e);
+                               }
                        }
 
                        // Script
@@ -75,16 +102,30 @@ 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());
                        }
 
                        // Properties
+                       if (cl.hasOption(propertiesOpt.getOpt())) {
+                               for (String propertyFile : cl.getOptionValues(propertiesOpt
+                                               .getOpt())) {
+                                       loadPropertyFile(properties, propertyFile);
+                               }
+                       }
                        if (cl.hasOption(propertyOpt.getOpt())) {
                                for (String property : cl.getOptionValues(propertyOpt.getOpt())) {
                                        addProperty(properties, property);
                                }
                        }
 
+                       // Runtime
+                       if (cl.hasOption(runtimeOpt.getOpt())) {
+                               runtimeStr = cl.getOptionValue(runtimeOpt.getOpt());
+                       }
                } catch (ParseException e) {
                        System.err.println("Problem with command line arguments. "
                                        + e.getMessage());
@@ -102,16 +143,20 @@ public class SlcMain {
                initLogging(properties);
                if (log.isDebugEnabled()) {
                        log.debug("Mode: " + mode);
+                       if (runtimeStr != null)
+                               log.debug("Runtime: " + runtimeStr);
                        log.debug("User properties: " + properties);
                        if (script != null)
-                               log.debug("Script: " + script.getAbsolutePath());
+                               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);
                }
        }
 
@@ -129,7 +174,7 @@ public class SlcMain {
                return str.substring(0, str.length() - 2);
        }
 
-       private static void addProperty(Properties properties, String property) {
+       protected static void addProperty(Properties properties, String property) {
                int eqIndex = property.indexOf('=');
                if (eqIndex == 0)
                        throw new SlcException("Badly formatted property " + property);
@@ -142,18 +187,32 @@ public class SlcMain {
                } else {
                        properties.setProperty(property, "true");
                }
+       }
 
+       protected static void loadPropertyFile(Properties properties,
+                       String propertyFile) {
+               FileInputStream in = null;
+               try {
+                       in = new FileInputStream(propertyFile);
+                       properties.load(in);
+               } catch (Exception e) {
+                       throw new SlcException("Could not load proeprty file "
+                                       + propertyFile);
+               } finally {
+                       IOUtils.closeQuietly(in);
+               }
        }
 
        private static void initLogging(Properties userProperties) {
+               System.setProperty("log4j.defaultInitOverride", "true");
+
                // Add log4j user properties to System properties
-               for (String key : userProperties.stringPropertyNames()) {
+               for (Object obj : userProperties.keySet()) {
+                       String key = obj.toString();
                        if (key.startsWith("log4j.")) {
-                                       System.setProperty(key, userProperties.getProperty(key));
+                               System.setProperty(key, userProperties.getProperty(key));
                        }
                }
-               
-               System.setProperty("log4j.defaultInitOverride", "true");
                Log4jUtils.initLog4j(System.getProperty("log4j.configuration",
                                "classpath:" + BOOTSTRAP_LOG4J_CONFIG));
                log = LogFactory.getLog(SlcMain.class);