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
{
18 private final static Log log
= LogFactory
.getLog(MapExecutionContext
.class);
20 private final Stack
<ExecutionFlowRuntime
> stack
= new Stack
<ExecutionFlowRuntime
>();
22 // TODO: make it thread safe?
23 private final Map
<String
, Object
> variables
= new HashMap
<String
, Object
>();
25 private final String uuid
;
27 private final Date creationDate
= new Date();
29 public MapExecutionContext() {
30 uuid
= UUID
.randomUUID().toString();
31 variables
.put(VAR_EXECUTION_CONTEXT_ID
, uuid
);
34 public void addVariables(
35 Map
<?
extends String
, ?
extends Object
> variablesToAdd
) {
36 variables
.putAll(variablesToAdd
);
39 public void enterFlow(ExecutionFlow executionFlow
) {
40 ExecutionFlowRuntime runtime
= new ExecutionFlowRuntime(executionFlow
);
42 variables
.put(VAR_FLOW_ID
, runtime
.getUuid());
43 variables
.put(VAR_FLOW_NAME
, runtime
.getExecutionFlow().getName());
45 if (log
.isDebugEnabled())
46 log
.debug(depthSpaces(stack
.size()) + "=> " + executionFlow
+ " #"
47 + uuid
+ ", depth=" + stack
.size());
49 Map
<String
, ExecutionSpecAttribute
> specAttrs
= executionFlow
50 .getExecutionSpec().getAttributes();
51 for (String key
: specAttrs
.keySet()) {
52 // ExecutionSpecAttribute esa = specAttrs.get(key);
53 if (executionFlow
.isSetAsParameter(key
)) {
54 runtime
.getLocalVariables().put(key
,
55 executionFlow
.getParameter(key
));
56 if (log
.isTraceEnabled())
57 log
.trace(depthSpaces(stack
.size()) + "Add '" + key
58 + "' as local variable.");
64 public Object
getVariable(String key
) {
65 Object obj
= findVariable(key
);
67 throw new SlcException("Variable '" + key
+ "' not found.");
71 public Object
findVariable(String key
) {
74 // Look if the variable is set in the global execution variables
75 // (i.e. the variable was overridden)
76 if (variables
.containsKey(key
))
77 obj
= variables
.get(key
);
79 // if the variable was not found, look in the stack starting at the
82 for (int i
= 0; i
< stack
.size(); i
++) {
83 if (stack
.get(i
).getLocalVariables().containsKey(key
)) {
84 obj
= stack
.get(i
).getLocalVariables().get(key
);
93 private static String
depthSpaces(int depth
) {
94 StringBuffer buf
= new StringBuffer(depth
* 2);
95 for (int i
= 0; i
< depth
; i
++)
97 return buf
.toString();
100 public void leaveFlow(ExecutionFlow executionFlow
) {
101 if (log
.isDebugEnabled())
102 log
.debug(depthSpaces(stack
.size()) + "<= " + executionFlow
+ " #"
103 + uuid
+ ", depth=" + stack
.size());
105 ExecutionFlowRuntime leftEf
= stack
.pop();
106 if (!leftEf
.getExecutionFlow().getName()
107 .equals(executionFlow
.getName()))
108 throw new SlcException("Asked to leave " + executionFlow
109 + " but last is " + leftEf
);
111 leftEf
.getScopedObjects().clear();
112 leftEf
.getLocalVariables().clear();
116 public void addScopedObject(String name
, Object obj
) {
117 // TODO: check that the object is not set yet ?
118 stack
.peek().getScopedObjects().put(name
, obj
);
121 /** return null if not found */
122 public Object
findScopedObject(String name
) {
124 for (int i
= stack
.size() - 1; i
>= 0; i
--) {
125 if (stack
.get(i
).getScopedObjects().containsKey(name
)) {
126 obj
= stack
.get(i
).getScopedObjects().get(name
);
133 public String
getUuid() {
137 public Date
getCreationDate() {
141 private static class ExecutionFlowRuntime
{
142 private final ExecutionFlow executionFlow
;
143 private final Map
<String
, Object
> scopedObjects
= new HashMap
<String
, Object
>();
144 private final Map
<String
, Object
> localVariables
= new HashMap
<String
, Object
>();
145 private final String uuid
= UUID
.randomUUID().toString();
147 public ExecutionFlowRuntime(ExecutionFlow executionFlow
) {
148 this.executionFlow
= executionFlow
;
151 public ExecutionFlow
getExecutionFlow() {
152 return executionFlow
;
155 public Map
<String
, Object
> getScopedObjects() {
156 return scopedObjects
;
159 public String
getUuid() {
163 public Map
<String
, Object
> getLocalVariables() {
164 return localVariables
;