]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/AntSlcApplication.java
Simplify new runtime
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / ant / AntSlcApplication.java
index 3638340e16cb8fa3eaea8fa6b863e3597adc7b8c..1a69c493bf7509eac155dfbc3d4e4e10ab15f8da 100644 (file)
@@ -17,18 +17,21 @@ 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.DefaultSRegistry;
 import org.argeo.slc.core.structure.SimpleSElement;
 import org.argeo.slc.core.structure.StructureRegistry;
 import org.argeo.slc.core.structure.tree.TreeSPath;
 import org.argeo.slc.core.structure.tree.TreeSRegistry;
+import org.argeo.slc.runtime.SlcExecutionContext;
 import org.argeo.slc.runtime.SlcRuntime;
 import org.springframework.beans.factory.BeanDefinitionStoreException;
 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.FileSystemResource;
 import org.springframework.core.io.Resource;
 
 public class AntSlcApplication {
@@ -37,16 +40,53 @@ public class AntSlcApplication {
        private SlcRuntime slcRuntime;
 
        private Resource contextLocation;
+       private ConfigurableApplicationContext context;
 
        private Resource rootDir;
        private Resource confDir;
-       private Resource workDir;
+       private File workDir;
 
-       public void execute(SlcExecution slcExecution, Properties properties,
-                       Map<String, Object> references) {
+       public void init() {
+               try {
+                       try {
+                               if (rootDir != null)
+                                       System.setProperty(SlcAntConstants.ROOT_DIR_PROPERTY,
+                                                       rootDir.getURL().toString());
+                               if (confDir != null)
+                                       System.setProperty(SlcAntConstants.CONF_DIR_PROPERTY,
+                                                       confDir.getURL().toString());
+                       } catch (IOException e) {
+                               throw new SlcAntException("Cannot interpret dir as URL.", e);
+                       }
+                       if (workDir != null)
+                               System.setProperty(SlcAntConstants.WORK_DIR_PROPERTY, workDir
+                                               .toString());
+
+                       if (confDir != null && contextLocation == null) {
+                               contextLocation = confDir
+                                               .createRelative("applicationContext.xml");
+                       }
+
+                       GenericApplicationContext ctx = new GenericApplicationContext(
+                                       slcRuntime.getRuntimeContext());
+                       if (contextLocation != null && contextLocation.exists()) {
+                               XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
+                                               ctx);
+                               xmlReader.loadBeanDefinitions(contextLocation);
+                       }
+                       ctx.refresh();
+                       context = ctx;
+               } catch (Exception e) {
+                       throw new SlcAntException(
+                                       "Cannot create SLC app application context.", e);
+               }
+
+       }
+
+       public SlcExecutionContext execute(SlcExecution slcExecution,
+                       Properties properties, Map<String, Object> references) {
+               log.info("### Start SLC execution " + slcExecution.getUuid() + " ###");
                if (log.isDebugEnabled()) {
-                       log.debug("### Start SLC execution " + slcExecution.getUuid()
-                                       + " ###");
                        log.debug("rootDir=" + rootDir);
                        log.debug("confDir=" + confDir);
                        log.debug("workDir=" + workDir);
@@ -56,9 +96,10 @@ public class AntSlcApplication {
                Resource script = findAntScript(slcExecution);
                List<String> targets = findAntTargets(slcExecution);
 
-               ConfigurableApplicationContext ctx = createExecutionContext();
+               ConfigurableApplicationContext ctx = createExecutionContext(properties);
 
                Project project = new Project();
+               AntExecutionContext executionContext = new AntExecutionContext(project);
                project.addReference(SlcAntConstants.REF_ROOT_CONTEXT, ctx);
                project.addReference(SlcAntConstants.REF_SLC_EXECUTION, slcExecution);
                initProject(project, properties, references);
@@ -66,6 +107,10 @@ public class AntSlcApplication {
 
                initStructure(project, script);
                runProject(project, targets);
+
+               ctx.close();
+
+               return executionContext;
        }
 
        protected Resource findAntScript(SlcExecution slcExecution) {
@@ -93,10 +138,17 @@ public class AntSlcApplication {
                        if (script.exists())
                                return script;
 
-                       throw new SlcAntException("Cannot find Ant script " + scriptStr);
+                       script = new FileSystemResource(scriptStr);
+                       if (log.isTraceEnabled())
+                               log.trace("script(fs)=" + script);
+                       if (script.exists())
+                               return script;
+
                } catch (Exception e) {
                        throw new SlcAntException("Cannot find Ant script " + scriptStr, e);
                }
+
+               throw new SlcAntException("Cannot find Ant script " + scriptStr);
        }
 
        protected List<String> findAntTargets(SlcExecution slcExecution) {
@@ -112,20 +164,24 @@ public class AntSlcApplication {
                return targets;
        }
 
-       protected ConfigurableApplicationContext createExecutionContext() {
-               try {
-                       if (confDir != null && contextLocation == null) {
-                               contextLocation = confDir
-                                               .createRelative("applicationContext.xml");
-                       }
+       protected ConfigurableApplicationContext createExecutionContext(
+                       Properties userProperties) {
+               // Set user properties as system properties so that Spring can access
+               // them
+               for (Object key : userProperties.keySet()) {
+                       System.setProperty(key.toString(), userProperties.getProperty(key
+                                       .toString()));
+               }
+
+               if (System.getProperty(SlcAntConstants.DEFAULT_TEST_RUN_PROPERTY) == null) {
+                       System.setProperty(SlcAntConstants.DEFAULT_TEST_RUN_PROPERTY,
+                                       "defaultTestRun");
+               }
 
+               try {
                        GenericApplicationContext ctx = new GenericApplicationContext(
-                                       slcRuntime.getRuntimeContext());
-                       if (contextLocation != null && contextLocation.exists()) {
-                               XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
-                                               ctx);
-                               xmlReader.loadBeanDefinitions(contextLocation);
-                       }
+                                       context);
+                       ctx.refresh();
                        return ctx;
                } catch (Exception e) {
                        throw new SlcAntException(
@@ -206,7 +262,6 @@ public class AntSlcApplication {
                        }
                        log.debug("scriptPath=" + scriptPath);
 
-                       List<String> dirNames = new Vector<String>();
                        StringTokenizer st = new StringTokenizer(scriptPath, "/");
                        TreeSPath currPath = null;
                        while (st.hasMoreTokens()) {
@@ -234,30 +289,6 @@ public class AntSlcApplication {
                        throw new SlcAntException("Cannot inititalize execution structure",
                                        e);
                }
-
-               /*
-                * File rootDir = new File(project
-                * .getUserProperty(SlcAntConstants.ROOT_DIR_PROPERTY))
-                * .getAbsoluteFile(); File baseDir =
-                * project.getBaseDir().getAbsoluteFile(); List<File> dirs = new Vector<File>();
-                * File currentDir = baseDir; do { dirs.add(currentDir); currentDir =
-                * currentDir.getParentFile(); if (log.isTraceEnabled()) log.trace("List " +
-                * currentDir); } while (!currentDir.equals(rootDir.getParentFile())); //
-                * first path is root dir (because of previous algorithm) TreeSPath
-                * currPath = TreeSPath.createRootPath(rootDir.getName()); for (int i =
-                * dirs.size() - 1; i >= 0; i--) { File dir = dirs.get(i); // retrieves
-                * description for this path final String description; if (i == 0) {//
-                * project itself description = project.getDescription() != null &&
-                * !project.getDescription().equals("") ? project .getDescription() :
-                * project.getName(); } else { description = dir.getName(); if
-                * (log.isTraceEnabled()) log.trace("Dir desc " + i + "/" + dirs.size() + ": " +
-                * description); } SimpleSElement element = new
-                * SimpleSElement(description); // creates and register path if
-                * (!dir.equals(rootDir)) {// already set currPath =
-                * currPath.createChild(dir.getName()); } registry.register(currPath,
-                * element); } project.addReference(SlcAntConstants.REF_PROJECT_PATH,
-                * currPath);
-                */
        }
 
        protected void parseProject(Project project, Resource script) {
@@ -318,7 +349,7 @@ public class AntSlcApplication {
                this.confDir = confDir;
        }
 
-       public void setWorkDir(Resource workDir) {
+       public void setWorkDir(File workDir) {
                this.workDir = workDir;
        }