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