]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.runtime/src/org/argeo/slc/runtime/DefaultExecutionStack.java
Make core runtime features independent of Spring.
[gpl/argeo-slc.git] / org.argeo.slc.runtime / src / org / argeo / slc / runtime / DefaultExecutionStack.java
diff --git a/org.argeo.slc.runtime/src/org/argeo/slc/runtime/DefaultExecutionStack.java b/org.argeo.slc.runtime/src/org/argeo/slc/runtime/DefaultExecutionStack.java
new file mode 100644 (file)
index 0000000..4939e32
--- /dev/null
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.argeo.slc.runtime;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Stack;
+import java.util.UUID;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.SlcException;
+import org.argeo.slc.execution.ExecutionFlow;
+import org.argeo.slc.execution.ExecutionSpecAttribute;
+import org.argeo.slc.execution.ExecutionStack;
+
+/** Canonical implementation of an execution stack. */
+public class DefaultExecutionStack implements ExecutionStack {
+
+       private final static Log log = LogFactory
+                       .getLog(DefaultExecutionStack.class);
+
+       private final Stack<ExecutionFlowRuntime> stack = new Stack<ExecutionFlowRuntime>();
+
+       public synchronized void enterFlow(ExecutionFlow executionFlow) {
+               ExecutionFlowRuntime runtime = new ExecutionFlowRuntime(executionFlow);
+               stack.push(runtime);
+
+               Map<String, ExecutionSpecAttribute> specAttrs = executionFlow
+                               .getExecutionSpec().getAttributes();
+               for (String key : specAttrs.keySet()) {
+                       if (executionFlow.isSetAsParameter(key)) {
+                               runtime.getLocalVariables().put(key,
+                                               executionFlow.getParameter(key));
+                       }
+               }
+       }
+
+       public synchronized String getCurrentStackLevelUuid() {
+               return stack.peek().getUuid();
+       }
+
+       public synchronized Integer getStackSize() {
+               return stack.size();
+       }
+
+       /**
+        * Looks for a set variable in the stack, starting at the upper flows
+        * 
+        * @return the variable or <code>null</code> if not found
+        */
+       public synchronized Object findLocalVariable(String key) {
+               Object obj = null;
+               for (int i = 0; i < stack.size(); i++) {
+                       if (stack.get(i).getLocalVariables().containsKey(key)) {
+                               obj = stack.get(i).getLocalVariables().get(key);
+                               break;
+                       }
+               }
+               return obj;
+       }
+
+       public synchronized void leaveFlow(ExecutionFlow executionFlow) {
+               ExecutionFlowRuntime leftEf = stack.pop();
+
+               if (!leftEf.getExecutionFlow().getName()
+                               .equals(executionFlow.getName()))
+                       throw new SlcException("Asked to leave " + executionFlow
+                                       + " but last is " + leftEf);
+
+               leftEf.getScopedObjects().clear();
+               leftEf.getLocalVariables().clear();
+       }
+
+       public synchronized void addScopedObject(String name, Object obj) {
+               ExecutionFlowRuntime runtime = stack.peek();
+               // TODO: check that the object is not set yet ?
+               if (log.isDebugEnabled()) {
+                       Object existing = findScopedObject(name);
+                       if (existing != null)
+                               log.warn("Scoped object " + name + " of type " + obj.getClass()
+                                               + " already registered in " + runtime);
+               }
+               runtime.getScopedObjects().put(name, obj);
+       }
+
+       /** @return </code>null<code> if not found */
+       public synchronized Object findScopedObject(String name) {
+               Object obj = null;
+               for (int i = stack.size() - 1; i >= 0; i--) {
+                       if (stack.get(i).getScopedObjects().containsKey(name)) {
+                               obj = stack.get(i).getScopedObjects().get(name);
+                               break;
+                       }
+               }
+               return obj;
+       }
+
+       protected static class ExecutionFlowRuntime {
+               private final ExecutionFlow executionFlow;
+               private final Map<String, Object> scopedObjects = new HashMap<String, Object>();
+               private final Map<String, Object> localVariables = new HashMap<String, Object>();
+               private final String uuid = UUID.randomUUID().toString();
+
+               public ExecutionFlowRuntime(ExecutionFlow executionFlow) {
+                       this.executionFlow = executionFlow;
+               }
+
+               public ExecutionFlow getExecutionFlow() {
+                       return executionFlow;
+               }
+
+               public Map<String, Object> getScopedObjects() {
+                       return scopedObjects;
+               }
+
+               public String getUuid() {
+                       return uuid;
+               }
+
+               public Map<String, Object> getLocalVariables() {
+                       return localVariables;
+               }
+
+               @Override
+               public String toString() {
+                       return "Stack Level #" + uuid;
+               }
+
+       }
+}