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