/
CallScopeContext.java
149 lines (120 loc) · 4.08 KB
/
CallScopeContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package org.acme;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.enterprise.context.ContextNotActiveException;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import io.quarkus.arc.ContextInstanceHandle;
import io.quarkus.arc.InjectableBean;
import io.quarkus.arc.InjectableContext;
import io.quarkus.arc.impl.ContextInstanceHandleImpl;
public class CallScopeContext implements InjectableContext { // InjectableContext extends AlterableContext extends Context
static final ThreadLocal<Map<Contextual<?>, ContextInstanceHandle<?>>> ACTIVE_SCOPE_ON_THREAD = new ThreadLocal<>();
@Override
public Class<? extends Annotation> getScope() {
return CallScoped.class;
}
@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
Map<Contextual<?>, ContextInstanceHandle<?>> activeScope = ACTIVE_SCOPE_ON_THREAD.get();
if (activeScope == null) {
throw new ContextNotActiveException();
}
@SuppressWarnings("unchecked")
ContextInstanceHandle<T> contextInstanceHandle = (ContextInstanceHandle<T>) activeScope
.computeIfAbsent(contextual, c -> {
if (creationalContext == null) {
return null;
}
T createdInstance = contextual.create(creationalContext);
return new ContextInstanceHandleImpl<T>((InjectableBean<T>) contextual, createdInstance,
creationalContext);
});
return contextInstanceHandle.get();
}
@Override
public <T> T get(Contextual<T> contextual) {
Map<Contextual<?>, ContextInstanceHandle<?>> activeScope = ACTIVE_SCOPE_ON_THREAD.get();
if (activeScope == null) {
throw new ContextNotActiveException();
}
@SuppressWarnings("unchecked")
ContextInstanceHandle<T> contextInstanceHandle = (ContextInstanceHandle<T>) activeScope.get(contextual);
if (contextInstanceHandle == null) {
return null;
}
return contextInstanceHandle.get();
}
@Override
public boolean isActive() {
return ACTIVE_SCOPE_ON_THREAD.get() != null;
}
@Override
public void destroy(Contextual<?> contextual) {
ContextInstanceHandle<?> contextInstanceHandle = ACTIVE_SCOPE_ON_THREAD.get().get(contextual);
if (contextInstanceHandle != null) {
contextInstanceHandle.destroy();
}
}
/**
* Two methods below are specific to Quarkus because defined on
* InjectableContext.
*/
@Override
public void destroy() {
Map<Contextual<?>, ContextInstanceHandle<?>> context = ACTIVE_SCOPE_ON_THREAD.get();
if (context == null) {
throw new ContextNotActiveException();
}
context.values().forEach(ContextInstanceHandle::destroy);
}
@Override
public ContextState getState() {
return new ContextState() {
@Override
public Map<InjectableBean<?>, Object> getContextualInstances() {
Map<Contextual<?>, ContextInstanceHandle<?>> activeScope = ACTIVE_SCOPE_ON_THREAD.get();
if (activeScope != null) {
return activeScope.values().stream()
.collect(Collectors.toMap(ContextInstanceHandle::getBean, ContextInstanceHandle::get));
}
return Collections.emptyMap();
}
};
}
/**
* Own API
*/
private final static CallScopeContext INSTANCE = new CallScopeContext();
public static CallScopeContext get() {
return INSTANCE;
}
public void enter() {
Map<Contextual<?>, ContextInstanceHandle<?>> activeScope = ACTIVE_SCOPE_ON_THREAD.get();
if (activeScope != null) {
throw new IllegalStateException("An instance of this scope is already active");
}
ACTIVE_SCOPE_ON_THREAD.set(new ConcurrentHashMap<>());
}
public void exit() {
Map<Contextual<?>, ContextInstanceHandle<?>> activeScope = ACTIVE_SCOPE_ON_THREAD.get();
if (activeScope == null) {
throw new IllegalStateException("Scope currently not active");
}
ACTIVE_SCOPE_ON_THREAD.set(null);
}
public <R> R with(Supplier<R> f) {
try {
this.enter();
return f.get();
} finally {
this.exit();
}
}
}