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
);
51 public static void enterFlow(ExecutionFlow executionFlow
) {
52 Stack
<ExecutionFlowRuntime
> stack
= executionContext
.get().stack
;
54 ExecutionFlowRuntime runtime
= new ExecutionFlowRuntime(executionFlow
);
57 if (log
.isDebugEnabled())
58 log
.debug(depthSpaces(stack
.size()) + "=> " + executionFlow
+ " #"
59 + getCurrentStackUuid() + ", depth=" + stack
.size());
61 Map
<String
, ExecutionSpecAttribute
> specAttrs
= executionFlow
62 .getExecutionSpec().getAttributes();
63 for (String key
: specAttrs
.keySet()) {
64 //ExecutionSpecAttribute esa = specAttrs.get(key);
65 if (executionFlow
.isSetAsParameter(key
)) {
66 runtime
.getLocalVariables().put(key
,
67 executionFlow
.getParameter(key
));
68 if (log
.isTraceEnabled())
69 log
.trace(depthSpaces(stack
.size()) + "Add '" + key
70 + "' as local variable.");
76 public static Object
getVariable(String key
) {
77 Object obj
= getWithCheck().findVariable(key
);
79 throw new SlcException("Variable '" + key
+ "' not found.");
83 protected Object
findVariable(String key
) {
85 for (int i
= stack
.size() - 1; i
>= 0; i
--) {
86 if (stack
.get(i
).getLocalVariables().containsKey(key
)) {
87 obj
= stack
.get(i
).getLocalVariables().get(key
);
92 // Look into global execution variables
94 if (variables
.containsKey(key
))
95 obj
= variables
.get(key
);
101 private static String
depthSpaces(int depth
) {
102 StringBuffer buf
= new StringBuffer(depth
* 2);
103 for (int i
= 0; i
< depth
; i
++)
105 return buf
.toString();
108 public static void leaveFlow(ExecutionFlow executionFlow
) {
109 Stack
<ExecutionFlowRuntime
> stack
= executionContext
.get().stack
;
110 if (log
.isDebugEnabled())
111 log
.debug(depthSpaces(stack
.size()) + "<= " + executionFlow
+ " #"
112 + getCurrentStackUuid() + ", depth=" + stack
.size());
114 ExecutionFlowRuntime leftEf
= stack
.pop();
115 if (!leftEf
.getExecutionFlow().getName()
116 .equals(executionFlow
.getName()))
117 throw new SlcException("Asked to leave " + executionFlow
118 + " but last is " + leftEf
);
120 leftEf
.getScopedObjects().clear();
121 leftEf
.getLocalVariables().clear();
125 public static String
getCurrentStackUuid() {
126 return getWithCheck().stack
.peek().uuid
;
129 // public static ExecutionFlow getCurrentFlow() {
130 // return getWithCheck().stack.peek().executionFlow;
133 public static Boolean
isExecuting() {
134 return executionContext
.get() != null;
137 protected static ExecutionContext
getWithCheck() {
138 if (executionContext
.get() == null)
139 throw new SlcException("No execution context");
140 return executionContext
.get();
143 public static Object
findOrAddScopedObject(String name
,
144 ObjectFactory objectFactory
) {
145 ExecutionContext executionContext
= getWithCheck();
146 Object obj
= executionContext
.findScopedObject(name
);
148 obj
= objectFactory
.getObject();
149 executionContext
.stack
.peek().getScopedObjects().put(name
, obj
);
154 /** return null if not found */
155 protected Object
findScopedObject(String key
) {
157 for (int i
= stack
.size() - 1; i
>= 0; i
--) {
158 if (stack
.get(i
).getScopedObjects().containsKey(key
)) {
159 obj
= stack
.get(i
).getScopedObjects().get(key
);
166 public String
getUuid() {
170 private static class ExecutionFlowRuntime
{
171 private final ExecutionFlow executionFlow
;
172 private final Map
<String
, Object
> scopedObjects
= new HashMap
<String
, Object
>();
173 private final Map
<String
, Object
> localVariables
= new HashMap
<String
, Object
>();
174 private final String uuid
= UUID
.randomUUID().toString();
176 public ExecutionFlowRuntime(ExecutionFlow executionFlow
) {
177 this.executionFlow
= executionFlow
;
180 public ExecutionFlow
getExecutionFlow() {
181 return executionFlow
;
184 public Map
<String
, Object
> getScopedObjects() {
185 return scopedObjects
;
188 public String
getUuid() {
192 public Map
<String
, Object
> getLocalVariables() {
193 return localVariables
;