1414import java .io .IOException ;
1515import java .net .InetSocketAddress ;
1616import java .util .ArrayList ;
17+ import java .util .concurrent .ConcurrentHashMap ;
1718import java .util .concurrent .Executor ;
1819import java .util .concurrent .Executors ;
1920
2526 */
2627public 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