Skip to content

Commit f126f15

Browse files
committed
Add local map to store data
1 parent bf25199 commit f126f15

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/express/Express.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.IOException;
1515
import java.net.InetSocketAddress;
1616
import java.util.ArrayList;
17+
import java.util.concurrent.ConcurrentHashMap;
1718
import java.util.concurrent.Executor;
1819
import java.util.concurrent.Executors;
1920

@@ -25,6 +26,8 @@
2526
*/
2627
public class Express extends ExpressMiddleware {
2728

29+
private final ConcurrentHashMap<Object, Object> LOCALS = new ConcurrentHashMap<>();
30+
2831
private final ArrayList<ExpressFilterWorker> WORKER = new ArrayList<>();
2932
private final ExpressFilterChain MIDDLEWARE_CHAIN = new ExpressFilterChain();
3033
private final ExpressFilterChain FILTER_CHAIN = new ExpressFilterChain();
@@ -46,6 +49,34 @@ public Express(String hostname) {
4649
this.executor = Executors.newCachedThreadPool();
4750
}
4851

52+
/**
53+
* Default, will bind the server to "localhost"
54+
*/
55+
public Express() {
56+
}
57+
58+
/**
59+
* Add an key-val pair to the express app, can be used
60+
* to store data. Uses ConcurrentHashMap so it's thread save.
61+
*
62+
* @param key The key
63+
* @param val The value
64+
* @return The last value which was attached by this key, can be null.
65+
*/
66+
public Object set(Object key, Object val) {
67+
return LOCALS.put(key, val);
68+
}
69+
70+
/**
71+
* Returns the value which was allocated by this key.
72+
*
73+
* @param key The key.
74+
* @return The value.
75+
*/
76+
public Object get(Object key) {
77+
return LOCALS.get(key);
78+
}
79+
4980
/**
5081
* Set an executor service. Default is CachedThreadPool
5182
* Can only changed if the server isn't already stardet.
@@ -61,12 +92,6 @@ public void setExecutor(Executor executor) throws IOException {
6192
}
6293
}
6394

64-
/**
65-
* Default, will bind the server to "localhost"
66-
*/
67-
public Express() {
68-
}
69-
7095
/**
7196
* Add an middleware which will be firea BEFORE EACH request-type listener will be fired.
7297
*

0 commit comments

Comments
 (0)