]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/AntSlcApplication.java
Restrcuture the location of the execution server
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / ant / AntSlcApplication.java
index 8b8d2e4e5e3f564e85f646851f167603c2d6d3fa..37bdff0dce8f93f352dd6dbfaf4e8e5fb1306998 100644 (file)
@@ -28,10 +28,10 @@ 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.SlcExecutionOutput;
+import org.springframework.beans.factory.BeanFactoryUtils;
 import org.springframework.beans.factory.ListableBeanFactory;
 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;
@@ -45,7 +45,7 @@ public class AntSlcApplication {
        private final static Log log = LogFactory.getLog(AntSlcApplication.class);
 
        private Resource contextLocation;
-       private ApplicationContext parentContext;
+       private ConfigurableApplicationContext parentContext;
 
        private Resource rootDir;
        private Resource confDir;
@@ -67,29 +67,32 @@ public class AntSlcApplication {
                        log.debug("workDir=" + workDir);
                }
 
-               // Spring initialization
-               ConfigurableApplicationContext ctx = createExecutionContext(slcExecution);
-
                // Ant coordinates
                String scriptRelativePath = findAntScript(slcExecution);
                List<String> targets = findAntTargets(slcExecution);
 
+               // Spring initialization
+               ConfigurableApplicationContext ctx = createExecutionContext(slcExecution);
+
                // Ant project initialization
                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);
-               parseProject(project, scriptRelativePath);
+               project.addReference(AntConstants.REF_ROOT_CONTEXT, ctx);
+               project.addReference(AntConstants.REF_SLC_EXECUTION, slcExecution);
 
-               // Execute project
-               initStructure(project, scriptRelativePath);
-               runProject(project, targets);
+               try {
+                       initProject(project, properties, references);
+                       parseProject(project, scriptRelativePath);
 
-               if (executionOutput != null)
-                       executionOutput.postExecution(executionContext);
+                       // Execute project
+                       initStructure(project, scriptRelativePath);
+                       runProject(project, targets);
 
-               ctx.close();
+                       if (executionOutput != null)
+                               executionOutput.postExecution(executionContext);
+               } finally {
+                       ctx.close();
+               }
        }
 
        protected void initSystemProperties(Properties userProperties) {
@@ -102,20 +105,18 @@ public class AntSlcApplication {
                        }
                }
 
-               if (System.getProperty(SlcAntConstants.DEFAULT_TEST_RUN_PROPERTY) == null) {
-                       System.setProperty(SlcAntConstants.DEFAULT_TEST_RUN_PROPERTY,
+               if (System.getProperty(AntConstants.DEFAULT_TEST_RUN_PROPERTY) == null) {
+                       System.setProperty(AntConstants.DEFAULT_TEST_RUN_PROPERTY,
                                        "defaultTestRun");
                }
 
                try {
                        if (rootDir != null)
-                               System.setProperty(SlcAntConstants.ROOT_DIR_PROPERTY, rootDir
-                                               .getURL().toString());
+                               setSystemPropertyForRes(AntConstants.ROOT_DIR_PROPERTY, rootDir);
                        if (confDir != null)
-                               System.setProperty(SlcAntConstants.CONF_DIR_PROPERTY, confDir
-                                               .getURL().toString());
+                               setSystemPropertyForRes(AntConstants.CONF_DIR_PROPERTY, confDir);
                        if (workDir != null)
-                               System.setProperty(SlcAntConstants.WORK_DIR_PROPERTY, workDir
+                               System.setProperty(AntConstants.WORK_DIR_PROPERTY, workDir
                                                .getCanonicalPath());
 
                        // Additional properties in slc.properties file. Already set sytem
@@ -133,7 +134,8 @@ public class AntSlcApplication {
                                                IOUtils.closeQuietly(in);
                                        }
 
-                                       for (String key : slcProperties.stringPropertyNames()) {
+                                       for (Object obj : slcProperties.keySet()) {
+                                               String key = obj.toString();
                                                if (!System.getProperties().containsKey(key)) {
                                                        String value = SystemPropertyUtils
                                                                        .resolvePlaceholders(slcProperties
@@ -148,6 +150,21 @@ public class AntSlcApplication {
                }
        }
 
+       /**
+        * Set property as an absolute file path if the resource can be located on
+        * the file system, or as an url.
+        */
+       private void setSystemPropertyForRes(String key, Resource res)
+                       throws IOException {
+               String value = null;
+               try {
+                       value = res.getFile().getCanonicalPath();
+               } catch (IOException e) {
+                       value = res.getURL().toString();
+               }
+               System.setProperty(key, value);
+       }
+
        protected ConfigurableApplicationContext createExecutionContext(
                        SlcExecution slcExecution) {
                try {
@@ -155,9 +172,10 @@ public class AntSlcApplication {
                        // Find runtime definition
                        Resource runtimeRes = null;
                        String runtimeStr = slcExecution.getAttributes().get(
-                                       SlcAntConstants.EXECATTR_RUNTIME);
+                                       AntConstants.EXECATTR_RUNTIME);
                        if (runtimeStr == null)
-                               runtimeStr = "default";
+                               runtimeStr = System.getProperty(AntConstants.RUNTIME_PROPERTY,
+                                               "default");
 
                        ResourceLoader rl = new DefaultResourceLoader(getClass()
                                        .getClassLoader());
@@ -208,7 +226,7 @@ public class AntSlcApplication {
 
        protected String findAntScript(SlcExecution slcExecution) {
                String scriptStr = slcExecution.getAttributes().get(
-                               SlcAntConstants.EXECATTR_ANT_FILE);
+                               AntConstants.EXECATTR_ANT_FILE);
                if (scriptStr == null)
                        throw new SlcException("No Ant script provided");
 
@@ -217,7 +235,7 @@ public class AntSlcApplication {
 
        protected List<String> findAntTargets(SlcExecution slcExecution) {
                String targetList = slcExecution.getAttributes().get(
-                               SlcAntConstants.EXECATTR_ANT_TARGETS);
+                               AntConstants.EXECATTR_ANT_TARGETS);
                List<String> targets = new Vector<String>();
                if (targetList != null) {
                        StringTokenizer stTargets = new StringTokenizer(targetList, ",");
@@ -246,16 +264,17 @@ public class AntSlcApplication {
                project.addBuildListener(new CommonsLoggingListener());
 
                ListableBeanFactory context = (ListableBeanFactory) project
-                               .getReference(SlcAntConstants.REF_ROOT_CONTEXT);
+                               .getReference(AntConstants.REF_ROOT_CONTEXT);
                // Register build listeners
-               Map<String, BuildListener> listeners = context.getBeansOfType(
-                               BuildListener.class, false, true);
+               Map<String, BuildListener> listeners = BeanFactoryUtils
+                               .beansOfTypeIncludingAncestors(context, BuildListener.class,
+                                               false, false);
                for (BuildListener listener : listeners.values()) {
                        project.addBuildListener(listener);
                }
 
                // Register log4j appenders from context
-               MDC.put(SlcAntConstants.MDC_ANT_PROJECT, project);
+               MDC.put(AntConstants.MDC_ANT_PROJECT, project);
                Map<String, Appender> appenders = context.getBeansOfType(
                                Appender.class, false, true);
                for (Appender appender : appenders.values()) {
@@ -269,7 +288,7 @@ public class AntSlcApplication {
        /** Loads the SLC specific Ant tasks. */
        protected void addCustomTaskAndTypes(Project project) {
                Properties taskdefs = getDefs(project,
-                               SlcAntConstants.SLC_TASKDEFS_RESOURCE_PATH);
+                               AntConstants.SLC_TASKDEFS_RESOURCE_PATH);
                for (Object o : taskdefs.keySet()) {
                        String name = o.toString();
                        try {
@@ -280,7 +299,7 @@ public class AntSlcApplication {
                        }
                }
                Properties typedefs = getDefs(project,
-                               SlcAntConstants.SLC_TYPEDEFS_RESOURCE_PATH);
+                               AntConstants.SLC_TYPEDEFS_RESOURCE_PATH);
                for (Object o : typedefs.keySet()) {
                        String name = o.toString();
                        try {
@@ -307,7 +326,7 @@ public class AntSlcApplication {
        protected void initStructure(Project project, String scriptRelativePath) {
                // Init structure registry
                StructureRegistry<TreeSPath> registry = new TreeSRegistry();
-               project.addReference(SlcAntConstants.REF_STRUCTURE_REGISTRY, registry);
+               project.addReference(AntConstants.REF_STRUCTURE_REGISTRY, registry);
 
                // Lowest levels
                StringTokenizer st = new StringTokenizer(scriptRelativePath, "/");
@@ -334,7 +353,7 @@ public class AntSlcApplication {
                                .getDescription() : projectPath.getName();
 
                registry.register(projectPath, new SimpleSElement(projectDesc));
-               project.addReference(SlcAntConstants.REF_PROJECT_PATH, projectPath);
+               project.addReference(AntConstants.REF_PROJECT_PATH, projectPath);
 
                if (log.isDebugEnabled())
                        log.debug("Project path: " + projectPath);
@@ -399,7 +418,7 @@ public class AntSlcApplication {
                this.workDir = workDir;
        }
 
-       public void setParentContext(ApplicationContext runtimeContext) {
+       public void setParentContext(ConfigurableApplicationContext runtimeContext) {
                this.parentContext = runtimeContext;
        }