1 package org
.argeo
.slc
.core
.execution
;
3 import java
.util
.HashMap
;
5 import java
.util
.Stack
;
8 import org
.apache
.commons
.logging
.Log
;
9 import org
.apache
.commons
.logging
.LogFactory
;
10 import org
.argeo
.slc
.SlcException
;
11 import org
.argeo
.slc
.execution
.ExecutionFlow
;
12 import org
.argeo
.slc
.execution
.ExecutionSpecAttribute
;
13 import org
.argeo
.slc
.process
.SlcExecution
;
14 import org
.springframework
.beans
.factory
.ObjectFactory
;
16 public class ExecutionContext
{
17 private final static Log log
= LogFactory
.getLog(ExecutionContext
.class);
19 private final static ThreadLocal
<ExecutionContext
> executionContext
= new ThreadLocal
<ExecutionContext
>();
21 private final Stack
<ExecutionFlowRuntime
> stack
= new Stack
<ExecutionFlowRuntime
>();
23 // TODO: make it thread safe?
24 private final Map
<String
, Object
> variables
= new HashMap
<String
, Object
>();
26 private final String uuid
= UUID
.randomUUID().toString();
28 public static Map
<String
, Object
> getVariables() {
29 if (executionContext
.get() == null)
31 return executionContext
.get().variables
;
34 public static ExecutionContext
getCurrent() {
35 return executionContext
.get();
38 public static String
getExecutionUuid() {
39 if (executionContext
.get() == null)
41 return executionContext
.get().getUuid();
44 public static void registerExecutionContext(ExecutionContext context
) {
45 if (executionContext
.get() != null)
46 throw new SlcException("Context #" + executionContext
.get().uuid
47 + " already registered.");
48 executionContext
.set(context
);
52 * Unregisters the ExecutionContext for the current Thread
53 * @throws a SlcException if no ExecutionContext was registered for the Thread
54 * @return the unregistered ExecutionContext
56 public static ExecutionContext
unregisterExecutionContext() {
57 ExecutionContext current
= executionContext
.get();
59 throw new SlcException("No Context registered.");
61 executionContext
.remove();
65 public static void enterFlow(ExecutionFlow executionFlow
) {
66 Stack
<ExecutionFlowRuntime
> stack
= executionContext
.get().stack
;
68 ExecutionFlowRuntime runtime
= new ExecutionFlowRuntime(executionFlow
);
71 if (log
.isDebugEnabled())
72 log
.debug(depthSpaces(stack
.size()) + "=> " + executionFlow
+ " #"
73 + getCurrentStackUuid() + ", depth=" + stack
.size());
75 Map
<String
, ExecutionSpecAttribute
> specAttrs
= executionFlow
76 .getExecutionSpec().getAttributes();
77 for (String key
: specAttrs
.keySet()) {
78 //ExecutionSpecAttribute esa = specAttrs.get(key);
79 if (executionFlow
.isSetAsParameter(key
)) {
80 runtime
.getLocalVariables().put(key
,
81 executionFlow
.getParameter(key
));
82 if (log
.isTraceEnabled())
83 log
.trace(depthSpaces(stack
.size()) + "Add '" + key
84 + "' as local variable.");
90 public static Object
getVariable(String key
) {
91 Object obj
= getWithCheck().findVariable(key
);
93 throw new SlcException("Variable '" + key
+ "' not found.");
97 protected Object
findVariable(String key
) {
99 for (int i
= stack
.size() - 1; i
>= 0; i
--) {
100 if (stack
.get(i
).getLocalVariables().containsKey(key
)) {
101 obj
= stack
.get(i
).getLocalVariables().get(key
);
106 // Look into global execution variables
108 if (variables
.containsKey(key
))
109 obj
= variables
.get(key
);
115 private static String
depthSpaces(int depth
) {
116 StringBuffer buf
= new StringBuffer(depth
* 2);
117 for (int i
= 0; i
< depth
; i
++)
119 return buf
.toString();
122 public static void leaveFlow(ExecutionFlow executionFlow
) {
123 Stack
<ExecutionFlowRuntime
> stack
= executionContext
.get().stack
;
124 if (log
.isDebugEnabled())
125 log
.debug(depthSpaces(stack
.size()) + "<= " + executionFlow
+ " #"
126 + getCurrentStackUuid() + ", depth=" + stack
.size());
128 ExecutionFlowRuntime leftEf
= stack
.pop();
129 if (!leftEf
.getExecutionFlow().getName()
130 .equals(executionFlow
.getName()))
131 throw new SlcException("Asked to leave " + executionFlow
132 + " but last is " + leftEf
);
134 leftEf
.getScopedObjects().clear();
135 leftEf
.getLocalVariables().clear();
139 public static String
getCurrentStackUuid() {
140 return getWithCheck().stack
.peek().uuid
;
143 // public static ExecutionFlow getCurrentFlow() {
144 // return getWithCheck().stack.peek().executionFlow;
147 public static Boolean
isExecuting() {
148 return executionContext
.get() != null;
151 protected static ExecutionContext
getWithCheck() {
152 if (executionContext
.get() == null)
153 throw new SlcException("No execution context");
154 return executionContext
.get();
157 public static Object
findOrAddScopedObject(String name
,
158 ObjectFactory objectFactory
) {
159 ExecutionContext executionContext
= getWithCheck();
160 Object obj
= executionContext
.findScopedObject(name
);
162 obj
= objectFactory
.getObject();
163 executionContext
.stack
.peek().getScopedObjects().put(name
, obj
);
168 /** return null if not found */
169 protected Object
findScopedObject(String key
) {
171 for (int i
= stack
.size() - 1; i
>= 0; i
--) {
172 if (stack
.get(i
).getScopedObjects().containsKey(key
)) {
173 obj
= stack
.get(i
).getScopedObjects().get(key
);
180 public String
getUuid() {
184 private static class ExecutionFlowRuntime
{
185 private final ExecutionFlow executionFlow
;
186 private final Map
<String
, Object
> scopedObjects
= new HashMap
<String
, Object
>();
187 private final Map
<String
, Object
> localVariables
= new HashMap
<String
, Object
>();
188 private final String uuid
= UUID
.randomUUID().toString();
190 public ExecutionFlowRuntime(ExecutionFlow executionFlow
) {
191 this.executionFlow
= executionFlow
;
194 public ExecutionFlow
getExecutionFlow() {
195 return executionFlow
;
198 public Map
<String
, Object
> getScopedObjects() {
199 return scopedObjects
;
202 public String
getUuid() {
206 public Map
<String
, Object
> getLocalVariables() {
207 return localVariables
;