]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Introduce runtime selection
authorMathieu Baudier <mbaudier@argeo.org>
Sat, 21 Jun 2008 13:13:32 +0000 (13:13 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sat, 21 Jun 2008 13:13:32 +0000 (13:13 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@1265 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/AntSlcApplication.java
org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/SlcAntConstants.java
org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/DefaultSlcRuntime.java
org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/SlcMain.java
org.argeo.slc.agent/src/test/java/org/argeo/slc/ant/SlcAntTest.java

index aac37a3b1fbe8b73ea771b8ead8edf56e9b508ff..68c7f18dede5117096a2b468fdc865a06ae184f8 100644 (file)
@@ -16,6 +16,7 @@ import org.apache.tools.ant.Project;
 import org.apache.tools.ant.ProjectHelper;
 import org.apache.tools.ant.helper.ProjectHelper2;
 import org.apache.tools.ant.listener.CommonsLoggingListener;
+import org.argeo.slc.core.SlcException;
 import org.argeo.slc.core.process.SlcExecution;
 import org.argeo.slc.core.structure.SimpleSElement;
 import org.argeo.slc.core.structure.StructureRegistry;
@@ -23,12 +24,15 @@ import org.argeo.slc.core.structure.tree.TreeSPath;
 import org.argeo.slc.core.structure.tree.TreeSRegistry;
 import org.argeo.slc.logging.Log4jUtils;
 import org.argeo.slc.runtime.SlcExecutionContext;
+import org.springframework.beans.factory.BeanDefinitionStoreException;
 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.core.io.DefaultResourceLoader;
 import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
 import org.springframework.util.SystemPropertyUtils;
 
 public class AntSlcApplication {
@@ -37,7 +41,7 @@ public class AntSlcApplication {
        private final static Log log = LogFactory.getLog(AntSlcApplication.class);
 
        private Resource contextLocation;
-       private ApplicationContext parentContext;
+       private ApplicationContext runtimeContext;
 
        private Resource rootDir;
        private Resource confDir;
@@ -59,6 +63,7 @@ public class AntSlcApplication {
                }
 
                // Spring initialization
+               initRuntimeContext(slcExecution);
                ConfigurableApplicationContext ctx = createExecutionContext();
 
                // Ant coordinates
@@ -81,32 +86,6 @@ public class AntSlcApplication {
                return executionContext;
        }
 
-       protected Resource findAntScript(SlcExecution slcExecution) {
-               String scriptStr = slcExecution.getAttributes().get(
-                               SlcAntConstants.EXECATTR_ANT_FILE);
-               if (scriptStr == null)
-                       throw new SlcAntException("No Ant script provided");
-
-               try {
-                       return rootDir.createRelative(scriptStr);
-               } catch (Exception e) {
-                       throw new SlcAntException("Cannot find Ant script " + scriptStr, e);
-               }
-       }
-
-       protected List<String> findAntTargets(SlcExecution slcExecution) {
-               String targetList = slcExecution.getAttributes().get(
-                               SlcAntConstants.EXECATTR_ANT_TARGETS);
-               List<String> targets = new Vector<String>();
-               if (targetList != null) {
-                       StringTokenizer stTargets = new StringTokenizer(targetList, ",");
-                       while (stTargets.hasMoreTokens()) {
-                               targets.add(stTargets.nextToken());
-                       }
-               }
-               return targets;
-       }
-
        protected void initSystemProperties(Properties userProperties) {
                // Set user properties as system properties so that Spring can access
                // them
@@ -163,6 +142,52 @@ public class AntSlcApplication {
                }
        }
 
+       protected void initRuntimeContext(SlcExecution slcExecution) {
+               if (runtimeContext == null) {
+                       Resource runtimeRes = null;
+                       String runtimeStr = slcExecution.getAttributes().get(
+                                       SlcAntConstants.EXECATTR_RUNTIME);
+                       if (runtimeStr != null) {
+                               try {
+                                       ResourceLoader rl = new DefaultResourceLoader(getClass()
+                                                       .getClassLoader());
+                                       try {
+                                               runtimeRes = rl.getResource(runtimeStr);
+                                       } catch (Exception e) {
+                                               // silent
+                                       }
+                                       if (runtimeRes == null || !runtimeRes.exists()) {
+                                               runtimeRes = confDir.createRelative("runtime/"
+                                                               + runtimeStr + ".xml");
+                                       }
+
+                                       if (runtimeRes.exists()) {
+                                               GenericApplicationContext ctx = new GenericApplicationContext();
+                                               XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
+                                                               ctx);
+                                               xmlReader.loadBeanDefinitions(runtimeRes);
+
+                                               // Add property place holder
+                                               PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
+                                               ppc.setIgnoreUnresolvablePlaceholders(true);
+                                               ctx.addBeanFactoryPostProcessor(ppc);
+
+                                               ctx.refresh();
+
+                                               runtimeContext = ctx;
+                                       }
+                               } catch (Exception e) {
+                                       throw new SlcException(
+                                                       "Could not initialize runtime context from "
+                                                                       + runtimeStr, e);
+                               }
+                       }
+
+                       if (runtimeContext == null)
+                               log.warn("No runtime is defined.");
+               }
+       }
+
        protected ConfigurableApplicationContext createExecutionContext() {
                try {
                        if (confDir != null && contextLocation == null) {
@@ -171,7 +196,7 @@ public class AntSlcApplication {
                        }
 
                        GenericApplicationContext ctx = new GenericApplicationContext(
-                                       parentContext);
+                                       runtimeContext);
                        if (contextLocation != null && contextLocation.exists()) {
                                XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
                                                ctx);
@@ -191,6 +216,32 @@ public class AntSlcApplication {
                }
        }
 
+       protected Resource findAntScript(SlcExecution slcExecution) {
+               String scriptStr = slcExecution.getAttributes().get(
+                               SlcAntConstants.EXECATTR_ANT_FILE);
+               if (scriptStr == null)
+                       throw new SlcAntException("No Ant script provided");
+
+               try {
+                       return rootDir.createRelative(scriptStr);
+               } catch (Exception e) {
+                       throw new SlcAntException("Cannot find Ant script " + scriptStr, e);
+               }
+       }
+
+       protected List<String> findAntTargets(SlcExecution slcExecution) {
+               String targetList = slcExecution.getAttributes().get(
+                               SlcAntConstants.EXECATTR_ANT_TARGETS);
+               List<String> targets = new Vector<String>();
+               if (targetList != null) {
+                       StringTokenizer stTargets = new StringTokenizer(targetList, ",");
+                       while (stTargets.hasMoreTokens()) {
+                               targets.add(stTargets.nextToken());
+                       }
+               }
+               return targets;
+       }
+
        protected void initProject(Project project, Properties properties,
                        Map<String, Object> references) {
                if (properties != null) {
@@ -351,8 +402,8 @@ public class AntSlcApplication {
                this.workDir = workDir;
        }
 
-       public void setParentContext(ApplicationContext parentContext) {
-               this.parentContext = parentContext;
+       public void setRuntimeContext(ApplicationContext runtimeContext) {
+               this.runtimeContext = runtimeContext;
        }
 
 }
index a170b1e33e095e51dda7eafea4bdf4e2aff665f1..2215af77db2af2bccb36b029d5d3da9add029a75 100644 (file)
@@ -22,6 +22,7 @@ public interface SlcAntConstants {
        public static final String EXECTYPE_SLC_ANT = "org.argeo.slc.ant";
        public static final String REF_SLC_EXECUTION = "slcExecution";
 
+       public final static String EXECATTR_RUNTIME = "slc.runtime";
        public final static String EXECATTR_ANT_FILE = "ant.file";
        public final static String EXECATTR_ANT_TARGETS = "ant.targets";
        /** Property for the root dir (SLC root property file). */
index ea046bb13e792b8c2bce1ead6307438aa0b6be52..e3ce331529a4af7e53490f7ccc9fabf4e14495cb 100644 (file)
@@ -27,8 +27,9 @@ public class DefaultSlcRuntime {
 
        public final static String SLC_ROOT_FILE_NAME = "slcRoot.properties";
 
-       public SlcExecutionContext executeScript(Resource script,
-                       Properties properties, Map<String, Object> references) {
+       public SlcExecutionContext executeScript(String runtimeStr,
+                       Resource script, String targets, Properties properties,
+                       Map<String, Object> references) {
 
                Resource slcRootFile = findSlcRootFile(script);
                String scriptRelativePath = SpringUtils.extractRelativePath(SpringUtils
@@ -36,8 +37,13 @@ public class DefaultSlcRuntime {
 
                SlcExecution slcExecution = createSlcExecution();
                slcExecution.setStatus(SlcExecution.STATUS_RUNNING);
+               slcExecution.getAttributes().put(SlcAntConstants.EXECATTR_RUNTIME,
+                               runtimeStr);
                slcExecution.getAttributes().put(SlcAntConstants.EXECATTR_ANT_FILE,
                                scriptRelativePath);
+               if (targets != null)
+                       slcExecution.getAttributes().put(
+                                       SlcAntConstants.EXECATTR_ANT_TARGETS, targets);
 
                AntSlcApplication application = getApplication(slcRootFile);
                return application.execute(slcExecution, properties, references);
index fd6a402093ff21cd411996b79a464a97089a2278..a2ffd17efe939d88e5862c34fabfba4f6f158fbb 100644 (file)
@@ -15,7 +15,9 @@ 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.DefaultResourceLoader;
 import org.springframework.core.io.FileSystemResource;
+import org.springframework.core.io.Resource;
 
 public class SlcMain {
        public enum Mode {
@@ -37,8 +39,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;
 
@@ -48,13 +59,17 @@ 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 {
 
@@ -75,7 +90,11 @@ 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
@@ -85,6 +104,13 @@ public class SlcMain {
                                }
                        }
 
+                       // 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());
@@ -102,15 +128,26 @@ public class SlcMain {
                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.getAbsolutePath());
+                               log.debug("Script: " + script);
+                       if (targets != null)
+                               log.debug("Targets: " + targets);
                }
 
                // Execution
                if (mode.equals(Mode.single)) {
+                       Resource scriptRes;
+                       if (new File(script).exists()) {
+                               scriptRes = new FileSystemResource(script);
+                       } else {
+                               scriptRes = new DefaultResourceLoader(SlcMain.class
+                                               .getClassLoader()).getResource(script);
+                       }
+
                        DefaultSlcRuntime runtime = new DefaultSlcRuntime();
-                       runtime.executeScript(new FileSystemResource(script), properties,
+                       runtime.executeScript(runtimeStr, scriptRes, targets, properties,
                                        null);
                }
        }
index 17f8a60c50f5ef5b4aed09e6fa6efe56adbc04a6..f58b8133d84f5ced64688a9cc1e40ef757980535 100644 (file)
@@ -7,14 +7,15 @@ import org.argeo.slc.unit.AbstractSpringTestCase;
 import org.springframework.core.io.FileSystemResource;\r
 \r
 public class SlcAntTest extends AbstractSpringTestCase {\r
-//     private Log log = LogFactory.getLog(getClass());\r
+       // private Log log = LogFactory.getLog(getClass());\r
 \r
        public void testSimpleRun() {\r
                AntSlcApplication slcApp = new AntSlcApplication();\r
                slcApp.setRootDir(new FileSystemResource(new File("src/test/resources")\r
                                .getAbsolutePath()\r
                                + File.separator));\r
-               slcApp.setParentContext(getContext());\r
+               slcApp.setWorkDir(new File(System.getProperty("java.io.tmpdir")));\r
+               slcApp.setRuntimeContext(getContext());\r
 \r
                SlcExecution slcExecution = new SlcExecution();\r
                slcExecution.getAttributes().put(SlcAntConstants.EXECATTR_ANT_FILE,\r