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
17 = new ThreadLocal
<ExecutionContext
>();
19 public final ThreadLocal
<String
> executionContextBeanName
= new ThreadLocal
<String
>();
21 public Object
get(String name
, ObjectFactory objectFactory
) {
23 if (log
.isTraceEnabled())
24 log
.trace("Getting scoped bean " + name
);
26 // check if an execution context is defined for this thread
27 if(executionContext
.get() == null) {
28 // if not, we expect objectFactory to produce an ExecutionContext
29 Object obj
= objectFactory
.getObject();
30 if(obj
instanceof ExecutionContext
) {
31 // store the ExecutionContext in the ThreadLocal
32 executionContext
.set((ExecutionContext
)obj
);
33 executionContextBeanName
.set(name
);
37 throw new SlcException("Expected an ExecutionContext, got an object of class "
38 + obj
.getClass() + " for bean " + name
);
42 if(name
.equals(executionContextBeanName
.get())) {
43 return executionContext
.get();
46 // see if the executionContext already knows the object
47 Object obj
= executionContext
.get().findScopedObject(name
);
49 obj
= objectFactory
.getObject();
50 if(!(obj
instanceof ExecutionContext
)) {
51 executionContext
.get().addScopedObject(name
, obj
);
54 throw new SlcException("Only one ExecutionContext can be defined per Thread");
60 // if (ExecutionContext.getScopedObjects().containsKey(name)) {
61 // // returns cached instance
62 // Object obj = ExecutionContext.getScopedObjects().get(name);
63 // if (log.isTraceEnabled())
64 // log.trace("Return cached scoped object " + obj);
67 // // creates instance
68 // Object obj = objectFactory.getObject();
69 // ExecutionContext.getScopedObjects().put(name, obj);
70 // if (log.isTraceEnabled())
71 // log.trace("Created regular scoped object " + obj);
76 public String
getConversationId() {
78 return executionContext
.get().getUuid();
81 public Boolean
hasExecutionContext() {
82 return executionContext
.get() != null;
86 public void registerDestructionCallback(String name
, Runnable callback
) {
88 //throw new UnsupportedOperationException();
91 public Object
remove(String name
) {
92 log
.debug("Remove object " + name
);
93 throw new UnsupportedOperationException();