1 package org
.argeo
.slc
.core
.execution
;
3 import java
.util
.HashMap
;
6 import org
.apache
.commons
.logging
.Log
;
7 import org
.apache
.commons
.logging
.LogFactory
;
8 import org
.argeo
.slc
.SlcException
;
9 import org
.argeo
.slc
.execution
.ExecutionContext
;
10 import org
.springframework
.beans
.factory
.ObjectFactory
;
11 import org
.springframework
.beans
.factory
.config
.Scope
;
13 public class ExecutionScope
implements Scope
{
14 private final static Log log
= LogFactory
.getLog(ExecutionScope
.class);
16 private final ThreadLocal
<ExecutionContext
> executionContext
= new ThreadLocal
<ExecutionContext
>();
18 public final ThreadLocal
<String
> executionContextBeanName
= new ThreadLocal
<String
>();
20 public Object
get(String name
, ObjectFactory objectFactory
) {
22 if (log
.isTraceEnabled())
23 log
.trace("Getting scoped bean " + name
);
25 // check if an execution context is defined for this thread
26 if (executionContext
.get() == null) {
27 // if not, we expect objectFactory to produce an ExecutionContext
28 Object obj
= objectFactory
.getObject();
29 if (obj
instanceof ExecutionContext
) {
30 // Check whether we are in an execution
31 // FIXME: do it more properly (not static)
32 // see https://www.argeo.org/bugzilla/show_bug.cgi?id=82
33 if (!ExecutionAspect
.inModuleExecution
.get()) {
35 .error("An execution context is being instantiated outside a module execution."
36 + " Please check your references to execution contexts."
37 + " This may lead to unexpected behaviour and will be rejected in the future.");
41 // store the ExecutionContext in the ThreadLocal
42 executionContext
.set((ExecutionContext
) obj
);
43 executionContextBeanName
.set(name
);
44 if (log
.isDebugEnabled()) {
45 log
.debug("Execution context #"
46 + executionContext
.get().getUuid()
47 + " instantiated. (beanName="
48 + executionContextBeanName
.get() + ")");
49 // Thread.dumpStack();
53 throw new SlcException(
54 "Expected an ExecutionContext, got an object of class "
58 + ": make sure that you have porperly set scope=\"execution\" where required");
62 if (name
.equals(executionContextBeanName
.get())) {
63 return executionContext
.get();
65 // see if the executionContext already knows the object
66 Object obj
= executionContext
.get().findScopedObject(name
);
68 obj
= objectFactory
.getObject();
69 if (!(obj
instanceof ExecutionContext
)) {
70 executionContext
.get().addScopedObject(name
, obj
);
72 throw new SlcException(
73 "Only one ExecutionContext can be defined per Thread");
79 // if (ExecutionContext.getScopedObjects().containsKey(name)) {
80 // // returns cached instance
81 // Object obj = ExecutionContext.getScopedObjects().get(name);
82 // if (log.isTraceEnabled())
83 // log.trace("Return cached scoped object " + obj);
86 // // creates instance
87 // Object obj = objectFactory.getObject();
88 // ExecutionContext.getScopedObjects().put(name, obj);
89 // if (log.isTraceEnabled())
90 // log.trace("Created regular scoped object " + obj);
95 public String
getConversationId() {
97 return executionContext
.get().getUuid();
100 public Boolean
hasExecutionContext() {
101 return executionContext
.get() != null;
104 public void registerDestructionCallback(String name
, Runnable callback
) {
105 // TODO: implement it
106 // throw new UnsupportedOperationException();
109 public Object
remove(String name
) {
110 log
.debug("Remove object " + name
);
111 throw new UnsupportedOperationException();