X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.simple%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fexecution%2FDefaultExecutionFlow.java;h=16105e241ef0e4ea10a14270831d500bf9550a3d;hb=5ae9dc81ad1c3ddfa99a8456b0c5263dd483642d;hp=5232ae3a77fe1fd9bd131f5d6360c9f0dc3be501;hpb=9b2cc129a29b7446e2aa429a562ecb4e0f2142b7;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java index 5232ae3a7..16105e241 100644 --- a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java @@ -4,52 +4,66 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.UUID; -import org.apache.commons.lang.math.RandomUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.argeo.slc.SlcException; import org.argeo.slc.core.structure.tree.TreeSPath; import org.argeo.slc.core.structure.tree.TreeSRegistry; -import org.argeo.slc.execution.Executable; import org.argeo.slc.execution.ExecutionFlow; import org.argeo.slc.execution.ExecutionSpec; import org.argeo.slc.execution.ExecutionSpecAttribute; import org.argeo.slc.structure.StructureAware; import org.argeo.slc.structure.StructureRegistry; -import org.argeo.slc.test.ExecutableTestRun; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ResourceLoaderAware; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; import org.springframework.validation.MapBindingResult; public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean, - BeanNameAware { - private ExecutionSpec executionSpec = new DefaultExecutionSpec(); + BeanNameAware, StructureAware, ResourceLoaderAware { + + private final static Log log = LogFactory + .getLog(DefaultExecutionFlow.class); + + private final ExecutionSpec executionSpec; private String name = null; private Map parameters = new HashMap(); - private List executables = new ArrayList(); + private List executables = new ArrayList(); private String path; private StructureRegistry registry = new TreeSRegistry(); - public DefaultExecutionFlow() { + private ResourceLoader resourceLoader = null; + public DefaultExecutionFlow() { + this.executionSpec = new DefaultExecutionSpec(); } - public DefaultExecutionFlow(Map parameters) { - this.parameters.putAll(parameters); + public DefaultExecutionFlow(ExecutionSpec executionSpec) { + this.executionSpec = executionSpec; } - public void execute() { - for (Executable executable : executables) { - executable.execute(); + public DefaultExecutionFlow(ExecutionSpec executionSpec, + Map parameters) { + // be sure to have an execution spec + this.executionSpec = (executionSpec == null) ? new DefaultExecutionSpec() + : executionSpec; + + // only parameters contained in the executionSpec can be set + for (String parameter : parameters.keySet()) { + if (!executionSpec.getAttributes().containsKey(parameter)) { + throw new SlcException("Parameter " + parameter + + " is not defined in the ExecutionSpec"); + } } - } - public void afterPropertiesSet() throws Exception { - // Validate execution specs - if (executionSpec == null) - return; + // set the parameters + this.parameters.putAll(parameters); + // check that all the required parameters are defined MapBindingResult errors = new MapBindingResult(parameters, "execution#" + getName()); for (String key : executionSpec.getAttributes().keySet()) { @@ -70,34 +84,27 @@ public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean, errors.rejectValue(key, "Hidden but not set as parameter"); break; } - - /* - * if (!parameters.containsKey(key)) { Object defaultValue = - * attr.getValue(); if (defaultValue == null) - * errors.rejectValue(key, "Not set and no default value"); else - * parameters.put(key, defaultValue); } else {// contains key Object - * obj = parameters.get(key); if (attr instanceof RefSpecAttribute) - * { RefSpecAttribute rsa = (RefSpecAttribute) attr; // TODO: make - * sure this will not cause pb with OSGi Class targetClass = - * rsa.getTargetClass(); if - * (!targetClass.isAssignableFrom(obj.getClass())) { - * errors.reject(key + " not compatible with target class " + - * targetClass); } } } - */ } if (errors.hasErrors()) throw new SlcException("Could not prepare execution flow: " + errors.toString()); - if (path == null) { - path = "/" + executionSpec.getName() + "/" + name; + } + + public void run() { + for (Runnable executable : executables) { + executable.run(); } + } - for (Executable executable : executables) { - if (executable instanceof StructureAware) { - ((StructureAware) executable).notifyCurrentPath( - registry, new TreeSPath(path)); + public void afterPropertiesSet() throws Exception { + if (path != null) { + for (Runnable executable : executables) { + if (executable instanceof StructureAware) { + ((StructureAware) executable).notifyCurrentPath( + registry, new TreeSPath(path)); + } } } } @@ -106,14 +113,10 @@ public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean, this.name = name; } - public void setExecutables(List executables) { + public void setExecutables(List executables) { this.executables = executables; } - public void setExecutionSpec(ExecutionSpec executionSpec) { - this.executionSpec = executionSpec; - } - public void setParameters(Map attributes) { this.parameters = attributes; } @@ -126,23 +129,34 @@ public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean, return executionSpec; } - public Object getParameter(String name) { - if (parameters.containsKey(name)) { - return parameters.get(name); + public Object getParameter(String parameterName) { + // Verify that there is a spec attribute + ExecutionSpecAttribute specAttr = null; + if (executionSpec.getAttributes().containsKey(parameterName)) { + specAttr = executionSpec.getAttributes().get(parameterName); } else { - if (executionSpec.getAttributes().containsKey(name)) { - ExecutionSpecAttribute esa = executionSpec.getAttributes().get( - name); - if (esa.getValue() != null) - return esa.getValue(); + throw new SlcException("Key " + parameterName + + " is not defined in the specifications of " + toString()); + } + + if (parameters.containsKey(parameterName)) { + Object paramValue = parameters.get(parameterName); + if (specAttr instanceof ResourceSpecAttribute) { + // deal with resources + Resource resource = resourceLoader.getResource(paramValue + .toString()); + return ((ResourceSpecAttribute) specAttr) + .convertResource(resource); } else { - throw new SlcException("Key " + name - + " is not defined in the specifications of " - + toString()); + return paramValue; + } + } else { + if (specAttr.getValue() != null) { + return specAttr.getValue(); } } - throw new SlcException("Key " + name + " is not set as parameter in " - + toString()); + throw new SlcException("Key " + parameterName + + " is not set as parameter in " + toString()); } public Boolean isSetAsParameter(String key) { @@ -158,4 +172,28 @@ public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean, public boolean equals(Object obj) { return ((ExecutionFlow) obj).getName().equals(name); } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public void setRegistry(StructureRegistry registry) { + this.registry = registry; + } + + public void notifyCurrentPath(StructureRegistry registry, + TreeSPath path) { + if (this.path == null) { + this.path = path.toString(); + } + } + + public void setResourceLoader(ResourceLoader resourceLoader) { + this.resourceLoader = resourceLoader; + } + }