]> git.argeo.org Git - gpl/argeo-slc.git/blob - 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
1 package org.argeo.slc.executionflow;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.argeo.slc.SlcException;
7 import org.springframework.aop.framework.ProxyFactory;
8 import org.springframework.beans.factory.BeanNameAware;
9
10 public class SimpleExecutionSpec implements ExecutionSpec, BeanNameAware {
11 private final static ThreadLocal<ExecutionFlow> initializingFlow = new ThreadLocal<ExecutionFlow>();
12
13 private Map<String, ExecutionSpecAttribute> attributes = new HashMap<String, ExecutionSpecAttribute>();
14
15 private String name = null;
16
17 public Map<String, ExecutionSpecAttribute> getAttributes() {
18 return attributes;
19 }
20
21 public void setAttributes(Map<String, ExecutionSpecAttribute> attributes) {
22 this.attributes = attributes;
23 }
24
25 public Object createRef(String name) {
26 ExecutionFlow flow = initializingFlow.get();
27 if (flow == null)
28 throw new SlcException("No flow is currently initializing."
29 + " Declare flow refs as inner beans or prototypes.");
30
31 RefSpecAttribute refSpecAttribute = (RefSpecAttribute) attributes
32 .get(name);
33 Class<?> targetClass = refSpecAttribute.getTargetClass();
34 ExecutionTargetSource targetSource = new ExecutionTargetSource(flow,
35 targetClass, name);
36 ProxyFactory proxyFactory = new ProxyFactory();
37 proxyFactory.setTargetClass(targetClass);
38 proxyFactory.setProxyTargetClass(true);
39 proxyFactory.setTargetSource(targetSource);
40
41 return proxyFactory.getProxy();
42 }
43
44 public void setBeanName(String name) {
45 this.name = name;
46 }
47
48 public String getName() {
49 return name;
50 }
51
52 public static void flowInitializationStarted(ExecutionFlow flow) {
53 initializingFlow.set(flow);
54 }
55
56 public static void flowInitializationFinished(ExecutionFlow flow) {
57 ExecutionFlow registeredFlow = initializingFlow.get();
58 if (registeredFlow == null)
59 throw new SlcException("No flow registered");
60 if (!flow.getUuid().equals(registeredFlow.getUuid()))
61 throw new SlcException("Current flow is " + flow);
62 initializingFlow.set(null);
63 }
64 }