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;
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 {
private final static Log log = LogFactory.getLog(AntSlcApplication.class);
private Resource contextLocation;
- private ApplicationContext parentContext;
+ private ApplicationContext runtimeContext;
private Resource rootDir;
private Resource confDir;
}
// Spring initialization
+ initRuntimeContext(slcExecution);
ConfigurableApplicationContext ctx = createExecutionContext();
// Ant coordinates
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
}
}
+ 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) {
}
GenericApplicationContext ctx = new GenericApplicationContext(
- parentContext);
+ runtimeContext);
if (contextLocation != null && contextLocation.exists()) {
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
ctx);
}
}
+ 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) {
this.workDir = workDir;
}
- public void setParentContext(ApplicationContext parentContext) {
- this.parentContext = parentContext;
+ public void setRuntimeContext(ApplicationContext runtimeContext) {
+ this.runtimeContext = runtimeContext;
}
}
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
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);
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 {
"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;
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 {
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
}
}
+ // 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());
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);
}
}