X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.simple%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fexecution%2FExecutionScope.java;h=05a60b73bd5e9db39073fed4cbc2fd54e05e3fdd;hb=c83e59caa846134d0a2d5eff2485d994255c12cc;hp=adbf18aed7d1903d477f83c4d50b31b2a8200329;hpb=1b19cac6a92c31d9119fc2986e7aaf3df98b2393;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionScope.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionScope.java index adbf18aed..05a60b73b 100644 --- a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionScope.java +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionScope.java @@ -1,20 +1,62 @@ package org.argeo.slc.core.execution; +import java.util.HashMap; +import java.util.Map; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.slc.SlcException; +import org.argeo.slc.execution.ExecutionContext; import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.config.Scope; public class ExecutionScope implements Scope { private final static Log log = LogFactory.getLog(ExecutionScope.class); + private final ThreadLocal executionContext + = new ThreadLocal(); + + public final ThreadLocal executionContextBeanName = new ThreadLocal(); + public Object get(String name, ObjectFactory objectFactory) { if (log.isTraceEnabled()) log.trace("Getting scoped bean " + name); - return ExecutionContext.findOrAddScopedObject(name, objectFactory); - + + // check if an execution context is defined for this thread + if(executionContext.get() == null) { + // if not, we expect objectFactory to produce an ExecutionContext + Object obj = objectFactory.getObject(); + if(obj instanceof ExecutionContext) { + // store the ExecutionContext in the ThreadLocal + executionContext.set((ExecutionContext)obj); + executionContextBeanName.set(name); + return obj; + } + else { + throw new SlcException("Expected an ExecutionContext, got an object of class " + + obj.getClass() + " for bean " + name); + } + } + + if(name.equals(executionContextBeanName.get())) { + return executionContext.get(); + } + else { + // see if the executionContext already knows the object + Object obj = executionContext.get().findScopedObject(name); + if(obj == null) { + obj = objectFactory.getObject(); + if(!(obj instanceof ExecutionContext)) { + executionContext.get().addScopedObject(name, obj); + } + else { + throw new SlcException("Only one ExecutionContext can be defined per Thread"); + } + } + return obj; + } + // if (ExecutionContext.getScopedObjects().containsKey(name)) { // // returns cached instance // Object obj = ExecutionContext.getScopedObjects().get(name); @@ -32,8 +74,14 @@ public class ExecutionScope implements Scope { } public String getConversationId() { - return ExecutionContext.getCurrentStackUuid(); + + return executionContext.get().getUuid(); + } + + public Boolean hasExecutionContext() { + return executionContext.get() != null; } + public void registerDestructionCallback(String name, Runnable callback) { // TODO: implement it