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.argeo.slc.runtime.SlcExecutionOutput;
-import org.springframework.beans.factory.BeanDefinitionStoreException;
+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;
private final static Log log = LogFactory.getLog(AntSlcApplication.class);
private Resource contextLocation;
- private ApplicationContext parentContext;
+ private ConfigurableApplicationContext parentContext;
private Resource rootDir;
private Resource confDir;
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) {
}
}
- 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
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
}
}
} 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(
// 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());
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<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, ",");
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()) {
/** 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 {
}
}
Properties typedefs = getDefs(project,
- SlcAntConstants.SLC_TYPEDEFS_RESOURCE_PATH);
+ AntConstants.SLC_TYPEDEFS_RESOURCE_PATH);
for (Object o : typedefs.keySet()) {
String name = o.toString();
try {
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;
}
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, "/");
.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);
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);
}
}
} 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);
}
this.workDir = workDir;
}
- public void setParentContext(ApplicationContext runtimeContext) {
+ public void setParentContext(ConfigurableApplicationContext runtimeContext) {
this.parentContext = runtimeContext;
}