]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/MapExecutionContext.java
Make tree test results serializable so that it can be used with ObjectList.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / MapExecutionContext.java
1 package org.argeo.slc.core.execution;
2
3 import java.util.Date;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Stack;
7 import java.util.UUID;
8
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;
17
18 public class MapExecutionContext implements ExecutionContext {
19 private final static Log log = LogFactory.getLog(MapExecutionContext.class);
20
21 private final Stack<ExecutionFlowRuntime> stack = new Stack<ExecutionFlowRuntime>();
22
23 // TODO: make it thread safe?
24 private final Map<String, Object> variables = new HashMap<String, Object>();
25
26 private final String uuid = UUID.randomUUID().toString();
27
28 private final Date creationDate = new Date();
29
30 public void addVariables(
31 Map<? extends String, ? extends Object> variablesToAdd) {
32 variables.putAll(variablesToAdd);
33 }
34
35 public void enterFlow(ExecutionFlow executionFlow) {
36 ExecutionFlowRuntime runtime = new ExecutionFlowRuntime(executionFlow);
37 stack.push(runtime);
38
39 if (log.isDebugEnabled())
40 log.debug(depthSpaces(stack.size()) + "=> " + executionFlow + " #"
41 + uuid + ", depth=" + stack.size());
42
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.");
53 }
54 }
55
56 }
57
58 public Object getVariable(String key) {
59 Object obj = findVariable(key);
60 if (obj == null)
61 throw new SlcException("Variable '" + key + "' not found.");
62 return obj;
63 }
64
65 public Object findVariable(String key) {
66 Object obj = null;
67
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);
72
73 // if the variable was not found, look in the stack starting at the
74 // upper flows
75 if (obj == null) {
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);
79 break;
80 }
81 }
82 }
83
84 return obj;
85 }
86
87 private static String depthSpaces(int depth) {
88 StringBuffer buf = new StringBuffer(depth * 2);
89 for (int i = 0; i < depth; i++)
90 buf.append(" ");
91 return buf.toString();
92 }
93
94 public void leaveFlow(ExecutionFlow executionFlow) {
95 if (log.isDebugEnabled())
96 log.debug(depthSpaces(stack.size()) + "<= " + executionFlow + " #"
97 + uuid + ", depth=" + stack.size());
98
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);
104
105 leftEf.getScopedObjects().clear();
106 leftEf.getLocalVariables().clear();
107
108 }
109
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);
113 }
114
115 /** return null if not found */
116 public Object findScopedObject(String name) {
117 Object obj = null;
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);
121 break;
122 }
123 }
124 return obj;
125 }
126
127 public String getUuid() {
128 return uuid;
129 }
130
131 public Date getCreationDate() {
132 return creationDate;
133 }
134
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();
140
141 public ExecutionFlowRuntime(ExecutionFlow executionFlow) {
142 this.executionFlow = executionFlow;
143 }
144
145 public ExecutionFlow getExecutionFlow() {
146 return executionFlow;
147 }
148
149 public Map<String, Object> getScopedObjects() {
150 return scopedObjects;
151 }
152
153 public String getUuid() {
154 return uuid;
155 }
156
157 public Map<String, Object> getLocalVariables() {
158 return localVariables;
159 }
160
161 }
162 }