]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/InstantiationManager.java
Introduce JMX agent
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / 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 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 if (log.isTraceEnabled())
30 log.trace("Start initialization of " + flow.hashCode() + " ("
31 + flow + " - " + flow.getClass() + ")");
32
33 // set the flow name if it is DefaultExecutionFlow
34 if (flow instanceof DefaultExecutionFlow) {
35 ((DefaultExecutionFlow) flow).setBeanName(flowName);
36 }
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 ExecutionFlow registeredFlow = flowStack.get().pop();
51 if (registeredFlow != null) {
52 if (!flow.getName().equals(registeredFlow.getName()))
53 throw new SlcException("Current flow is " + flow);
54 // log.info("# flowInitializationFinished " + flowName);
55 // initializingFlow.set(null);
56 }
57 }
58
59 protected ExecutionFlow findInitializingFlowWithParameter(String key) {
60 if ((flowStack.get() == null) || flowStack.get().empty())
61 throw new SlcException("No initializing flow available.");
62
63 // first look in the outer flow (that may override parameters)
64 for (int i = 0; i < flowStack.get().size(); i++) {
65 if (flowStack.get().elementAt(i).isSetAsParameter(key)) {
66 return flowStack.get().elementAt(i);
67 }
68 }
69 throw new SlcException("Key " + key + " is not set as parameter in "
70 + flowStack.get().firstElement().toString() + " (stack size="
71 + flowStack.get().size() + ")");
72
73 }
74
75 public Object getInitializingFlowParameter(String key) {
76 return findInitializingFlowWithParameter(key).getParameter(key);
77 }
78
79 public Class<?> getInitializingFlowParameterClass(String key) {
80 ExecutionSpecAttribute attr = findInitializingFlowWithParameter(key)
81 .getExecutionSpec().getAttributes().get(key);
82 if (attr instanceof RefSpecAttribute)
83 return ((RefSpecAttribute) attr).getTargetClass();
84 else if (attr instanceof PrimitiveSpecAttribute)
85 return ((PrimitiveSpecAttribute) attr).getTypeAsClass();
86 else
87 return null;
88 }
89
90 public Boolean isInFlowInitialization() {
91 return (flowStack.get() != null) && !flowStack.get().empty();
92 }
93 }