]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionScope.java
Make tree test results serializable so that it can be used with ObjectList.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / ExecutionScope.java
1 package org.argeo.slc.core.execution;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
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;
12
13 public class ExecutionScope implements Scope {
14 private final static Log log = LogFactory.getLog(ExecutionScope.class);
15
16 private final ThreadLocal<ExecutionContext> executionContext = new ThreadLocal<ExecutionContext>();
17
18 public final ThreadLocal<String> executionContextBeanName = new ThreadLocal<String>();
19
20 public Object get(String name, ObjectFactory objectFactory) {
21
22 if (log.isTraceEnabled())
23 log.trace("Getting scoped bean " + name);
24
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);
33 return obj;
34 } else {
35 throw new SlcException(
36 "Expected an ExecutionContext, got an object of class "
37 + obj.getClass()
38 + " for bean "
39 + name
40 + ": make sure that you have porperly set scope=\"execution\" where required");
41 }
42 }
43
44 if (name.equals(executionContextBeanName.get())) {
45 return executionContext.get();
46 } else {
47 // see if the executionContext already knows the object
48 Object obj = executionContext.get().findScopedObject(name);
49 if (obj == null) {
50 obj = objectFactory.getObject();
51 if (!(obj instanceof ExecutionContext)) {
52 executionContext.get().addScopedObject(name, obj);
53 } else {
54 throw new SlcException(
55 "Only one ExecutionContext can be defined per Thread");
56 }
57 }
58 return obj;
59 }
60
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);
66 // return obj;
67 // } else {
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);
73 // return obj;
74 // }
75 }
76
77 public String getConversationId() {
78
79 return executionContext.get().getUuid();
80 }
81
82 public Boolean hasExecutionContext() {
83 return executionContext.get() != null;
84 }
85
86 public void registerDestructionCallback(String name, Runnable callback) {
87 // TODO: implement it
88 // throw new UnsupportedOperationException();
89 }
90
91 public Object remove(String name) {
92 log.debug("Remove object " + name);
93 throw new UnsupportedOperationException();
94 }
95
96 }