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
;
15 import org
.argeo
.slc
.process
.SlcExecution
;
16 import org
.springframework
.beans
.factory
.ObjectFactory
;
18 public class MapExecutionContext
implements ExecutionContext
{
19 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 private final Date creationDate
= new Date();
30 public void addVariables(
31 Map
<?
extends String
, ?
extends Object
> variablesToAdd
) {
32 variables
.putAll(variablesToAdd
);
35 public void enterFlow(ExecutionFlow executionFlow
) {
36 ExecutionFlowRuntime runtime
= new ExecutionFlowRuntime(executionFlow
);
39 if (log
.isDebugEnabled())
40 log
.debug(depthSpaces(stack
.size()) + "=> " + executionFlow
+ " #"
41 + uuid
+ ", depth=" + stack
.size());
43 Map
<String
, ExecutionSpecAttribute
> specAttrs
= executionFlow
44 .getExecutionSpec().getAttributes();
45 for (String key
: specAttrs
.keySet()) {
46 // ExecutionSpecAttribute esa = specAttrs.get(key);
47 if (executionFlow
.isSetAsParameter(key
)) {
48 runtime
.getLocalVariables().put(key
,
49 executionFlow
.getParameter(key
));
50 if (log
.isTraceEnabled())
51 log
.trace(depthSpaces(stack
.size()) + "Add '" + key
52 + "' as local variable.");
58 public Object
getVariable(String key
) {
59 Object obj
= findVariable(key
);
61 throw new SlcException("Variable '" + key
+ "' not found.");
65 public Object
findVariable(String key
) {
68 // Look if the variable is set in the global execution variables
69 // (i.e. the variable was overridden)
70 if (variables
.containsKey(key
))
71 obj
= variables
.get(key
);
73 // if the variable was not found, look in the stack starting at the
76 for (int i
= 0; i
< stack
.size(); i
++) {
77 if (stack
.get(i
).getLocalVariables().containsKey(key
)) {
78 obj
= stack
.get(i
).getLocalVariables().get(key
);
87 private static String
depthSpaces(int depth
) {
88 StringBuffer buf
= new StringBuffer(depth
* 2);
89 for (int i
= 0; i
< depth
; i
++)
91 return buf
.toString();
94 public void leaveFlow(ExecutionFlow executionFlow
) {
95 if (log
.isDebugEnabled())
96 log
.debug(depthSpaces(stack
.size()) + "<= " + executionFlow
+ " #"
97 + uuid
+ ", depth=" + stack
.size());
99 ExecutionFlowRuntime leftEf
= stack
.pop();
100 if (!leftEf
.getExecutionFlow().getName()
101 .equals(executionFlow
.getName()))
102 throw new SlcException("Asked to leave " + executionFlow
103 + " but last is " + leftEf
);
105 leftEf
.getScopedObjects().clear();
106 leftEf
.getLocalVariables().clear();
110 public void addScopedObject(String name
, Object obj
) {
111 // TODO: check that the object is not set yet ?
112 stack
.peek().getScopedObjects().put(name
, obj
);
115 /** return null if not found */
116 public Object
findScopedObject(String name
) {
118 for (int i
= stack
.size() - 1; i
>= 0; i
--) {
119 if (stack
.get(i
).getScopedObjects().containsKey(name
)) {
120 obj
= stack
.get(i
).getScopedObjects().get(name
);
127 public String
getUuid() {
131 public Date
getCreationDate() {
135 private static class ExecutionFlowRuntime
{
136 private final ExecutionFlow executionFlow
;
137 private final Map
<String
, Object
> scopedObjects
= new HashMap
<String
, Object
>();
138 private final Map
<String
, Object
> localVariables
= new HashMap
<String
, Object
>();
139 private final String uuid
= UUID
.randomUUID().toString();
141 public ExecutionFlowRuntime(ExecutionFlow executionFlow
) {
142 this.executionFlow
= executionFlow
;
145 public ExecutionFlow
getExecutionFlow() {
146 return executionFlow
;
149 public Map
<String
, Object
> getScopedObjects() {
150 return scopedObjects
;
153 public String
getUuid() {
157 public Map
<String
, Object
> getLocalVariables() {
158 return localVariables
;