1 package org
.argeo
.slc
.core
.execution
;
4 import java
.util
.HashMap
;
6 import java
.util
.Stack
;
9 import org
.apache
.commons
.logging
.Log
;
10 import org
.apache
.commons
.logging
.LogFactory
;
11 import org
.argeo
.slc
.SlcException
;
12 import org
.argeo
.slc
.execution
.ExecutionContext
;
13 import org
.argeo
.slc
.execution
.ExecutionFlow
;
14 import org
.argeo
.slc
.execution
.ExecutionSpecAttribute
;
16 public class MapExecutionContext
implements ExecutionContext
{
17 private final static Log log
= LogFactory
.getLog(MapExecutionContext
.class);
19 private final Stack
<ExecutionFlowRuntime
> stack
= new Stack
<ExecutionFlowRuntime
>();
21 // TODO: make it thread safe?
22 private final Map
<String
, Object
> variables
= new HashMap
<String
, Object
>();
24 private final String uuid
;
26 private final Date creationDate
= new Date();
28 public MapExecutionContext() {
29 uuid
= UUID
.randomUUID().toString();
32 public void addVariables(
33 Map
<?
extends String
, ?
extends Object
> variablesToAdd
) {
34 variables
.putAll(variablesToAdd
);
37 public void enterFlow(ExecutionFlow executionFlow
) {
38 ExecutionFlowRuntime runtime
= new ExecutionFlowRuntime(executionFlow
);
41 if (log
.isDebugEnabled())
42 log
.debug(depthSpaces(stack
.size()) + "=> " + executionFlow
+ " #"
43 + uuid
+ ", depth=" + stack
.size());
45 Map
<String
, ExecutionSpecAttribute
> specAttrs
= executionFlow
46 .getExecutionSpec().getAttributes();
47 for (String key
: specAttrs
.keySet()) {
48 // ExecutionSpecAttribute esa = specAttrs.get(key);
49 if (executionFlow
.isSetAsParameter(key
)) {
50 runtime
.getLocalVariables().put(key
,
51 executionFlow
.getParameter(key
));
52 if (log
.isTraceEnabled())
53 log
.trace(depthSpaces(stack
.size()) + "Add '" + key
54 + "' as local variable.");
60 public Object
getVariable(String key
) {
61 Object obj
= findVariable(key
);
63 throw new SlcException("Variable '" + key
+ "' not found.");
67 public Object
findVariable(String key
) {
70 // Look if the variable is set in the global execution variables
71 // (i.e. the variable was overridden)
72 if (variables
.containsKey(key
))
73 obj
= variables
.get(key
);
75 // if the variable was not found, look in the stack starting at the
78 for (int i
= 0; i
< stack
.size(); i
++) {
79 if (stack
.get(i
).getLocalVariables().containsKey(key
)) {
80 obj
= stack
.get(i
).getLocalVariables().get(key
);
89 private static String
depthSpaces(int depth
) {
90 StringBuffer buf
= new StringBuffer(depth
* 2);
91 for (int i
= 0; i
< depth
; i
++)
93 return buf
.toString();
96 public void leaveFlow(ExecutionFlow executionFlow
) {
97 if (log
.isDebugEnabled())
98 log
.debug(depthSpaces(stack
.size()) + "<= " + executionFlow
+ " #"
99 + uuid
+ ", depth=" + stack
.size());
101 ExecutionFlowRuntime leftEf
= stack
.pop();
102 if (!leftEf
.getExecutionFlow().getName()
103 .equals(executionFlow
.getName()))
104 throw new SlcException("Asked to leave " + executionFlow
105 + " but last is " + leftEf
);
107 leftEf
.getScopedObjects().clear();
108 leftEf
.getLocalVariables().clear();
112 public void addScopedObject(String name
, Object obj
) {
113 // TODO: check that the object is not set yet ?
114 stack
.peek().getScopedObjects().put(name
, obj
);
117 /** return null if not found */
118 public Object
findScopedObject(String name
) {
120 for (int i
= stack
.size() - 1; i
>= 0; i
--) {
121 if (stack
.get(i
).getScopedObjects().containsKey(name
)) {
122 obj
= stack
.get(i
).getScopedObjects().get(name
);
129 public String
getUuid() {
133 public Date
getCreationDate() {
137 private static class ExecutionFlowRuntime
{
138 private final ExecutionFlow executionFlow
;
139 private final Map
<String
, Object
> scopedObjects
= new HashMap
<String
, Object
>();
140 private final Map
<String
, Object
> localVariables
= new HashMap
<String
, Object
>();
141 private final String uuid
= UUID
.randomUUID().toString();
143 public ExecutionFlowRuntime(ExecutionFlow executionFlow
) {
144 this.executionFlow
= executionFlow
;
147 public ExecutionFlow
getExecutionFlow() {
148 return executionFlow
;
151 public Map
<String
, Object
> getScopedObjects() {
152 return scopedObjects
;
155 public String
getUuid() {
159 public Map
<String
, Object
> getLocalVariables() {
160 return localVariables
;