]> 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
Introduce system calls
[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 void addVariables(Map<? extends String, ? extends Object> variablesToAdd) {
29 variables.putAll(variablesToAdd);
30 }
31
32 public void enterFlow(ExecutionFlow executionFlow) {
33 ExecutionFlowRuntime runtime = new ExecutionFlowRuntime(executionFlow);
34 stack.push(runtime);
35
36 if (log.isDebugEnabled())
37 log.debug(depthSpaces(stack.size()) + "=> " + executionFlow + " #"
38 + uuid + ", depth=" + stack.size());
39
40 Map<String, ExecutionSpecAttribute> specAttrs = executionFlow
41 .getExecutionSpec().getAttributes();
42 for (String key : specAttrs.keySet()) {
43 //ExecutionSpecAttribute esa = specAttrs.get(key);
44 if (executionFlow.isSetAsParameter(key)) {
45 runtime.getLocalVariables().put(key,
46 executionFlow.getParameter(key));
47 if (log.isTraceEnabled())
48 log.trace(depthSpaces(stack.size()) + "Add '" + key
49 + "' as local variable.");
50 }
51 }
52
53 }
54
55 public Object getVariable(String key) {
56 Object obj = findVariable(key);
57 if (obj == null)
58 throw new SlcException("Variable '" + key + "' not found.");
59 return obj;
60 }
61
62 public Object findVariable(String key) {
63 Object obj = null;
64
65 // Look if the variable is set in the global execution variables
66 // (i.e. the variable was overridden)
67 if (variables.containsKey(key))
68 obj = variables.get(key);
69
70 // if the variable was not found, look in the stack starting at the
71 // upper flows
72 if (obj == null) {
73 for (int i = 0; i < stack.size(); i++) {
74 if (stack.get(i).getLocalVariables().containsKey(key)) {
75 obj = stack.get(i).getLocalVariables().get(key);
76 break;
77 }
78 }
79 }
80
81 return obj;
82 }
83
84 private static String depthSpaces(int depth) {
85 StringBuffer buf = new StringBuffer(depth * 2);
86 for (int i = 0; i < depth; i++)
87 buf.append(" ");
88 return buf.toString();
89 }
90
91 public void leaveFlow(ExecutionFlow executionFlow) {
92 if (log.isDebugEnabled())
93 log.debug(depthSpaces(stack.size()) + "<= " + executionFlow + " #"
94 + uuid + ", depth=" + stack.size());
95
96 ExecutionFlowRuntime leftEf = stack.pop();
97 if (!leftEf.getExecutionFlow().getName()
98 .equals(executionFlow.getName()))
99 throw new SlcException("Asked to leave " + executionFlow
100 + " but last is " + leftEf);
101
102 leftEf.getScopedObjects().clear();
103 leftEf.getLocalVariables().clear();
104
105 }
106
107 public void addScopedObject(String name, Object obj) {
108 //TODO: check that the object is not set yet ?
109 stack.peek().getScopedObjects().put(name, obj);
110 }
111
112 /** return null if not found */
113 public Object findScopedObject(String name) {
114 Object obj = null;
115 for (int i = stack.size() - 1; i >= 0; i--) {
116 if (stack.get(i).getScopedObjects().containsKey(name)) {
117 obj = stack.get(i).getScopedObjects().get(name);
118 break;
119 }
120 }
121 return obj;
122 }
123
124 public String getUuid() {
125 return uuid;
126 }
127
128 private static class ExecutionFlowRuntime {
129 private final ExecutionFlow executionFlow;
130 private final Map<String, Object> scopedObjects = new HashMap<String, Object>();
131 private final Map<String, Object> localVariables = new HashMap<String, Object>();
132 private final String uuid = UUID.randomUUID().toString();
133
134 public ExecutionFlowRuntime(ExecutionFlow executionFlow) {
135 this.executionFlow = executionFlow;
136 }
137
138 public ExecutionFlow getExecutionFlow() {
139 return executionFlow;
140 }
141
142 public Map<String, Object> getScopedObjects() {
143 return scopedObjects;
144 }
145
146 public String getUuid() {
147 return uuid;
148 }
149
150 public Map<String, Object> getLocalVariables() {
151 return localVariables;
152 }
153
154 }
155 }