]> 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
Introduce revision build numbers
[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 // 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())
34 log
35 .error("An execution context is being instatiated outside an execution."
36 + " Please check that your references to execution contexts."
37 + " This may lead to unexpected behaviour and will be rejected in the future.");
38
39 // store the ExecutionContext in the ThreadLocal
40 executionContext.set((ExecutionContext) obj);
41 executionContextBeanName.set(name);
42 if (log.isDebugEnabled()) {
43 log.debug("Execution context #"
44 + executionContext.get().getUuid()
45 + " instantiated. (beanName="
46 + executionContextBeanName.get() + ")");
47 // Thread.dumpStack();
48 }
49 return obj;
50 } else {
51 throw new SlcException(
52 "Expected an ExecutionContext, got an object of class "
53 + obj.getClass()
54 + " for bean "
55 + name
56 + ": make sure that you have porperly set scope=\"execution\" where required");
57 }
58 }
59
60 if (name.equals(executionContextBeanName.get())) {
61 return executionContext.get();
62 } else {
63 // see if the executionContext already knows the object
64 Object obj = executionContext.get().findScopedObject(name);
65 if (obj == null) {
66 obj = objectFactory.getObject();
67 if (!(obj instanceof ExecutionContext)) {
68 executionContext.get().addScopedObject(name, obj);
69 } else {
70 throw new SlcException(
71 "Only one ExecutionContext can be defined per Thread");
72 }
73 }
74 return obj;
75 }
76
77 // if (ExecutionContext.getScopedObjects().containsKey(name)) {
78 // // returns cached instance
79 // Object obj = ExecutionContext.getScopedObjects().get(name);
80 // if (log.isTraceEnabled())
81 // log.trace("Return cached scoped object " + obj);
82 // return obj;
83 // } else {
84 // // creates instance
85 // Object obj = objectFactory.getObject();
86 // ExecutionContext.getScopedObjects().put(name, obj);
87 // if (log.isTraceEnabled())
88 // log.trace("Created regular scoped object " + obj);
89 // return obj;
90 // }
91 }
92
93 public String getConversationId() {
94
95 return executionContext.get().getUuid();
96 }
97
98 public Boolean hasExecutionContext() {
99 return executionContext.get() != null;
100 }
101
102 public void registerDestructionCallback(String name, Runnable callback) {
103 // TODO: implement it
104 // throw new UnsupportedOperationException();
105 }
106
107 public Object remove(String name) {
108 log.debug("Remove object " + name);
109 throw new UnsupportedOperationException();
110 }
111
112 }