1 package org
.argeo
.slc
.runtime
;
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
;
10 import org
.argeo
.slc
.execution
.RefSpecAttribute
;
11 import org
.argeo
.slc
.primitive
.PrimitiveSpecAttribute
;
12 import org
.argeo
.slc
.primitive
.PrimitiveUtils
;
14 /** Manage parameters that need to be set during the instantiation of a flow */
15 public class InstantiationManager
{
17 private final static Log log
= LogFactory
18 .getLog(InstantiationManager
.class);
20 private ThreadLocal
<Stack
<ExecutionFlow
>> flowStack
= new ThreadLocal
<Stack
<ExecutionFlow
>>();
22 public Object
createRef(String name
) {
24 if ((flowStack
.get() == null) || flowStack
.get().empty()) {
25 throw new SlcException("No flow is currently initializing."
26 + " Declare ParameterRef as inner beans or prototypes.");
29 return getInitializingFlowParameter(name
);
32 public void flowInitializationStarted(ExecutionFlow flow
, String flowName
) {
33 // set the flow name if it is DefaultExecutionFlow
34 if (flow
instanceof DefaultExecutionFlow
) {
35 ((DefaultExecutionFlow
) flow
).setName(flowName
);
38 if (log
.isTraceEnabled())
39 log
.trace("Start initialization of " + flow
.hashCode() + " ("
40 + flow
+ " - " + flow
.getClass() + ")");
42 // log.info("# flowInitializationStarted " + flowName);
43 // create a stack for this thread if there is none
44 if (flowStack
.get() == null) {
45 flowStack
.set(new Stack
<ExecutionFlow
>());
47 flowStack
.get().push(flow
);
50 public void flowInitializationFinished(ExecutionFlow flow
, String flowName
) {
51 if (log
.isTraceEnabled())
52 log
.trace("Finish initialization of " + flow
.hashCode() + " ("
53 + flow
+ " - " + flow
.getClass() + ")");
55 if (flowStack
.get() != null) {
56 ExecutionFlow registeredFlow
= flowStack
.get().pop();
57 if (registeredFlow
!= null) {
58 if (!flow
.getName().equals(registeredFlow
.getName()))
59 throw new SlcException("Current flow is " + flow
);
60 // log.info("# flowInitializationFinished " + flowName);
61 // initializingFlow.set(null);
64 // happens for flows imported as services
65 log
.warn("flowInitializationFinished - Flow Stack is null");
69 protected ExecutionFlow
findInitializingFlowWithParameter(String key
) {
70 if ((flowStack
.get() == null) || flowStack
.get().empty())
71 throw new SlcException("No initializing flow available.");
73 // first look in the outer flow (that may override parameters)
74 for (int i
= 0; i
< flowStack
.get().size(); i
++) {
75 if (flowStack
.get().elementAt(i
).isSetAsParameter(key
)) {
76 return flowStack
.get().elementAt(i
);
79 throw new SlcException("Key " + key
+ " is not set as parameter in "
80 + flowStack
.get().firstElement().toString() + " (stack size="
81 + flowStack
.get().size() + ")");
85 public Object
getInitializingFlowParameter(String key
) {
86 return findInitializingFlowWithParameter(key
).getParameter(key
);
89 public Class
<?
> getInitializingFlowParameterClass(String key
) {
90 ExecutionSpecAttribute attr
= findInitializingFlowWithParameter(key
)
91 .getExecutionSpec().getAttributes().get(key
);
92 if (attr
instanceof RefSpecAttribute
)
93 return ((RefSpecAttribute
) attr
).getTargetClass();
94 else if (attr
instanceof PrimitiveSpecAttribute
) {
95 String type
= ((PrimitiveSpecAttribute
) attr
).getType();
96 Class
<?
> clss
= PrimitiveUtils
.typeAsClass(type
);
98 throw new SlcException("Cannot convert type " + type
105 public Boolean
isInFlowInitialization() {
106 return (flowStack
.get() != null) && !flowStack
.get().empty();