]> 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 system calls
[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
17 = new ThreadLocal<ExecutionContext>();
18
19 public final ThreadLocal<String> executionContextBeanName = new ThreadLocal<String>();
20
21 public Object get(String name, ObjectFactory objectFactory) {
22
23 if (log.isTraceEnabled())
24 log.trace("Getting scoped bean " + name);
25
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);
34 return obj;
35 }
36 else {
37 throw new SlcException("Expected an ExecutionContext, got an object of class "
38 + obj.getClass() + " for bean " + name);
39 }
40 }
41
42 if(name.equals(executionContextBeanName.get())) {
43 return executionContext.get();
44 }
45 else {
46 // see if the executionContext already knows the object
47 Object obj = executionContext.get().findScopedObject(name);
48 if(obj == null) {
49 obj = objectFactory.getObject();
50 if(!(obj instanceof ExecutionContext)) {
51 executionContext.get().addScopedObject(name, obj);
52 }
53 else {
54 throw new SlcException("Only one ExecutionContext can be defined per Thread");
55 }
56 }
57 return obj;
58 }
59
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);
65 // return obj;
66 // } else {
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);
72 // return obj;
73 // }
74 }
75
76 public String getConversationId() {
77
78 return executionContext.get().getUuid();
79 }
80
81 public Boolean hasExecutionContext() {
82 return executionContext.get() != null;
83 }
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 }