]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultExecutionStack.java
Move ReailizedFlow to execution package
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / DefaultExecutionStack.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.core.execution;
17
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Stack;
21 import java.util.UUID;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.argeo.slc.SlcException;
26 import org.argeo.slc.execution.ExecutionFlow;
27 import org.argeo.slc.execution.ExecutionSpecAttribute;
28 import org.argeo.slc.execution.ExecutionStack;
29
30 public class DefaultExecutionStack implements ExecutionStack {
31
32 private final static Log log = LogFactory
33 .getLog(DefaultExecutionStack.class);
34
35 private final Stack<ExecutionFlowRuntime> stack = new Stack<ExecutionFlowRuntime>();
36
37 public synchronized void enterFlow(ExecutionFlow executionFlow) {
38 ExecutionFlowRuntime runtime = new ExecutionFlowRuntime(executionFlow);
39 stack.push(runtime);
40
41 Map<String, ExecutionSpecAttribute> specAttrs = executionFlow
42 .getExecutionSpec().getAttributes();
43 for (String key : specAttrs.keySet()) {
44 if (executionFlow.isSetAsParameter(key)) {
45 runtime.getLocalVariables().put(key,
46 executionFlow.getParameter(key));
47 }
48 }
49 }
50
51 public synchronized String getCurrentStackLevelUuid() {
52 return stack.peek().getUuid();
53 }
54
55 public synchronized Integer getStackSize() {
56 return stack.size();
57 }
58
59 /**
60 * Looks for a set variable in the stack, starting at the upper flows
61 *
62 * @return the variable or <code>null</code> if not found
63 */
64 public synchronized Object findLocalVariable(String key) {
65 Object obj = null;
66 for (int i = 0; i < stack.size(); i++) {
67 if (stack.get(i).getLocalVariables().containsKey(key)) {
68 obj = stack.get(i).getLocalVariables().get(key);
69 break;
70 }
71 }
72 return obj;
73 }
74
75 public synchronized void leaveFlow(ExecutionFlow executionFlow) {
76 ExecutionFlowRuntime leftEf = stack.pop();
77
78 if (!leftEf.getExecutionFlow().getName()
79 .equals(executionFlow.getName()))
80 throw new SlcException("Asked to leave " + executionFlow
81 + " but last is " + leftEf);
82
83 leftEf.getScopedObjects().clear();
84 leftEf.getLocalVariables().clear();
85 }
86
87 public synchronized void addScopedObject(String name, Object obj) {
88 ExecutionFlowRuntime runtime = stack.peek();
89 // TODO: check that the object is not set yet ?
90 if (log.isDebugEnabled()) {
91 Object existing = findScopedObject(name);
92 if (existing != null)
93 log.warn("Scoped object " + name + " of type " + obj.getClass()
94 + " already registered in " + runtime);
95 }
96 runtime.getScopedObjects().put(name, obj);
97 }
98
99 /** @return </code>null<code> if not found */
100 public synchronized Object findScopedObject(String name) {
101 Object obj = null;
102 for (int i = stack.size() - 1; i >= 0; i--) {
103 if (stack.get(i).getScopedObjects().containsKey(name)) {
104 obj = stack.get(i).getScopedObjects().get(name);
105 break;
106 }
107 }
108 return obj;
109 }
110
111 protected static class ExecutionFlowRuntime {
112 private final ExecutionFlow executionFlow;
113 private final Map<String, Object> scopedObjects = new HashMap<String, Object>();
114 private final Map<String, Object> localVariables = new HashMap<String, Object>();
115 private final String uuid = UUID.randomUUID().toString();
116
117 public ExecutionFlowRuntime(ExecutionFlow executionFlow) {
118 this.executionFlow = executionFlow;
119 }
120
121 public ExecutionFlow getExecutionFlow() {
122 return executionFlow;
123 }
124
125 public Map<String, Object> getScopedObjects() {
126 return scopedObjects;
127 }
128
129 public String getUuid() {
130 return uuid;
131 }
132
133 public Map<String, Object> getLocalVariables() {
134 return localVariables;
135 }
136
137 @Override
138 public String toString() {
139 return "Stack Level #" + uuid;
140 }
141
142 }
143 }