From 17df8a7064503fe6db47c85f4791ec645dfc639f Mon Sep 17 00:00:00 2001 From: Oleksandr Bolbat Date: Wed, 13 Nov 2024 17:51:54 +0100 Subject: [PATCH 1/2] implemented VertxBuilder.VertxBuilderBridge as implementation to io.vertx.core.VertxBuilder and possibility to create io.vertx.core.impl.VertxBuilder from it related code cleanup and removed code duplication --- src/main/java/io/vertx/core/Vertx.java | 66 +----------------- .../java/io/vertx/core/impl/VertxBuilder.java | 68 +++++++++++++++++++ .../impl/launcher/VertxLifecycleHooks.java | 9 ++- 3 files changed, 77 insertions(+), 66 deletions(-) diff --git a/src/main/java/io/vertx/core/Vertx.java b/src/main/java/io/vertx/core/Vertx.java index f8d3120bdfd..cb07990e8ba 100644 --- a/src/main/java/io/vertx/core/Vertx.java +++ b/src/main/java/io/vertx/core/Vertx.java @@ -20,21 +20,16 @@ import io.vertx.core.eventbus.EventBus; import io.vertx.core.file.FileSystem; import io.vertx.core.http.*; -import io.vertx.core.impl.ContextInternal; +import io.vertx.core.impl.VertxBuilder.VertxBuilderBridge; import io.vertx.core.impl.VertxImpl; import io.vertx.core.metrics.Measured; -import io.vertx.core.metrics.MetricsOptions; import io.vertx.core.net.NetClient; import io.vertx.core.net.NetClientOptions; import io.vertx.core.net.NetServer; import io.vertx.core.net.NetServerOptions; import io.vertx.core.shareddata.SharedData; import io.vertx.core.spi.VerticleFactory; -import io.vertx.core.spi.VertxMetricsFactory; -import io.vertx.core.spi.VertxTracerFactory; -import io.vertx.core.spi.cluster.ClusterManager; import io.vertx.core.streams.ReadStream; -import io.vertx.core.tracing.TracingOptions; import java.util.Objects; import java.util.Set; @@ -72,64 +67,7 @@ public interface Vertx extends Measured { static VertxBuilder builder() { - return new io.vertx.core.VertxBuilder() { - VertxOptions options; - VertxTracerFactory tracerFactory; - VertxMetricsFactory metricsFactory; - ClusterManager clusterManager; - @Override - public VertxBuilder with(VertxOptions options) { - this.options = options; - return this; - } - @Override - public VertxBuilder withTracer(VertxTracerFactory factory) { - this.tracerFactory = factory; - return this; - } - @Override - public VertxBuilder withClusterManager(ClusterManager clusterManager) { - this.clusterManager = clusterManager; - return this; - } - @Override - public VertxBuilder withMetrics(VertxMetricsFactory factory) { - this.metricsFactory = factory; - return this; - } - private io.vertx.core.impl.VertxBuilder internalBuilder() { - VertxOptions opts = options != null ? options : new VertxOptions(); - if (clusterManager != null) { - opts.setClusterManager(clusterManager); - } - if (metricsFactory != null) { - MetricsOptions metricsOptions = opts.getMetricsOptions(); - if (metricsOptions != null) { - metricsOptions.setFactory(metricsFactory); - } else { - opts.setMetricsOptions(new MetricsOptions().setFactory(metricsFactory)); - } - metricsOptions.setEnabled(true); - } - if (tracerFactory != null) { - TracingOptions tracingOptions = opts.getTracingOptions(); - if (tracingOptions != null) { - tracingOptions.setFactory(tracerFactory); - } else { - opts.setTracingOptions(new TracingOptions().setFactory(tracerFactory)); - } - } - return new io.vertx.core.impl.VertxBuilder(opts).init(); - } - @Override - public Vertx build() { - return internalBuilder().vertx(); - } - @Override - public Future buildClustered() { - return Future.future(p -> internalBuilder().clusteredVertx(p)); - } - }; + return new VertxBuilderBridge(); } /** diff --git a/src/main/java/io/vertx/core/impl/VertxBuilder.java b/src/main/java/io/vertx/core/impl/VertxBuilder.java index 48d96355264..39115b88e72 100644 --- a/src/main/java/io/vertx/core/impl/VertxBuilder.java +++ b/src/main/java/io/vertx/core/impl/VertxBuilder.java @@ -421,4 +421,72 @@ static Transport findTransport(boolean preferNative) { return JDKTransport.INSTANCE; } } + + public static class VertxBuilderBridge implements io.vertx.core.VertxBuilder { + VertxOptions options; + VertxTracerFactory tracerFactory; + VertxMetricsFactory metricsFactory; + ClusterManager clusterManager; + + @Override + public io.vertx.core.VertxBuilder with(VertxOptions options) { + this.options = options; + return this; + } + + @Override + public io.vertx.core.VertxBuilder withTracer(VertxTracerFactory factory) { + this.tracerFactory = factory; + return this; + } + + @Override + public io.vertx.core.VertxBuilder withClusterManager(ClusterManager clusterManager) { + this.clusterManager = clusterManager; + return this; + } + + @Override + public io.vertx.core.VertxBuilder withMetrics(VertxMetricsFactory factory) { + this.metricsFactory = factory; + return this; + } + + @SuppressWarnings("deprecation") + public io.vertx.core.impl.VertxBuilder internalBuilder() { + VertxOptions opts = options != null ? options : new VertxOptions(); + if (clusterManager != null) { + opts.setClusterManager(clusterManager); + } + if (metricsFactory != null) { + MetricsOptions metricsOptions = opts.getMetricsOptions(); + if (metricsOptions != null) { + metricsOptions.setFactory(metricsFactory); + } else { + opts.setMetricsOptions(new MetricsOptions().setFactory(metricsFactory)); + } + metricsOptions.setEnabled(true); + } + if (tracerFactory != null) { + TracingOptions tracingOptions = opts.getTracingOptions(); + if (tracingOptions != null) { + tracingOptions.setFactory(tracerFactory); + } else { + opts.setTracingOptions(new TracingOptions().setFactory(tracerFactory)); + } + } + return new io.vertx.core.impl.VertxBuilder(opts).init(); + } + + @Override + public Vertx build() { + return internalBuilder().vertx(); + } + + @Override + public Future buildClustered() { + return Future.future(p -> internalBuilder().clusteredVertx(p)); + } + } + } diff --git a/src/main/java/io/vertx/core/impl/launcher/VertxLifecycleHooks.java b/src/main/java/io/vertx/core/impl/launcher/VertxLifecycleHooks.java index 33619befb5f..f5eab0347d6 100644 --- a/src/main/java/io/vertx/core/impl/launcher/VertxLifecycleHooks.java +++ b/src/main/java/io/vertx/core/impl/launcher/VertxLifecycleHooks.java @@ -15,6 +15,7 @@ import io.vertx.core.Vertx; import io.vertx.core.VertxOptions; import io.vertx.core.impl.VertxBuilder; +import io.vertx.core.impl.VertxBuilder.VertxBuilderBridge; import io.vertx.core.json.JsonObject; import io.vertx.core.spi.tracing.VertxTracer; @@ -40,9 +41,13 @@ public interface VertxLifecycleHooks { * * @param config the Vert.x options to use, in JSON format * @return the Vert.x builder instance - */ + */ default VertxBuilder createVertxBuilder(JsonObject config) { - return config == null ? new VertxBuilder() : new VertxBuilder(config); + final VertxBuilderBridge vertxBuilder = new VertxBuilder.VertxBuilderBridge(); + if (config != null) + vertxBuilder.with(new VertxOptions(config)); + + return vertxBuilder.internalBuilder(); } /** From 8956deb8e8508d9c8c509adcf71e968eb45e0e93 Mon Sep 17 00:00:00 2001 From: Oleksandr Bolbat Date: Wed, 13 Nov 2024 18:16:38 +0100 Subject: [PATCH 2/2] implemented VertxBuilder.VertxBuilderBridge as implementation to io.vertx.core.VertxBuilder and possibility to create io.vertx.core.impl.VertxBuilder from it related code cleanup and removed code duplication --- .../java/io/vertx/core/impl/VertxBuilder.java | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/vertx/core/impl/VertxBuilder.java b/src/main/java/io/vertx/core/impl/VertxBuilder.java index 39115b88e72..eec79bff78b 100644 --- a/src/main/java/io/vertx/core/impl/VertxBuilder.java +++ b/src/main/java/io/vertx/core/impl/VertxBuilder.java @@ -11,17 +11,24 @@ package io.vertx.core.impl; -import io.vertx.core.*; -import io.vertx.core.impl.transports.EpollTransport; -import io.vertx.core.impl.transports.JDKTransport; -import io.vertx.core.impl.transports.KQueueTransport; -import io.vertx.core.spi.file.FileResolver; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +import io.vertx.core.AsyncResult; +import io.vertx.core.Future; +import io.vertx.core.Handler; +import io.vertx.core.ServiceHelper; +import io.vertx.core.Vertx; +import io.vertx.core.VertxOptions; import io.vertx.core.file.impl.FileResolverImpl; import io.vertx.core.impl.logging.Logger; import io.vertx.core.impl.logging.LoggerFactory; +import io.vertx.core.impl.transports.EpollTransport; +import io.vertx.core.impl.transports.JDKTransport; +import io.vertx.core.impl.transports.KQueueTransport; import io.vertx.core.json.JsonObject; import io.vertx.core.metrics.MetricsOptions; -import io.vertx.core.spi.transport.Transport; import io.vertx.core.spi.ExecutorServiceFactory; import io.vertx.core.spi.VertxMetricsFactory; import io.vertx.core.spi.VertxServiceProvider; @@ -30,14 +37,12 @@ import io.vertx.core.spi.cluster.ClusterManager; import io.vertx.core.spi.cluster.NodeSelector; import io.vertx.core.spi.cluster.impl.DefaultNodeSelector; +import io.vertx.core.spi.file.FileResolver; import io.vertx.core.spi.metrics.VertxMetrics; import io.vertx.core.spi.tracing.VertxTracer; +import io.vertx.core.spi.transport.Transport; import io.vertx.core.tracing.TracingOptions; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; - /** * Vertx builder for creating vertx instances with SPI overrides. * @@ -475,17 +480,18 @@ public io.vertx.core.impl.VertxBuilder internalBuilder() { opts.setTracingOptions(new TracingOptions().setFactory(tracerFactory)); } } - return new io.vertx.core.impl.VertxBuilder(opts).init(); + + return new io.vertx.core.impl.VertxBuilder(opts); } @Override public Vertx build() { - return internalBuilder().vertx(); + return internalBuilder().init().vertx(); } @Override public Future buildClustered() { - return Future.future(p -> internalBuilder().clusteredVertx(p)); + return Future.future(p -> internalBuilder().init().clusteredVertx(p)); } }