X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.agent%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fant%2FAntSlcApplication.java;h=37bdff0dce8f93f352dd6dbfaf4e8e5fb1306998;hb=e95a183611a40be7aca3233bb19b89bdc5043338;hp=ed214b49e0d38955a1b8028798522d7b79407404;hpb=8912b1cdb0b73547d70d2a2729ea075751f37f35;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/AntSlcApplication.java b/org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/AntSlcApplication.java index ed214b49e..37bdff0dc 100644 --- a/org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/AntSlcApplication.java +++ b/org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/AntSlcApplication.java @@ -20,6 +20,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; @@ -27,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; @@ -44,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; @@ -66,29 +67,32 @@ public class AntSlcApplication { log.debug("workDir=" + workDir); } - // Spring initialization - ConfigurableApplicationContext ctx = createExecutionContext(slcExecution); - // Ant coordinates String scriptRelativePath = findAntScript(slcExecution); List 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) { @@ -101,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 @@ -132,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 @@ -143,8 +146,23 @@ public class AntSlcApplication { } } } catch (Exception e) { - throw new SlcAntException("Cannot init system properties.", e); + throw new SlcException("Cannot init system properties.", e); + } + } + + /** + * 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( @@ -154,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()); @@ -200,23 +219,23 @@ public class AntSlcApplication { ctx.refresh(); return ctx; } catch (Exception e) { - throw new SlcAntException( + throw new SlcException( "Cannot create SLC execution application context.", e); } } protected String findAntScript(SlcExecution slcExecution) { String scriptStr = slcExecution.getAttributes().get( - SlcAntConstants.EXECATTR_ANT_FILE); + AntConstants.EXECATTR_ANT_FILE); if (scriptStr == null) - throw new SlcAntException("No Ant script provided"); + throw new SlcException("No Ant script provided"); return scriptStr; } protected List findAntTargets(SlcExecution slcExecution) { String targetList = slcExecution.getAttributes().get( - SlcAntConstants.EXECATTR_ANT_TARGETS); + AntConstants.EXECATTR_ANT_TARGETS); List targets = new Vector(); if (targetList != null) { StringTokenizer stTargets = new StringTokenizer(targetList, ","); @@ -245,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 listeners = context.getBeansOfType( - BuildListener.class, false, true); + Map 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 appenders = context.getBeansOfType( Appender.class, false, true); for (Appender appender : appenders.values()) { @@ -268,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 { @@ -279,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 { @@ -298,7 +318,7 @@ public class AntSlcApplication { defs.load(in); in.close(); } catch (IOException e) { - throw new SlcAntException("Cannot load task definitions", e); + throw new SlcException("Cannot load task definitions", e); } return defs; } @@ -306,7 +326,7 @@ public class AntSlcApplication { protected void initStructure(Project project, String scriptRelativePath) { // Init structure registry StructureRegistry registry = new TreeSRegistry(); - project.addReference(SlcAntConstants.REF_STRUCTURE_REGISTRY, registry); + project.addReference(AntConstants.REF_STRUCTURE_REGISTRY, registry); // Lowest levels StringTokenizer st = new StringTokenizer(scriptRelativePath, "/"); @@ -333,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); @@ -359,7 +379,7 @@ public class AntSlcApplication { projectHelper); projectHelper.parse(project, script.getURL()); } catch (Exception e) { - throw new SlcAntException("Could not parse project for script " + throw new SlcException("Could not parse project for script " + scriptRelativePath, e); } @@ -376,7 +396,7 @@ public class AntSlcApplication { } } catch (Throwable e) { exception = e; - throw new SlcAntException("SLC Ant execution failed", exception); + throw new SlcException("SLC Ant execution failed", exception); } finally { p.fireBuildFinished(exception); } @@ -398,7 +418,7 @@ public class AntSlcApplication { this.workDir = workDir; } - public void setParentContext(ApplicationContext runtimeContext) { + public void setParentContext(ConfigurableApplicationContext runtimeContext) { this.parentContext = runtimeContext; }