X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=sandbox%2Fargeo.slc.executionflow%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fexecutionflow%2FSimpleExecutionFlow.java;h=225400e1b9e8f31ec857edf9ca6d9b78d5dd867b;hb=ca80f678fb454888fd5217cef21d42605171aaad;hp=d816419129577755264a300af367ca1d145ebd67;hpb=fd419cbd85435070ee575b4cf71f74be011d4282;p=gpl%2Fargeo-slc.git diff --git a/sandbox/argeo.slc.executionflow/src/main/java/org/argeo/slc/executionflow/SimpleExecutionFlow.java b/sandbox/argeo.slc.executionflow/src/main/java/org/argeo/slc/executionflow/SimpleExecutionFlow.java index d81641912..225400e1b 100644 --- a/sandbox/argeo.slc.executionflow/src/main/java/org/argeo/slc/executionflow/SimpleExecutionFlow.java +++ b/sandbox/argeo.slc.executionflow/src/main/java/org/argeo/slc/executionflow/SimpleExecutionFlow.java @@ -7,33 +7,65 @@ import java.util.Map; import java.util.UUID; import org.apache.commons.lang.math.RandomUtils; +import org.argeo.slc.SlcException; import org.argeo.slc.process.Executable; import org.argeo.slc.test.ExecutableTestRun; +import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.InitializingBean; +import org.springframework.validation.MapBindingResult; -public class SimpleExecutionFlow implements ExecutionFlow, InitializingBean { - private static ThreadLocal executionFlow = new ThreadLocal(); - - private ExecutionSpec executionSpec; - private Map attributes = new HashMap(); +public class SimpleExecutionFlow implements ExecutionFlow, InitializingBean, + BeanNameAware { + private ExecutionSpec executionSpec = new SimpleExecutionSpec(); + private String name = null; + private Map parameters = new HashMap(); private List executables = new ArrayList(); - + private final String uuid = UUID.randomUUID().toString(); public void execute() { - try { - executionFlow.set(this); - for (Executable executable : executables) { - executable.execute(); - } - } finally { - executionFlow.set(null); + for (Executable executable : executables) { + executable.execute(); } } public void afterPropertiesSet() throws Exception { - // TODO Auto-generated method stub + // Validate execution specs + if (executionSpec == null) + return; + + MapBindingResult errors = new MapBindingResult(parameters, "execution#" + + getUuid()); + for (String key : executionSpec.getAttributes().keySet()) { + ExecutionSpecAttribute executionSpecAttr = executionSpec + .getAttributes().get(key); + if (!parameters.containsKey(key)) { + Object defaultValue = executionSpecAttr.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 (executionSpecAttr instanceof RefSpecAttribute) { + RefSpecAttribute rsa = (RefSpecAttribute) executionSpecAttr; + 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()); + } + public void setBeanName(String name) { + this.name = name; } public void setExecutables(List executables) { @@ -44,20 +76,39 @@ public class SimpleExecutionFlow implements ExecutionFlow, InitializingBean { this.executionSpec = executionSpec; } - public void setAttributes(Map attributes) { - this.attributes = attributes; + public void setParameters(Map attributes) { + this.parameters = attributes; } - public static ExecutionFlow getCurrentExecutionFlow() { - return executionFlow.get(); + public String getUuid() { + return uuid; } - public Map getAttributes() { - return attributes; + public ExecutionSpec getExecutionSpec() { + return executionSpec; } - public String getUuid() { - return uuid; + public Object getParameter(String name) { + if (parameters.containsKey(name)) { + return parameters.get(name); + } else { + if (executionSpec.getAttributes().containsKey(name)) { + ExecutionSpecAttribute esa = executionSpec.getAttributes().get( + name); + if (esa.getValue() != null) + return esa.getValue(); + } else { + throw new SlcException("Key " + name + + " is not define in the specifications of " + + toString()); + } + } + throw new SlcException("Key " + name + " is not set as parameter in " + + toString()); } + public String toString() { + return new StringBuffer("Flow ").append(name).toString();// .append(" [#") + // .append(uuid).append(']').toString(); + } }