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 // store the ExecutionContext in the ThreadLocal
31 executionContext
.set((ExecutionContext
) obj
);
32 executionContextBeanName
.set(name
);
35 throw new SlcException(
36 "Expected an ExecutionContext, got an object of class "
40 + ": make sure that you have porperly set scope=\"execution\" where required");
44 if (name
.equals(executionContextBeanName
.get())) {
45 return executionContext
.get();
47 // see if the executionContext already knows the object
48 Object obj
= executionContext
.get().findScopedObject(name
);
50 obj
= objectFactory
.getObject();
51 if (!(obj
instanceof ExecutionContext
)) {
52 executionContext
.get().addScopedObject(name
, obj
);
54 throw new SlcException(
55 "Only one ExecutionContext can be defined per Thread");
61 // if (ExecutionContext.getScopedObjects().containsKey(name)) {
62 // // returns cached instance
63 // Object obj = ExecutionContext.getScopedObjects().get(name);
64 // if (log.isTraceEnabled())
65 // log.trace("Return cached scoped object " + obj);
68 // // creates instance
69 // Object obj = objectFactory.getObject();
70 // ExecutionContext.getScopedObjects().put(name, obj);
71 // if (log.isTraceEnabled())
72 // log.trace("Created regular scoped object " + obj);
77 public String
getConversationId() {
79 return executionContext
.get().getUuid();
82 public Boolean
hasExecutionContext() {
83 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();