forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestScope.java
More file actions
106 lines (95 loc) · 2.81 KB
/
RequestScope.java
File metadata and controls
106 lines (95 loc) · 2.81 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
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
/**
* Thread-Local request scope implementation useful for save/store request attribute and access
* to them using a static way.
*
* 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(@Nonnull 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(@Nonnull Object key, @Nonnull 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(@Nonnull Object key) {
final Map<Object, Object> sessionMap = threadMap();
T existing = null;
if (sessionMap != null) {
existing = (T) sessionMap.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(@Nonnull Object key) {
final Map<Object, Object> sessionMap = threadMap();
if (sessionMap == null) {
return null;
} else {
return (T) sessionMap.get(key);
}
}
private static Map<Object, Object> threadMap() {
return threadMap(false);
}
private static Map<Object, Object> threadMap(boolean createMap) {
Map<Object, Object> sessionMap = CONTEXT_TL.get();
if (sessionMap == null && createMap) {
sessionMap = new HashMap<>();
CONTEXT_TL.set(sessionMap);
}
return sessionMap;
}
private static void doCleanup() {
final Map<Object, Object> ctx = threadMap(false);
if (ctx != null) {
if (ctx.isEmpty()) {
CONTEXT_TL.remove();
}
}
}
}