]> git.argeo.org Git - gpl/argeo-slc.git/blob - core/execution/InstantiationManager.java
Prepare next development cycle
[gpl/argeo-slc.git] / core / execution / InstantiationManager.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.core.execution;
18
19 import java.util.Stack;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.argeo.slc.SlcException;
24 import org.argeo.slc.execution.ExecutionFlow;
25 import org.argeo.slc.execution.ExecutionSpecAttribute;
26
27 /** Manage parameters that need to be set during the instantiation of a flow */
28 public class InstantiationManager {
29
30 private final static Log log = LogFactory
31 .getLog(InstantiationManager.class);
32
33 private ThreadLocal<Stack<ExecutionFlow>> flowStack = new ThreadLocal<Stack<ExecutionFlow>>();
34
35 public Object createRef(String name) {
36
37 if ((flowStack.get() == null) || flowStack.get().empty()) {
38 throw new SlcException("No flow is currently initializing."
39 + " Declare ParameterRef as inner beans or prototypes.");
40 }
41
42 return getInitializingFlowParameter(name);
43 }
44
45 public void flowInitializationStarted(ExecutionFlow flow, String flowName) {
46 // set the flow name if it is DefaultExecutionFlow
47 if (flow instanceof DefaultExecutionFlow) {
48 ((DefaultExecutionFlow) flow).setBeanName(flowName);
49 }
50
51 if (log.isTraceEnabled())
52 log.trace("Start initialization of " + flow.hashCode() + " ("
53 + flow + " - " + flow.getClass() + ")");
54
55 // log.info("# flowInitializationStarted " + flowName);
56 // create a stack for this thread if there is none
57 if (flowStack.get() == null) {
58 flowStack.set(new Stack<ExecutionFlow>());
59 }
60 flowStack.get().push(flow);
61 }
62
63 public void flowInitializationFinished(ExecutionFlow flow, String flowName) {
64 if (log.isTraceEnabled())
65 log.trace("Finish initialization of " + flow.hashCode() + " ("
66 + flow + " - " + flow.getClass() + ")");
67
68 if (flowStack.get() != null) {
69 ExecutionFlow registeredFlow = flowStack.get().pop();
70 if (registeredFlow != null) {
71 if (!flow.getName().equals(registeredFlow.getName()))
72 throw new SlcException("Current flow is " + flow);
73 // log.info("# flowInitializationFinished " + flowName);
74 // initializingFlow.set(null);
75 }
76 } else {
77 // happens for flows imported as services
78 log.warn("flowInitializationFinished - Flow Stack is null");
79 }
80 }
81
82 protected ExecutionFlow findInitializingFlowWithParameter(String key) {
83 if ((flowStack.get() == null) || flowStack.get().empty())
84 throw new SlcException("No initializing flow available.");
85
86 // first look in the outer flow (that may override parameters)
87 for (int i = 0; i < flowStack.get().size(); i++) {
88 if (flowStack.get().elementAt(i).isSetAsParameter(key)) {
89 return flowStack.get().elementAt(i);
90 }
91 }
92 throw new SlcException("Key " + key + " is not set as parameter in "
93 + flowStack.get().firstElement().toString() + " (stack size="
94 + flowStack.get().size() + ")");
95
96 }
97
98 public Object getInitializingFlowParameter(String key) {
99 return findInitializingFlowWithParameter(key).getParameter(key);
100 }
101
102 public Class<?> getInitializingFlowParameterClass(String key) {
103 ExecutionSpecAttribute attr = findInitializingFlowWithParameter(key)
104 .getExecutionSpec().getAttributes().get(key);
105 if (attr instanceof RefSpecAttribute)
106 return ((RefSpecAttribute) attr).getTargetClass();
107 else if (attr instanceof PrimitiveSpecAttribute) {
108 String type = ((PrimitiveSpecAttribute) attr).getType();
109 Class<?> clss = PrimitiveUtils.typeAsClass(type);
110 if (clss == null)
111 throw new SlcException("Cannot convert type " + type
112 + " to class.");
113 return clss;
114 } else
115 return null;
116 }
117
118 public Boolean isInFlowInitialization() {
119 return (flowStack.get() != null) && !flowStack.get().empty();
120 }
121 }