1 package org
.argeo
.slc
.runtime
;
3 import java
.util
.Stack
;
5 import org
.argeo
.api
.cms
.CmsLog
;
6 import org
.argeo
.slc
.SlcException
;
7 import org
.argeo
.slc
.execution
.ExecutionFlow
;
8 import org
.argeo
.slc
.execution
.ExecutionSpecAttribute
;
9 import org
.argeo
.slc
.execution
.RefSpecAttribute
;
10 import org
.argeo
.slc
.primitive
.PrimitiveSpecAttribute
;
11 import org
.argeo
.slc
.primitive
.PrimitiveUtils
;
13 /** Manage parameters that need to be set during the instantiation of a flow */
14 public class InstantiationManager
{
16 private final static CmsLog log
= CmsLog
17 .getLog(InstantiationManager
.class);
19 private ThreadLocal
<Stack
<ExecutionFlow
>> flowStack
= new ThreadLocal
<Stack
<ExecutionFlow
>>();
21 public Object
createRef(String name
) {
23 if ((flowStack
.get() == null) || flowStack
.get().empty()) {
24 throw new SlcException("No flow is currently initializing."
25 + " Declare ParameterRef as inner beans or prototypes.");
28 return getInitializingFlowParameter(name
);
31 public void flowInitializationStarted(ExecutionFlow flow
, String flowName
) {
32 // set the flow name if it is DefaultExecutionFlow
33 if (flow
instanceof DefaultExecutionFlow
) {
34 ((DefaultExecutionFlow
) flow
).setName(flowName
);
37 if (log
.isTraceEnabled())
38 log
.trace("Start initialization of " + flow
.hashCode() + " ("
39 + flow
+ " - " + flow
.getClass() + ")");
41 // log.info("# flowInitializationStarted " + flowName);
42 // create a stack for this thread if there is none
43 if (flowStack
.get() == null) {
44 flowStack
.set(new Stack
<ExecutionFlow
>());
46 flowStack
.get().push(flow
);
49 public void flowInitializationFinished(ExecutionFlow flow
, String flowName
) {
50 if (log
.isTraceEnabled())
51 log
.trace("Finish initialization of " + flow
.hashCode() + " ("
52 + flow
+ " - " + flow
.getClass() + ")");
54 if (flowStack
.get() != null) {
55 ExecutionFlow registeredFlow
= flowStack
.get().pop();
56 if (registeredFlow
!= null) {
57 if (!flow
.getName().equals(registeredFlow
.getName()))
58 throw new SlcException("Current flow is " + flow
);
59 // log.info("# flowInitializationFinished " + flowName);
60 // initializingFlow.set(null);
63 // happens for flows imported as services
64 log
.warn("flowInitializationFinished - Flow Stack is null");
68 protected ExecutionFlow
findInitializingFlowWithParameter(String key
) {
69 if ((flowStack
.get() == null) || flowStack
.get().empty())
70 throw new SlcException("No initializing flow available.");
72 // first look in the outer flow (that may override parameters)
73 for (int i
= 0; i
< flowStack
.get().size(); i
++) {
74 if (flowStack
.get().elementAt(i
).isSetAsParameter(key
)) {
75 return flowStack
.get().elementAt(i
);
78 throw new SlcException("Key " + key
+ " is not set as parameter in "
79 + flowStack
.get().firstElement().toString() + " (stack size="
80 + flowStack
.get().size() + ")");
84 public Object
getInitializingFlowParameter(String key
) {
85 return findInitializingFlowWithParameter(key
).getParameter(key
);
88 public Class
<?
> getInitializingFlowParameterClass(String key
) {
89 ExecutionSpecAttribute attr
= findInitializingFlowWithParameter(key
)
90 .getExecutionSpec().getAttributes().get(key
);
91 if (attr
instanceof RefSpecAttribute
)
92 return ((RefSpecAttribute
) attr
).getTargetClass();
93 else if (attr
instanceof PrimitiveSpecAttribute
) {
94 String type
= ((PrimitiveSpecAttribute
) attr
).getType();
95 Class
<?
> clss
= PrimitiveUtils
.typeAsClass(type
);
97 throw new SlcException("Cannot convert type " + type
104 public Boolean
isInFlowInitialization() {
105 return (flowStack
.get() != null) && !flowStack
.get().empty();