]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.spring/src/org/argeo/slc/core/execution/InstantiationManager.java
211fc4cf9fa8db366f743ffa3c4675f09a10a716
[gpl/argeo-slc.git] / org.argeo.slc.spring / src / org / argeo / slc / core / execution / InstantiationManager.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 package org.argeo.slc.core.execution;
17
18 import java.util.Stack;
19
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;
28
29 /** Manage parameters that need to be set during the instantiation of a flow */
30 public class InstantiationManager {
31
32 private final static Log log = LogFactory
33 .getLog(InstantiationManager.class);
34
35 private ThreadLocal<Stack<ExecutionFlow>> flowStack = new ThreadLocal<Stack<ExecutionFlow>>();
36
37 public Object createRef(String name) {
38
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.");
42 }
43
44 return getInitializingFlowParameter(name);
45 }
46
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).setBeanName(flowName);
51 }
52
53 if (log.isTraceEnabled())
54 log.trace("Start initialization of " + flow.hashCode() + " ("
55 + flow + " - " + flow.getClass() + ")");
56
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>());
61 }
62 flowStack.get().push(flow);
63 }
64
65 public void flowInitializationFinished(ExecutionFlow flow, String flowName) {
66 if (log.isTraceEnabled())
67 log.trace("Finish initialization of " + flow.hashCode() + " ("
68 + flow + " - " + flow.getClass() + ")");
69
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);
77 }
78 } else {
79 // happens for flows imported as services
80 log.warn("flowInitializationFinished - Flow Stack is null");
81 }
82 }
83
84 protected ExecutionFlow findInitializingFlowWithParameter(String key) {
85 if ((flowStack.get() == null) || flowStack.get().empty())
86 throw new SlcException("No initializing flow available.");
87
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);
92 }
93 }
94 throw new SlcException("Key " + key + " is not set as parameter in "
95 + flowStack.get().firstElement().toString() + " (stack size="
96 + flowStack.get().size() + ")");
97
98 }
99
100 public Object getInitializingFlowParameter(String key) {
101 return findInitializingFlowWithParameter(key).getParameter(key);
102 }
103
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);
112 if (clss == null)
113 throw new SlcException("Cannot convert type " + type
114 + " to class.");
115 return clss;
116 } else
117 return null;
118 }
119
120 public Boolean isInFlowInitialization() {
121 return (flowStack.get() != null) && !flowStack.get().empty();
122 }
123 }