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
;
10 public class InstantiationManager
{
12 private final static Log log
= LogFactory
.getLog(InstantiationManager
.class);
14 private ThreadLocal
<Stack
<ExecutionFlow
> > flowStack
= new ThreadLocal
<Stack
<ExecutionFlow
> >();
16 public Object
createRef(String name
) {
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.");
23 return getInitializingFlowParameter(name
);
26 public void flowInitializationStarted(ExecutionFlow flow
, String flowName
) {
27 if (log
.isTraceEnabled())
28 log
.trace("Start initialization of " + flow
.hashCode() + " ("
29 + flow
+ " - " + flow
.getClass() + ")");
31 // set the flow name if it is DefaultExecutionFlow
32 if(flow
instanceof DefaultExecutionFlow
) {
33 ((DefaultExecutionFlow
) flow
).setBeanName(flowName
);
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
>());
41 flowStack
.get().push(flow
);
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);
57 public Object
getInitializingFlowParameter(String key
) {
58 if ((flowStack
.get() == null) || flowStack
.get().empty())
59 throw new SlcException("No initializing flow available.");
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
);
67 throw new SlcException("Key " + key
+ " is not set as parameter in "
68 + flowStack
.get().firstElement().toString() + " (stack size="+flowStack
.get().size()+")");
71 public Boolean
isInFlowInitialization() {
72 return (flowStack
.get() != null) && !flowStack
.get().empty();