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 Map<String, Object> getVariables() {
32 public void addVariables(Map
<?
extends String
, ?
extends Object
> variablesToAdd
) {
33 variables
.putAll(variablesToAdd
);
36 public void enterFlow(ExecutionFlow executionFlow
) {
37 ExecutionFlowRuntime runtime
= new ExecutionFlowRuntime(executionFlow
);
40 if (log
.isDebugEnabled())
41 log
.debug(depthSpaces(stack
.size()) + "=> " + executionFlow
+ " #"
42 + uuid
+ ", depth=" + stack
.size());
44 Map
<String
, ExecutionSpecAttribute
> specAttrs
= executionFlow
45 .getExecutionSpec().getAttributes();
46 for (String key
: specAttrs
.keySet()) {
47 //ExecutionSpecAttribute esa = specAttrs.get(key);
48 if (executionFlow
.isSetAsParameter(key
)) {
49 runtime
.getLocalVariables().put(key
,
50 executionFlow
.getParameter(key
));
51 if (log
.isTraceEnabled())
52 log
.trace(depthSpaces(stack
.size()) + "Add '" + key
53 + "' as local variable.");
59 public Object
getVariable(String key
) {
60 Object obj
= findVariable(key
);
62 throw new SlcException("Variable '" + key
+ "' not found.");
66 protected Object
findVariable(String key
) {
69 // Look if the variable is set in the global execution variables
70 // (i.e. the variable was overridden)
71 if (variables
.containsKey(key
))
72 obj
= variables
.get(key
);
74 // if the variable was not found, look in the stack starting at the
77 for (int i
= 0; i
< stack
.size(); i
++) {
78 if (stack
.get(i
).getLocalVariables().containsKey(key
)) {
79 obj
= stack
.get(i
).getLocalVariables().get(key
);
88 private static String
depthSpaces(int depth
) {
89 StringBuffer buf
= new StringBuffer(depth
* 2);
90 for (int i
= 0; i
< depth
; i
++)
92 return buf
.toString();
95 public void leaveFlow(ExecutionFlow executionFlow
) {
96 if (log
.isDebugEnabled())
97 log
.debug(depthSpaces(stack
.size()) + "<= " + executionFlow
+ " #"
98 + uuid
+ ", depth=" + stack
.size());
100 ExecutionFlowRuntime leftEf
= stack
.pop();
101 if (!leftEf
.getExecutionFlow().getName()
102 .equals(executionFlow
.getName()))
103 throw new SlcException("Asked to leave " + executionFlow
104 + " but last is " + leftEf
);
106 leftEf
.getScopedObjects().clear();
107 leftEf
.getLocalVariables().clear();
111 /* public Object findOrAddScopedObject(String name, ObjectFactory objectFactory) {
112 Object obj = findScopedObject(name);
114 obj = objectFactory.getObject();
115 stack.peek().getScopedObjects().put(name, obj);
121 public void addScopedObject(String name
, Object obj
) {
122 //TODO: check that the object is not set yet ?
123 stack
.peek().getScopedObjects().put(name
, obj
);
126 /** return null if not found */
127 public Object
findScopedObject(String name
) {
129 for (int i
= stack
.size() - 1; i
>= 0; i
--) {
130 if (stack
.get(i
).getScopedObjects().containsKey(name
)) {
131 obj
= stack
.get(i
).getScopedObjects().get(name
);
138 public String
getUuid() {
142 private static class ExecutionFlowRuntime
{
143 private final ExecutionFlow executionFlow
;
144 private final Map
<String
, Object
> scopedObjects
= new HashMap
<String
, Object
>();
145 private final Map
<String
, Object
> localVariables
= new HashMap
<String
, Object
>();
146 private final String uuid
= UUID
.randomUUID().toString();
148 public ExecutionFlowRuntime(ExecutionFlow executionFlow
) {
149 this.executionFlow
= executionFlow
;
152 public ExecutionFlow
getExecutionFlow() {
153 return executionFlow
;
156 public Map
<String
, Object
> getScopedObjects() {
157 return scopedObjects
;
160 public String
getUuid() {
164 public Map
<String
, Object
> getLocalVariables() {
165 return localVariables
;