2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.core
.execution
;
18 import java
.util
.HashMap
;
20 import java
.util
.Stack
;
21 import java
.util
.UUID
;
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
;
30 public class DefaultExecutionStack
implements ExecutionStack
{
32 private final static Log log
= LogFactory
33 .getLog(DefaultExecutionStack
.class);
35 private final Stack
<ExecutionFlowRuntime
> stack
= new Stack
<ExecutionFlowRuntime
>();
37 public synchronized void enterFlow(ExecutionFlow executionFlow
) {
38 ExecutionFlowRuntime runtime
= new ExecutionFlowRuntime(executionFlow
);
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
));
51 public synchronized String
getCurrentStackLevelUuid() {
52 return stack
.peek().getUuid();
55 public synchronized Integer
getStackSize() {
60 * Looks for a set variable in the stack, starting at the upper flows
62 * @return the variable or <code>null</code> if not found
64 public synchronized Object
findLocalVariable(String key
) {
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
);
75 public synchronized void leaveFlow(ExecutionFlow executionFlow
) {
76 ExecutionFlowRuntime leftEf
= stack
.pop();
78 if (!leftEf
.getExecutionFlow().getName()
79 .equals(executionFlow
.getName()))
80 throw new SlcException("Asked to leave " + executionFlow
81 + " but last is " + leftEf
);
83 leftEf
.getScopedObjects().clear();
84 leftEf
.getLocalVariables().clear();
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
);
93 log
.warn("Scoped object " + name
+ " of type " + obj
.getClass()
94 + " already registered in " + runtime
);
96 runtime
.getScopedObjects().put(name
, obj
);
99 /** @return </code>null<code> if not found */
100 public synchronized Object
findScopedObject(String name
) {
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
);
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();
117 public ExecutionFlowRuntime(ExecutionFlow executionFlow
) {
118 this.executionFlow
= executionFlow
;
121 public ExecutionFlow
getExecutionFlow() {
122 return executionFlow
;
125 public Map
<String
, Object
> getScopedObjects() {
126 return scopedObjects
;
129 public String
getUuid() {
133 public Map
<String
, Object
> getLocalVariables() {
134 return localVariables
;
138 public String
toString() {
139 return "Stack Level #" + uuid
;