]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.runtime/src/org/argeo/slc/runtime/InstantiationManager.java
ecac292e23210707d9f7cdf7c047f400b03bfc1a
[gpl/argeo-slc.git] / org.argeo.slc.runtime / src / org / argeo / slc / runtime / InstantiationManager.java
1 package org.argeo.slc.runtime;
2
3 import java.util.Stack;
4
5 import org.argeo.api.cms.CmsLog;
6 import org.argeo.slc.SlcException;
7 import org.argeo.slc.execution.ExecutionFlow;
8 import org.argeo.slc.execution.ExecutionSpecAttribute;
9 import org.argeo.slc.execution.RefSpecAttribute;
10 import org.argeo.slc.primitive.PrimitiveSpecAttribute;
11 import org.argeo.slc.primitive.PrimitiveUtils;
12
13 /** Manage parameters that need to be set during the instantiation of a flow */
14 public class InstantiationManager {
15
16 private final static CmsLog log = CmsLog
17 .getLog(InstantiationManager.class);
18
19 private ThreadLocal<Stack<ExecutionFlow>> flowStack = new ThreadLocal<Stack<ExecutionFlow>>();
20
21 public Object createRef(String name) {
22
23 if ((flowStack.get() == null) || flowStack.get().empty()) {
24 throw new SlcException("No flow is currently initializing."
25 + " Declare ParameterRef as inner beans or prototypes.");
26 }
27
28 return getInitializingFlowParameter(name);
29 }
30
31 public void flowInitializationStarted(ExecutionFlow flow, String flowName) {
32 // set the flow name if it is DefaultExecutionFlow
33 if (flow instanceof DefaultExecutionFlow) {
34 ((DefaultExecutionFlow) flow).setName(flowName);
35 }
36
37 if (log.isTraceEnabled())
38 log.trace("Start initialization of " + flow.hashCode() + " ("
39 + flow + " - " + flow.getClass() + ")");
40
41 // log.info("# flowInitializationStarted " + flowName);
42 // create a stack for this thread if there is none
43 if (flowStack.get() == null) {
44 flowStack.set(new Stack<ExecutionFlow>());
45 }
46 flowStack.get().push(flow);
47 }
48
49 public void flowInitializationFinished(ExecutionFlow flow, String flowName) {
50 if (log.isTraceEnabled())
51 log.trace("Finish initialization of " + flow.hashCode() + " ("
52 + flow + " - " + flow.getClass() + ")");
53
54 if (flowStack.get() != null) {
55 ExecutionFlow registeredFlow = flowStack.get().pop();
56 if (registeredFlow != null) {
57 if (!flow.getName().equals(registeredFlow.getName()))
58 throw new SlcException("Current flow is " + flow);
59 // log.info("# flowInitializationFinished " + flowName);
60 // initializingFlow.set(null);
61 }
62 } else {
63 // happens for flows imported as services
64 log.warn("flowInitializationFinished - Flow Stack is null");
65 }
66 }
67
68 protected ExecutionFlow findInitializingFlowWithParameter(String key) {
69 if ((flowStack.get() == null) || flowStack.get().empty())
70 throw new SlcException("No initializing flow available.");
71
72 // first look in the outer flow (that may override parameters)
73 for (int i = 0; i < flowStack.get().size(); i++) {
74 if (flowStack.get().elementAt(i).isSetAsParameter(key)) {
75 return flowStack.get().elementAt(i);
76 }
77 }
78 throw new SlcException("Key " + key + " is not set as parameter in "
79 + flowStack.get().firstElement().toString() + " (stack size="
80 + flowStack.get().size() + ")");
81
82 }
83
84 public Object getInitializingFlowParameter(String key) {
85 return findInitializingFlowWithParameter(key).getParameter(key);
86 }
87
88 public Class<?> getInitializingFlowParameterClass(String key) {
89 ExecutionSpecAttribute attr = findInitializingFlowWithParameter(key)
90 .getExecutionSpec().getAttributes().get(key);
91 if (attr instanceof RefSpecAttribute)
92 return ((RefSpecAttribute) attr).getTargetClass();
93 else if (attr instanceof PrimitiveSpecAttribute) {
94 String type = ((PrimitiveSpecAttribute) attr).getType();
95 Class<?> clss = PrimitiveUtils.typeAsClass(type);
96 if (clss == null)
97 throw new SlcException("Cannot convert type " + type
98 + " to class.");
99 return clss;
100 } else
101 return null;
102 }
103
104 public Boolean isInFlowInitialization() {
105 return (flowStack.get() != null) && !flowStack.get().empty();
106 }
107 }