1 package org
.argeo
.slc
.core
.execution
;
3 import java
.util
.Stack
;
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
;
11 public class InstantiationManager
{
13 private final static Log log
= LogFactory
14 .getLog(InstantiationManager
.class);
16 private ThreadLocal
<Stack
<ExecutionFlow
>> flowStack
= new ThreadLocal
<Stack
<ExecutionFlow
>>();
18 public Object
createRef(String name
) {
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.");
25 return getInitializingFlowParameter(name
);
28 public void flowInitializationStarted(ExecutionFlow flow
, String flowName
) {
29 if (log
.isTraceEnabled())
30 log
.trace("Start initialization of " + flow
.hashCode() + " ("
31 + flow
+ " - " + flow
.getClass() + ")");
33 // set the flow name if it is DefaultExecutionFlow
34 if (flow
instanceof DefaultExecutionFlow
) {
35 ((DefaultExecutionFlow
) flow
).setBeanName(flowName
);
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
>());
43 flowStack
.get().push(flow
);
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);
59 protected ExecutionFlow
findInitializingFlowWithParameter(String key
) {
60 if ((flowStack
.get() == null) || flowStack
.get().empty())
61 throw new SlcException("No initializing flow available.");
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
);
69 throw new SlcException("Key " + key
+ " is not set as parameter in "
70 + flowStack
.get().firstElement().toString() + " (stack size="
71 + flowStack
.get().size() + ")");
75 public Object
getInitializingFlowParameter(String key
) {
76 return findInitializingFlowWithParameter(key
).getParameter(key
);
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();
90 public Boolean
isInFlowInitialization() {
91 return (flowStack
.get() != null) && !flowStack
.get().empty();