2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.runtime
;
18 import java
.util
.Stack
;
20 import org
.apache
.commons
.logging
.Log
;
21 import org
.apache
.commons
.logging
.LogFactory
;
22 import org
.argeo
.slc
.SlcException
;
23 import org
.argeo
.slc
.execution
.ExecutionFlow
;
24 import org
.argeo
.slc
.execution
.ExecutionSpecAttribute
;
25 import org
.argeo
.slc
.execution
.RefSpecAttribute
;
26 import org
.argeo
.slc
.primitive
.PrimitiveSpecAttribute
;
27 import org
.argeo
.slc
.primitive
.PrimitiveUtils
;
29 /** Manage parameters that need to be set during the instantiation of a flow */
30 public class InstantiationManager
{
32 private final static Log log
= LogFactory
33 .getLog(InstantiationManager
.class);
35 private ThreadLocal
<Stack
<ExecutionFlow
>> flowStack
= new ThreadLocal
<Stack
<ExecutionFlow
>>();
37 public Object
createRef(String name
) {
39 if ((flowStack
.get() == null) || flowStack
.get().empty()) {
40 throw new SlcException("No flow is currently initializing."
41 + " Declare ParameterRef as inner beans or prototypes.");
44 return getInitializingFlowParameter(name
);
47 public void flowInitializationStarted(ExecutionFlow flow
, String flowName
) {
48 // set the flow name if it is DefaultExecutionFlow
49 if (flow
instanceof DefaultExecutionFlow
) {
50 ((DefaultExecutionFlow
) flow
).setName(flowName
);
53 if (log
.isTraceEnabled())
54 log
.trace("Start initialization of " + flow
.hashCode() + " ("
55 + flow
+ " - " + flow
.getClass() + ")");
57 // log.info("# flowInitializationStarted " + flowName);
58 // create a stack for this thread if there is none
59 if (flowStack
.get() == null) {
60 flowStack
.set(new Stack
<ExecutionFlow
>());
62 flowStack
.get().push(flow
);
65 public void flowInitializationFinished(ExecutionFlow flow
, String flowName
) {
66 if (log
.isTraceEnabled())
67 log
.trace("Finish initialization of " + flow
.hashCode() + " ("
68 + flow
+ " - " + flow
.getClass() + ")");
70 if (flowStack
.get() != null) {
71 ExecutionFlow registeredFlow
= flowStack
.get().pop();
72 if (registeredFlow
!= null) {
73 if (!flow
.getName().equals(registeredFlow
.getName()))
74 throw new SlcException("Current flow is " + flow
);
75 // log.info("# flowInitializationFinished " + flowName);
76 // initializingFlow.set(null);
79 // happens for flows imported as services
80 log
.warn("flowInitializationFinished - Flow Stack is null");
84 protected ExecutionFlow
findInitializingFlowWithParameter(String key
) {
85 if ((flowStack
.get() == null) || flowStack
.get().empty())
86 throw new SlcException("No initializing flow available.");
88 // first look in the outer flow (that may override parameters)
89 for (int i
= 0; i
< flowStack
.get().size(); i
++) {
90 if (flowStack
.get().elementAt(i
).isSetAsParameter(key
)) {
91 return flowStack
.get().elementAt(i
);
94 throw new SlcException("Key " + key
+ " is not set as parameter in "
95 + flowStack
.get().firstElement().toString() + " (stack size="
96 + flowStack
.get().size() + ")");
100 public Object
getInitializingFlowParameter(String key
) {
101 return findInitializingFlowWithParameter(key
).getParameter(key
);
104 public Class
<?
> getInitializingFlowParameterClass(String key
) {
105 ExecutionSpecAttribute attr
= findInitializingFlowWithParameter(key
)
106 .getExecutionSpec().getAttributes().get(key
);
107 if (attr
instanceof RefSpecAttribute
)
108 return ((RefSpecAttribute
) attr
).getTargetClass();
109 else if (attr
instanceof PrimitiveSpecAttribute
) {
110 String type
= ((PrimitiveSpecAttribute
) attr
).getType();
111 Class
<?
> clss
= PrimitiveUtils
.typeAsClass(type
);
113 throw new SlcException("Cannot convert type " + type
120 public Boolean
isInFlowInitialization() {
121 return (flowStack
.get() != null) && !flowStack
.get().empty();