]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - sandbox/argeo.slc.executionflow/src/main/java/org/argeo/slc/executionflow/SimpleExecutionFlow.java
Introduce executor and event notifications
[gpl/argeo-slc.git] / sandbox / argeo.slc.executionflow / src / main / java / org / argeo / slc / executionflow / SimpleExecutionFlow.java
index d816419129577755264a300af367ca1d145ebd67..cb27e68401327d34e1342a0cf3408b9d9f9a4d53 100644 (file)
@@ -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> executionFlow = new ThreadLocal<ExecutionFlow>();
-
-       private ExecutionSpec executionSpec;
+public class SimpleExecutionFlow implements ExecutionFlow, InitializingBean,
+               BeanNameAware {
+       private ExecutionSpec executionSpec = new SimpleExecutionSpec();
+       private String name = null;
        private Map<String, Object> attributes = new HashMap<String, Object>();
        private List<Executable> executables = new ArrayList<Executable>();
-       
+
        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(attributes, "execution#"
+                               + getUuid());
+               for (String key : executionSpec.getAttributes().keySet()) {
+                       ExecutionSpecAttribute executionSpecAttr = executionSpec
+                                       .getAttributes().get(key);
+                       if (!attributes.containsKey(key)) {
+                               Object defaultValue = executionSpecAttr.getValue();
+                               if (defaultValue == null)
+                                       errors.rejectValue(key, "Not set and no default value");
+                               else
+                                       attributes.put(key, defaultValue);
+                       } else {// contains key
+                               Object obj = attributes.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<Executable> executables) {
@@ -48,10 +80,6 @@ public class SimpleExecutionFlow implements ExecutionFlow, InitializingBean {
                this.attributes = attributes;
        }
 
-       public static ExecutionFlow getCurrentExecutionFlow() {
-               return executionFlow.get();
-       }
-
        public Map<String, Object> getAttributes() {
                return attributes;
        }
@@ -60,4 +88,12 @@ public class SimpleExecutionFlow implements ExecutionFlow, InitializingBean {
                return uuid;
        }
 
+       public ExecutionSpec getExecutionSpec() {
+               return executionSpec;
+       }
+
+       public String toString() {
+               return new StringBuffer("Flow ").append(name).toString();// .append(" [#")
+               // .append(uuid).append(']').toString();
+       }
 }