-
-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathRequestScope.java
More file actions
112 lines (99 loc) · 2.9 KB
/
RequestScope.java
File metadata and controls
112 lines (99 loc) · 2.9 KB
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
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import java.util.HashMap;
import java.util.Map;
import org.jspecify.annotations.Nullable;
/**
* Thread-Local request scope implementation useful for save/store request attribute and access to
* them using a static way.
*
* <p>This is part of public API but usage must be keep to minimum.
*
* @author edgar
*/
public final class RequestScope {
private static final ThreadLocal<Map<Object, Object>> CONTEXT_TL = new ThreadLocal<>();
private RequestScope() {}
/**
* Check to see if there is already a value associated with the current thread for the given key.
*
* @param key The key against which to check for a given value within the current thread.
* @return True if there is currently a session bound.
*/
public static boolean hasBind(Object key) {
return get(key) != null;
}
/**
* Binds the given value to the current context for its key.
*
* @param key The key to be bound.
* @param value The value to be bound.
* @param <T> Bind type.
* @return Any previously bound session (should be null in most cases).
*/
public static @Nullable <T> T bind(Object key, T value) {
return (T) threadMap(true).put(key, value);
}
/**
* Unbinds the session (if one) current associated with the context for the given session.
*
* @param key The factory for which to unbind the current session.
* @param <T> Bind type.
* @return The bound session if one, else null.
*/
public static @Nullable <T> T unbind(Object key) {
var contextMap = threadMap();
T existing = null;
if (contextMap != null) {
existing = (T) contextMap.remove(key);
doCleanup();
}
return existing;
}
/**
* Get a previously bind value for the given key or <code>null</code>.
*
* @param key Key.
* @param <T> Object type.
* @return Binded value or <code>null</code>.
*/
public static @Nullable <T> T get(Object key) {
var contextMap = threadMap();
if (contextMap == null) {
return null;
} else {
return (T) contextMap.get(key);
}
}
/**
* Exposes thread local state. Internal usage only (don't use it).
*
* @return Exposes thread local state. Internal usage only (don't use it).
*/
public static ThreadLocal<Map<Object, Object>> threadLocal() {
return CONTEXT_TL;
}
private static Map<Object, Object> threadMap() {
return threadMap(false);
}
private static Map<Object, Object> threadMap(boolean createMap) {
var contextMap = CONTEXT_TL.get();
if (contextMap == null && createMap) {
contextMap = new HashMap<>();
CONTEXT_TL.set(contextMap);
}
return contextMap;
}
private static void doCleanup() {
var ctx = threadMap(false);
if (ctx != null) {
if (ctx.isEmpty()) {
CONTEXT_TL.remove();
}
}
}
}