]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ExecutionScope.java
Move resource distribution
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / 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 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.");
38 //Thread.dumpStack();
39 }
40
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();
50 }
51 return obj;
52 } else {
53 throw new SlcException(
54 "Expected an ExecutionContext, got an object of class "
55 + obj.getClass()
56 + " for bean "
57 + name
58 + ": make sure that you have porperly set scope=\"execution\" where required");
59 }
60 }
61
62 if (name.equals(executionContextBeanName.get())) {
63 return executionContext.get();
64 } else {
65 // see if the executionContext already knows the object
66 Object obj = executionContext.get().findScopedObject(name);
67 if (obj == null) {
68 obj = objectFactory.getObject();
69 if (!(obj instanceof ExecutionContext)) {
70 executionContext.get().addScopedObject(name, obj);
71 } else {
72 throw new SlcException(
73 "Only one ExecutionContext can be defined per Thread");
74 }
75 }
76 return obj;
77 }
78
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);
84 // return obj;
85 // } else {
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);
91 // return obj;
92 // }
93 }
94
95 public String getConversationId() {
96
97 return executionContext.get().getUuid();
98 }
99
100 public Boolean hasExecutionContext() {
101 return executionContext.get() != null;
102 }
103
104 public void registerDestructionCallback(String name, Runnable callback) {
105 // TODO: implement it
106 // throw new UnsupportedOperationException();
107 }
108
109 public Object remove(String name) {
110 log.debug("Remove object " + name);
111 throw new UnsupportedOperationException();
112 }
113
114 }