]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/InstantiationManager.java
Remove eclipse project definitions
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / 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
10 public class InstantiationManager {
11
12 private final static Log log = LogFactory.getLog(InstantiationManager.class);
13
14 private ThreadLocal<Stack<ExecutionFlow> > flowStack = new ThreadLocal<Stack<ExecutionFlow> >();
15
16 public Object createRef(String name) {
17
18 if((flowStack.get() == null) || flowStack.get().empty()) {
19 throw new SlcException("No flow is currently initializing."
20 + " Declare ParameterRef as inner beans or prototypes.");
21 }
22
23 return getInitializingFlowParameter(name);
24 }
25
26 public void flowInitializationStarted(ExecutionFlow flow, String flowName) {
27 if (log.isTraceEnabled())
28 log.trace("Start initialization of " + flow.hashCode() + " ("
29 + flow + " - " + flow.getClass() + ")");
30
31 // set the flow name if it is DefaultExecutionFlow
32 if(flow instanceof DefaultExecutionFlow) {
33 ((DefaultExecutionFlow) flow).setBeanName(flowName);
34 }
35
36 // log.info("# flowInitializationStarted " + flowName);
37 // create a stack for this thread if there is none
38 if(flowStack.get() == null) {
39 flowStack.set(new Stack<ExecutionFlow>());
40 }
41 flowStack.get().push(flow);
42 }
43
44 public void flowInitializationFinished(ExecutionFlow flow, String flowName) {
45 if (log.isTraceEnabled())
46 log.trace("Finish initialization of " + flow.hashCode() + " ("
47 + flow + " - " + flow.getClass() + ")");
48 ExecutionFlow registeredFlow = flowStack.get().pop();
49 if (registeredFlow != null) {
50 if (!flow.getName().equals(registeredFlow.getName()))
51 throw new SlcException("Current flow is " + flow);
52 // log.info("# flowInitializationFinished " + flowName);
53 // initializingFlow.set(null);
54 }
55 }
56
57 public Object getInitializingFlowParameter(String key) {
58 if ((flowStack.get() == null) || flowStack.get().empty())
59 throw new SlcException("No initializing flow available.");
60
61 // first look in the outer flow (that may override parameters)
62 for(int i = 0; i < flowStack.get().size(); i++) {
63 if(flowStack.get().elementAt(i).isSetAsParameter(key)) {
64 return flowStack.get().elementAt(i).getParameter(key);
65 }
66 }
67 throw new SlcException("Key " + key + " is not set as parameter in "
68 + flowStack.get().firstElement().toString() + " (stack size="+flowStack.get().size()+")");
69 }
70
71 public Boolean isInFlowInitialization() {
72 return (flowStack.get() != null) && !flowStack.get().empty();
73 }
74 }