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 // set the flow name if it is DefaultExecutionFlow
30 if (flow
instanceof DefaultExecutionFlow
) {
31 ((DefaultExecutionFlow
) flow
).setBeanName(flowName
);
34 if (log
.isTraceEnabled())
35 log
.trace("Start initialization of " + flow
.hashCode() + " ("
36 + flow
+ " - " + flow
.getClass() + ")");
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() + ")");
51 if(flowStack
.get() != null) {
52 ExecutionFlow registeredFlow
= flowStack
.get().pop();
53 if (registeredFlow
!= null) {
54 if (!flow
.getName().equals(registeredFlow
.getName()))
55 throw new SlcException("Current flow is " + flow
);
56 // log.info("# flowInitializationFinished " + flowName);
57 // initializingFlow.set(null);
61 // happens for flows imported as services
62 log
.warn("flowInitializationFinished - Flow Stack is null");
66 protected ExecutionFlow
findInitializingFlowWithParameter(String key
) {
67 if ((flowStack
.get() == null) || flowStack
.get().empty())
68 throw new SlcException("No initializing flow available.");
70 // first look in the outer flow (that may override parameters)
71 for (int i
= 0; i
< flowStack
.get().size(); i
++) {
72 if (flowStack
.get().elementAt(i
).isSetAsParameter(key
)) {
73 return flowStack
.get().elementAt(i
);
76 throw new SlcException("Key " + key
+ " is not set as parameter in "
77 + flowStack
.get().firstElement().toString() + " (stack size="
78 + flowStack
.get().size() + ")");
82 public Object
getInitializingFlowParameter(String key
) {
83 return findInitializingFlowWithParameter(key
).getParameter(key
);
86 public Class
<?
> getInitializingFlowParameterClass(String key
) {
87 ExecutionSpecAttribute attr
= findInitializingFlowWithParameter(key
)
88 .getExecutionSpec().getAttributes().get(key
);
89 if (attr
instanceof RefSpecAttribute
)
90 return ((RefSpecAttribute
) attr
).getTargetClass();
91 else if (attr
instanceof PrimitiveSpecAttribute
) {
92 String type
= ((PrimitiveSpecAttribute
) attr
).getType();
93 Class
<?
> clss
= PrimitiveUtils
.typeAsClass(type
);
95 throw new SlcException("Cannot convert type " + type
102 public Boolean
isInFlowInitialization() {
103 return (flowStack
.get() != null) && !flowStack
.get().empty();