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
.ExecutionContext
;
12 import org
.argeo
.slc
.execution
.ExecutionFlow
;
13 import org
.argeo
.slc
.execution
.ExecutionSpecAttribute
;
14 import org
.argeo
.slc
.process
.SlcExecution
;
15 import org
.springframework
.beans
.factory
.ObjectFactory
;
17 public class MapExecutionContext
implements ExecutionContext
{
18 private final static Log log
= LogFactory
.getLog(MapExecutionContext
.class);
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 void addVariables(Map
<?
extends String
, ?
extends Object
> variablesToAdd
) {
29 variables
.putAll(variablesToAdd
);
32 public void enterFlow(ExecutionFlow executionFlow
) {
33 ExecutionFlowRuntime runtime
= new ExecutionFlowRuntime(executionFlow
);
36 if (log
.isDebugEnabled())
37 log
.debug(depthSpaces(stack
.size()) + "=> " + executionFlow
+ " #"
38 + uuid
+ ", depth=" + stack
.size());
40 Map
<String
, ExecutionSpecAttribute
> specAttrs
= executionFlow
41 .getExecutionSpec().getAttributes();
42 for (String key
: specAttrs
.keySet()) {
43 //ExecutionSpecAttribute esa = specAttrs.get(key);
44 if (executionFlow
.isSetAsParameter(key
)) {
45 runtime
.getLocalVariables().put(key
,
46 executionFlow
.getParameter(key
));
47 if (log
.isTraceEnabled())
48 log
.trace(depthSpaces(stack
.size()) + "Add '" + key
49 + "' as local variable.");
55 public Object
getVariable(String key
) {
56 Object obj
= findVariable(key
);
58 throw new SlcException("Variable '" + key
+ "' not found.");
62 public Object
findVariable(String key
) {
65 // Look if the variable is set in the global execution variables
66 // (i.e. the variable was overridden)
67 if (variables
.containsKey(key
))
68 obj
= variables
.get(key
);
70 // if the variable was not found, look in the stack starting at the
73 for (int i
= 0; i
< stack
.size(); i
++) {
74 if (stack
.get(i
).getLocalVariables().containsKey(key
)) {
75 obj
= stack
.get(i
).getLocalVariables().get(key
);
84 private static String
depthSpaces(int depth
) {
85 StringBuffer buf
= new StringBuffer(depth
* 2);
86 for (int i
= 0; i
< depth
; i
++)
88 return buf
.toString();
91 public void leaveFlow(ExecutionFlow executionFlow
) {
92 if (log
.isDebugEnabled())
93 log
.debug(depthSpaces(stack
.size()) + "<= " + executionFlow
+ " #"
94 + uuid
+ ", depth=" + stack
.size());
96 ExecutionFlowRuntime leftEf
= stack
.pop();
97 if (!leftEf
.getExecutionFlow().getName()
98 .equals(executionFlow
.getName()))
99 throw new SlcException("Asked to leave " + executionFlow
100 + " but last is " + leftEf
);
102 leftEf
.getScopedObjects().clear();
103 leftEf
.getLocalVariables().clear();
107 public void addScopedObject(String name
, Object obj
) {
108 //TODO: check that the object is not set yet ?
109 stack
.peek().getScopedObjects().put(name
, obj
);
112 /** return null if not found */
113 public Object
findScopedObject(String name
) {
115 for (int i
= stack
.size() - 1; i
>= 0; i
--) {
116 if (stack
.get(i
).getScopedObjects().containsKey(name
)) {
117 obj
= stack
.get(i
).getScopedObjects().get(name
);
124 public String
getUuid() {
128 private static class ExecutionFlowRuntime
{
129 private final ExecutionFlow executionFlow
;
130 private final Map
<String
, Object
> scopedObjects
= new HashMap
<String
, Object
>();
131 private final Map
<String
, Object
> localVariables
= new HashMap
<String
, Object
>();
132 private final String uuid
= UUID
.randomUUID().toString();
134 public ExecutionFlowRuntime(ExecutionFlow executionFlow
) {
135 this.executionFlow
= executionFlow
;
138 public ExecutionFlow
getExecutionFlow() {
139 return executionFlow
;
142 public Map
<String
, Object
> getScopedObjects() {
143 return scopedObjects
;
146 public String
getUuid() {
150 public Map
<String
, Object
> getLocalVariables() {
151 return localVariables
;