]> 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
Add path in execution flow descriptor
[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.HashMap;
4 import java.util.Map;
5 import java.util.Stack;
6 import java.util.UUID;
7
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;
16
17 public class MapExecutionContext implements ExecutionContext {
18 private final static Log log = LogFactory.getLog(MapExecutionContext.class);
19
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 /* public Map<String, Object> getVariables() {
29 return variables;
30 }*/
31
32 public void addVariables(Map<? extends String, ? extends Object> variablesToAdd) {
33 variables.putAll(variablesToAdd);
34 }
35
36 public void enterFlow(ExecutionFlow executionFlow) {
37 ExecutionFlowRuntime runtime = new ExecutionFlowRuntime(executionFlow);
38 stack.push(runtime);
39
40 if (log.isDebugEnabled())
41 log.debug(depthSpaces(stack.size()) + "=> " + executionFlow + " #"
42 + uuid + ", depth=" + stack.size());
43
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.");
54 }
55 }
56
57 }
58
59 public Object getVariable(String key) {
60 Object obj = findVariable(key);
61 if (obj == null)
62 throw new SlcException("Variable '" + key + "' not found.");
63 return obj;
64 }
65
66 protected Object findVariable(String key) {
67 Object obj = null;
68
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);
73
74 // if the variable was not found, look in the stack starting at the
75 // upper flows
76 if (obj == null) {
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);
80 break;
81 }
82 }
83 }
84
85 return obj;
86 }
87
88 private static String depthSpaces(int depth) {
89 StringBuffer buf = new StringBuffer(depth * 2);
90 for (int i = 0; i < depth; i++)
91 buf.append(" ");
92 return buf.toString();
93 }
94
95 public void leaveFlow(ExecutionFlow executionFlow) {
96 if (log.isDebugEnabled())
97 log.debug(depthSpaces(stack.size()) + "<= " + executionFlow + " #"
98 + uuid + ", depth=" + stack.size());
99
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);
105
106 leftEf.getScopedObjects().clear();
107 leftEf.getLocalVariables().clear();
108
109 }
110
111 /* public Object findOrAddScopedObject(String name, ObjectFactory objectFactory) {
112 Object obj = findScopedObject(name);
113 if (obj == null) {
114 obj = objectFactory.getObject();
115 stack.peek().getScopedObjects().put(name, obj);
116 }
117 return obj;
118 }*/
119
120
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);
124 }
125
126 /** return null if not found */
127 public Object findScopedObject(String name) {
128 Object obj = null;
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);
132 break;
133 }
134 }
135 return obj;
136 }
137
138 public String getUuid() {
139 return uuid;
140 }
141
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();
147
148 public ExecutionFlowRuntime(ExecutionFlow executionFlow) {
149 this.executionFlow = executionFlow;
150 }
151
152 public ExecutionFlow getExecutionFlow() {
153 return executionFlow;
154 }
155
156 public Map<String, Object> getScopedObjects() {
157 return scopedObjects;
158 }
159
160 public String getUuid() {
161 return uuid;
162 }
163
164 public Map<String, Object> getLocalVariables() {
165 return localVariables;
166 }
167
168 }
169 }