]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - sandbox/argeo.slc.executionflow/src/main/java/org/argeo/slc/executionflow/SimpleExecutionSpec.java
Introduce executor and event notifications
[gpl/argeo-slc.git] / sandbox / argeo.slc.executionflow / src / main / java / org / argeo / slc / executionflow / SimpleExecutionSpec.java
index f8eb969b8ead89fd8996596ed25674d1ca4cd9d6..ce14efd592fb40e218b836bf67dbd92ab2020e2f 100644 (file)
@@ -3,11 +3,17 @@ package org.argeo.slc.executionflow;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.argeo.slc.SlcException;
 import org.springframework.aop.framework.ProxyFactory;
+import org.springframework.beans.factory.BeanNameAware;
+
+public class SimpleExecutionSpec implements ExecutionSpec, BeanNameAware {
+       private final static ThreadLocal<ExecutionFlow> initializingFlow = new ThreadLocal<ExecutionFlow>();
 
-public class SimpleExecutionSpec implements ExecutionSpec {
        private Map<String, ExecutionSpecAttribute> attributes = new HashMap<String, ExecutionSpecAttribute>();
 
+       private String name = null;
+
        public Map<String, ExecutionSpecAttribute> getAttributes() {
                return attributes;
        }
@@ -17,17 +23,42 @@ public class SimpleExecutionSpec implements ExecutionSpec {
        }
 
        public Object createRef(String name) {
-               RefSpecAttribute<Object> refSpecAttribute = (RefSpecAttribute<Object>) attributes
+               ExecutionFlow flow = initializingFlow.get();
+               if (flow == null)
+                       throw new SlcException("No flow is currently initializing."
+                                       + " Declare flow refs as inner beans or prototypes.");
+
+               RefSpecAttribute refSpecAttribute = (RefSpecAttribute) attributes
                                .get(name);
-               Class targetClass = refSpecAttribute.getTargetClass();
-               ExecutionTargetSource targetSource = new ExecutionTargetSource();
-               targetSource.setName(name);
-               targetSource.setTargetClass(targetClass);
+               Class<?> targetClass = refSpecAttribute.getTargetClass();
+               ExecutionTargetSource targetSource = new ExecutionTargetSource(flow,
+                               targetClass, name);
                ProxyFactory proxyFactory = new ProxyFactory();
                proxyFactory.setTargetClass(targetClass);
                proxyFactory.setProxyTargetClass(true);
                proxyFactory.setTargetSource(targetSource);
-               
+
                return proxyFactory.getProxy();
        }
+
+       public void setBeanName(String name) {
+               this.name = name;
+       }
+
+       public String getName() {
+               return name;
+       }
+
+       public static void flowInitializationStarted(ExecutionFlow flow) {
+               initializingFlow.set(flow);
+       }
+
+       public static void flowInitializationFinished(ExecutionFlow flow) {
+               ExecutionFlow registeredFlow = initializingFlow.get();
+               if (registeredFlow == null)
+                       throw new SlcException("No flow registered");
+               if (!flow.getUuid().equals(registeredFlow.getUuid()))
+                       throw new SlcException("Current flow is " + flow);
+               initializingFlow.set(null);
+       }
 }