@@ -36,7 +36,7 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
3636
3737 @ Override
3838 public <T > void put (Key <T > key , T instance ) {
39- final io . vertx . core . Context context = Vertx . currentContext ();
39+ final ContextInternal context = currentContext ();
4040 if ( context != null ) {
4141 if ( trace ) LOG .tracef ( "Putting key,value in context: [%1$s, %2$s]" , key , instance );
4242 context .putLocal ( key , instance );
@@ -47,9 +47,13 @@ public <T> void put(Key<T> key, T instance) {
4747 }
4848 }
4949
50+ private static ContextInternal currentContext () {
51+ return (ContextInternal ) Vertx .currentContext ();
52+ }
53+
5054 @ Override
5155 public <T > T get (Key <T > key ) {
52- final io . vertx . core . Context context = Vertx . currentContext ();
56+ final ContextInternal context = currentContext ();
5357 if ( context != null ) {
5458 T local = context .getLocal ( key );
5559 if ( trace ) LOG .tracef ( "Getting value %2$s from context for key %1$s" , key , local );
@@ -63,7 +67,7 @@ public <T> T get(Key<T> key) {
6367
6468 @ Override
6569 public void remove (Key <?> key ) {
66- final io . vertx . core . Context context = Vertx . currentContext ();
70+ final ContextInternal context = currentContext ();
6771 if ( context != null ) {
6872 boolean removed = context .removeLocal ( key );
6973 if ( trace ) LOG .tracef ( "Key %s removed from context: %s" , key , removed );
@@ -75,14 +79,15 @@ public void remove(Key<?> key) {
7579
7680 @ Override
7781 public void execute (Runnable runnable ) {
78- final io .vertx .core .Context currentContext = Vertx . currentContext ();
82+ final io .vertx .core .Context currentContext = currentContext ();
7983 if ( currentContext == null ) {
8084 if ( trace ) LOG .tracef ( "Not in a Vert.x context, checking the VertxInstance service" );
8185 final io .vertx .core .Context newContext = vertxInstance .getVertx ().getOrCreateContext ();
8286 // Ensure we don't run on the root context, which is globally scoped:
8387 // that could lead to unintentionally share the same session with other streams.
8488 ContextInternal newContextInternal = (ContextInternal ) newContext ;
8589 final ContextInternal duplicate = newContextInternal .duplicate ();
90+
8691 if ( trace ) LOG .tracef ( "Using duplicated context from VertxInstance: %s" , duplicate );
8792 duplicate .runOnContext ( x -> runnable .run () );
8893 }
0 commit comments