From 8cd8606fd845c8a1f5cbb12df35465be6c855ad2 Mon Sep 17 00:00:00 2001 From: Julien Viet Date: Wed, 15 Jan 2025 10:49:22 +0100 Subject: [PATCH] Provide access to context locals. Motivation: Expose the list of registered context locals of the vertx instance, so it gives knowledge for interacting with context locals. --- src/main/java/io/vertx/core/impl/VertxImpl.java | 7 +++++++ src/main/java/io/vertx/core/impl/VertxInternal.java | 7 +++++++ src/main/java/io/vertx/core/impl/VertxWrapper.java | 7 +++++++ src/test/java/io/vertx/core/ContextTest.java | 8 ++++++++ 4 files changed, 29 insertions(+) diff --git a/src/main/java/io/vertx/core/impl/VertxImpl.java b/src/main/java/io/vertx/core/impl/VertxImpl.java index 90e34119bd4..c6f7232583e 100644 --- a/src/main/java/io/vertx/core/impl/VertxImpl.java +++ b/src/main/java/io/vertx/core/impl/VertxImpl.java @@ -137,6 +137,7 @@ private static ThreadFactory virtualThreadFactory() { private final Map sharedHttpServers = new HashMap<>(); private final Map sharedNetServers = new HashMap<>(); private final ContextLocal[] contextLocals; + private final List> contextLocalsList; final WorkerPool workerPool; final WorkerPool internalWorkerPool; final WorkerPool virtualThreaWorkerPool; @@ -194,6 +195,7 @@ private static ThreadFactory virtualThreadFactory() { ThreadFactory virtualThreadFactory = virtualThreadFactory(); contextLocals = LocalSeq.get(); + contextLocalsList = Collections.unmodifiableList(Arrays.asList(contextLocals)); closeFuture = new CloseFuture(log); maxEventLoopExecTime = maxEventLoopExecuteTime; maxEventLoopExecTimeUnit = maxEventLoopExecuteTimeUnit; @@ -994,6 +996,11 @@ public AddressResolverGroup nettyAddressResolverGroup() { return addressResolver.nettyAddressResolverGroup(); } + @Override + public List> contextLocals() { + return contextLocalsList; + } + @Override public FileResolver fileResolver() { return fileResolver; diff --git a/src/main/java/io/vertx/core/impl/VertxInternal.java b/src/main/java/io/vertx/core/impl/VertxInternal.java index fd5b5480034..77bc250f8a4 100644 --- a/src/main/java/io/vertx/core/impl/VertxInternal.java +++ b/src/main/java/io/vertx/core/impl/VertxInternal.java @@ -24,6 +24,7 @@ import io.vertx.core.net.impl.NetServerImpl; import io.vertx.core.net.impl.ServerID; import io.vertx.core.net.impl.TCPServerBase; +import io.vertx.core.spi.context.storage.ContextLocal; import io.vertx.core.spi.transport.Transport; import io.vertx.core.spi.cluster.ClusterManager; import io.vertx.core.spi.file.FileResolver; @@ -33,6 +34,7 @@ import java.io.File; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.util.List; import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.Executor; @@ -240,6 +242,11 @@ default Future executeBlockingInternal(Callable blockingCodeHandler, b */ AddressResolverGroup nettyAddressResolverGroup(); + /** + * @return an immutable list of this vertx instance context locals + */ + List> contextLocals(); + BlockedThreadChecker blockedThreadChecker(); CloseFuture closeFuture(); diff --git a/src/main/java/io/vertx/core/impl/VertxWrapper.java b/src/main/java/io/vertx/core/impl/VertxWrapper.java index 086e4720417..520b12e6c8c 100644 --- a/src/main/java/io/vertx/core/impl/VertxWrapper.java +++ b/src/main/java/io/vertx/core/impl/VertxWrapper.java @@ -31,6 +31,7 @@ import io.vertx.core.net.impl.NetServerImpl; import io.vertx.core.net.impl.ServerID; import io.vertx.core.net.impl.TCPServerBase; +import io.vertx.core.spi.context.storage.ContextLocal; import io.vertx.core.spi.transport.Transport; import io.vertx.core.shareddata.SharedData; import io.vertx.core.spi.VerticleFactory; @@ -42,6 +43,7 @@ import java.io.File; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.Callable; @@ -506,6 +508,11 @@ public AddressResolverGroup nettyAddressResolverGroup() { return delegate.nettyAddressResolverGroup(); } + @Override + public List> contextLocals() { + return delegate.contextLocals(); + } + @Override public BlockedThreadChecker blockedThreadChecker() { return delegate.blockedThreadChecker(); diff --git a/src/test/java/io/vertx/core/ContextTest.java b/src/test/java/io/vertx/core/ContextTest.java index 988586fe306..494b3bbab97 100644 --- a/src/test/java/io/vertx/core/ContextTest.java +++ b/src/test/java/io/vertx/core/ContextTest.java @@ -1253,4 +1253,12 @@ public void testNestedDuplicate() { assertEquals("bar", duplicate.getLocal("foo")); assertEquals(expected, duplicate.getLocal(contextLocal)); } + + @Test + public void testContextLocals() { + List> locals = ((VertxInternal) vertx).contextLocals(); + assertSame(ContextInternal.LOCAL_MAP, locals.get(0)); + assertSame(contextLocal, locals.get(1)); + assertSame(locals, ((VertxInternal) vertx).contextLocals()); + } }