]> 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
9e6c2b5bd89736cc0ea64c879e40ba1f264bdf15
[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
16 public class MapExecutionContext implements ExecutionContext {
17 private final static Log log = LogFactory.getLog(MapExecutionContext.class);
18
19 private final Stack<ExecutionFlowRuntime> stack = new Stack<ExecutionFlowRuntime>();
20
21 // TODO: make it thread safe?
22 private final Map<String, Object> variables = new HashMap<String, Object>();
23
24 private final String uuid;
25
26 private final Date creationDate = new Date();
27
28 public MapExecutionContext() {
29 uuid = UUID.randomUUID().toString();
30 }
31
32 public void addVariables(
33 Map<? extends String, ? extends Object> variablesToAdd) {
34 variables.putAll(variablesToAdd);
35 }
36
37 public void enterFlow(ExecutionFlow executionFlow) {
38 ExecutionFlowRuntime runtime = new ExecutionFlowRuntime(executionFlow);
39 stack.push(runtime);
40
41 if (log.isDebugEnabled())
42 log.debug(depthSpaces(stack.size()) + "=> " + executionFlow + " #"
43 + uuid + ", depth=" + stack.size());
44
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.");
55 }
56 }
57
58 }
59
60 public Object getVariable(String key) {
61 Object obj = findVariable(key);
62 if (obj == null)
63 throw new SlcException("Variable '" + key + "' not found.");
64 return obj;
65 }
66
67 public Object findVariable(String key) {
68 Object obj = null;
69
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);
74
75 // if the variable was not found, look in the stack starting at the
76 // upper flows
77 if (obj == null) {
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);
81 break;
82 }
83 }
84 }
85
86 return obj;
87 }
88
89 private static String depthSpaces(int depth) {
90 StringBuffer buf = new StringBuffer(depth * 2);
91 for (int i = 0; i < depth; i++)
92 buf.append(" ");
93 return buf.toString();
94 }
95
96 public void leaveFlow(ExecutionFlow executionFlow) {
97 if (log.isDebugEnabled())
98 log.debug(depthSpaces(stack.size()) + "<= " + executionFlow + " #"
99 + uuid + ", depth=" + stack.size());
100
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);
106
107 leftEf.getScopedObjects().clear();
108 leftEf.getLocalVariables().clear();
109
110 }
111
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);
115 }
116
117 /** return null if not found */
118 public Object findScopedObject(String name) {
119 Object obj = null;
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);
123 break;
124 }
125 }
126 return obj;
127 }
128
129 public String getUuid() {
130 return uuid;
131 }
132
133 public Date getCreationDate() {
134 return creationDate;
135 }
136
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();
142
143 public ExecutionFlowRuntime(ExecutionFlow executionFlow) {
144 this.executionFlow = executionFlow;
145 }
146
147 public ExecutionFlow getExecutionFlow() {
148 return executionFlow;
149 }
150
151 public Map<String, Object> getScopedObjects() {
152 return scopedObjects;
153 }
154
155 public String getUuid() {
156 return uuid;
157 }
158
159 public Map<String, Object> getLocalVariables() {
160 return localVariables;
161 }
162
163 }
164 }