From 037baaa685702af2421a5dd15f8a1e85a43dd023 Mon Sep 17 00:00:00 2001 From: Thomas Segismont Date: Fri, 13 May 2022 19:50:17 +0200 Subject: [PATCH] Let DevServices processors provide a reactive datasource URL This is simpler than transforming a JDBC url string. (cherry picked from commit 1254a7ee50c118c784ae8a35684e9eabf7f1fcb1) --- ...tasourceConfigurationHandlerBuildItem.java | 26 ++++++------------- .../spi/DevServicesDatasourceProvider.java | 17 ++++++++---- .../deployment/DB2DevServicesProcessor.java | 5 ++++ .../deployment/DerbyDevServicesProcessor.java | 1 + .../h2/deployment/H2DevServicesProcessor.java | 1 + .../MariaDBDevServicesProcessor.java | 5 ++++ .../deployment/MSSQLDevServicesProcessor.java | 11 ++++++++ .../deployment/MySQLDevServicesProcessor.java | 5 ++++ .../OracleDevServicesProcessor.java | 5 ++++ .../PostgresqlDevServicesProcessor.java | 5 ++++ .../ReactiveMSSQLClientProcessor.java | 12 +-------- 11 files changed, 59 insertions(+), 34 deletions(-) diff --git a/extensions/datasource/deployment-spi/src/main/java/io/quarkus/datasource/deployment/spi/DevServicesDatasourceConfigurationHandlerBuildItem.java b/extensions/datasource/deployment-spi/src/main/java/io/quarkus/datasource/deployment/spi/DevServicesDatasourceConfigurationHandlerBuildItem.java index 65a1a1a02138d..bdbd71fd0c34a 100644 --- a/extensions/datasource/deployment-spi/src/main/java/io/quarkus/datasource/deployment/spi/DevServicesDatasourceConfigurationHandlerBuildItem.java +++ b/extensions/datasource/deployment-spi/src/main/java/io/quarkus/datasource/deployment/spi/DevServicesDatasourceConfigurationHandlerBuildItem.java @@ -3,7 +3,6 @@ import java.util.Collections; import java.util.Map; import java.util.function.BiFunction; -import java.util.function.Function; import java.util.function.Predicate; import io.quarkus.builder.item.MultiBuildItem; @@ -56,15 +55,16 @@ public static DevServicesDatasourceConfigurationHandlerBuildItem jdbc(String dbK @Override public Map apply(String dsName, DevServicesDatasourceProvider.RunningDevServicesDatasource runningDevDb) { + String jdbcUrl = runningDevDb.getJdbcUrl(); if (dsName == null) { - return Collections.singletonMap("quarkus.datasource.jdbc.url", runningDevDb.getUrl()); + return Collections.singletonMap("quarkus.datasource.jdbc.url", jdbcUrl); } else { // we use quoted and unquoted versions because depending on whether a user configured other JDBC properties // one of the URLs may be ignored // see https://github.com/quarkusio/quarkus/issues/21387 return Map.of( - datasourceURLPropName(dsName), runningDevDb.getUrl(), - datasourceURLPropName("\"" + dsName + "\""), runningDevDb.getUrl()); + datasourceURLPropName(dsName), jdbcUrl, + datasourceURLPropName("\"" + dsName + "\""), jdbcUrl); } } @@ -86,31 +86,21 @@ private static String datasourceURLPropName(String dsName) { } public static DevServicesDatasourceConfigurationHandlerBuildItem reactive(String dbKind) { - return reactive(dbKind, new Function() { - @Override - public String apply(String url) { - return url.replaceFirst("jdbc:", "vertx-reactive:"); - } - }); - } - - public static DevServicesDatasourceConfigurationHandlerBuildItem reactive(String dbKind, - Function jdbcUrlTransformer) { return new DevServicesDatasourceConfigurationHandlerBuildItem(dbKind, new BiFunction>() { @Override public Map apply(String dsName, DevServicesDatasourceProvider.RunningDevServicesDatasource runningDevDb) { - String url = jdbcUrlTransformer.apply(runningDevDb.getUrl()); + String reactiveUrl = runningDevDb.getReactiveUrl(); if (dsName == null) { - return Collections.singletonMap("quarkus.datasource.reactive.url", url); + return Collections.singletonMap("quarkus.datasource.reactive.url", reactiveUrl); } else { // we use quoted and unquoted versions because depending on whether a user configured other JDBC properties // one of the URLs may be ignored // see https://github.com/quarkusio/quarkus/issues/21387 return Map.of( - datasourceReactiveURLPropName(dsName, false), url, - datasourceReactiveURLPropName(dsName, true), url); + datasourceReactiveURLPropName(dsName, false), reactiveUrl, + datasourceReactiveURLPropName(dsName, true), reactiveUrl); } } }, new Predicate() { diff --git a/extensions/datasource/deployment-spi/src/main/java/io/quarkus/datasource/deployment/spi/DevServicesDatasourceProvider.java b/extensions/datasource/deployment-spi/src/main/java/io/quarkus/datasource/deployment/spi/DevServicesDatasourceProvider.java index 25bd51c5571ed..d83a95aad15c9 100644 --- a/extensions/datasource/deployment-spi/src/main/java/io/quarkus/datasource/deployment/spi/DevServicesDatasourceProvider.java +++ b/extensions/datasource/deployment-spi/src/main/java/io/quarkus/datasource/deployment/spi/DevServicesDatasourceProvider.java @@ -24,14 +24,17 @@ default boolean isDockerRequired() { class RunningDevServicesDatasource { private final String id; - private final String url; + private final String jdbcUrl; + private final String reactiveUrl; private final String username; private final String password; private final Closeable closeTask; - public RunningDevServicesDatasource(String id, String url, String username, String password, Closeable closeTask) { + public RunningDevServicesDatasource(String id, String jdbcUrl, String reactiveUrl, String username, String password, + Closeable closeTask) { this.id = id; - this.url = url; + this.jdbcUrl = jdbcUrl; + this.reactiveUrl = reactiveUrl; this.username = username; this.password = password; this.closeTask = closeTask; @@ -41,8 +44,12 @@ public String getId() { return id; } - public String getUrl() { - return url; + public String getJdbcUrl() { + return jdbcUrl; + } + + public String getReactiveUrl() { + return reactiveUrl; } public Closeable getCloseTask() { diff --git a/extensions/devservices/db2/src/main/java/io/quarkus/devservices/db2/deployment/DB2DevServicesProcessor.java b/extensions/devservices/db2/src/main/java/io/quarkus/devservices/db2/deployment/DB2DevServicesProcessor.java index 6fb9218f41c2a..84051f317a5c6 100644 --- a/extensions/devservices/db2/src/main/java/io/quarkus/devservices/db2/deployment/DB2DevServicesProcessor.java +++ b/extensions/devservices/db2/src/main/java/io/quarkus/devservices/db2/deployment/DB2DevServicesProcessor.java @@ -46,6 +46,7 @@ public RunningDevServicesDatasource startDatabase(Optional username, Opt return new RunningDevServicesDatasource(container.getContainerId(), container.getEffectiveJdbcUrl(), + container.getReactiveUrl(), container.getUsername(), container.getPassword(), new Closeable() { @@ -104,5 +105,9 @@ public String getEffectiveJdbcUrl() { return super.getJdbcUrl(); } } + + public String getReactiveUrl() { + return getEffectiveJdbcUrl().replaceFirst("jdbc:", "vertx-reactive:"); + } } } diff --git a/extensions/devservices/derby/src/main/java/io/quarkus/devservices/derby/deployment/DerbyDevServicesProcessor.java b/extensions/devservices/derby/src/main/java/io/quarkus/devservices/derby/deployment/DerbyDevServicesProcessor.java index 4a2389fe066cf..d76492fc2fddb 100644 --- a/extensions/devservices/derby/src/main/java/io/quarkus/devservices/derby/deployment/DerbyDevServicesProcessor.java +++ b/extensions/devservices/derby/src/main/java/io/quarkus/devservices/derby/deployment/DerbyDevServicesProcessor.java @@ -69,6 +69,7 @@ public RunningDevServicesDatasource startDatabase(Optional username, Opt + additionalArgs.toString(), null, null, + null, new Closeable() { @Override public void close() throws IOException { diff --git a/extensions/devservices/h2/src/main/java/io/quarkus/devservices/h2/deployment/H2DevServicesProcessor.java b/extensions/devservices/h2/src/main/java/io/quarkus/devservices/h2/deployment/H2DevServicesProcessor.java index 9b4fb7a7200aa..3505b54791208 100644 --- a/extensions/devservices/h2/src/main/java/io/quarkus/devservices/h2/deployment/H2DevServicesProcessor.java +++ b/extensions/devservices/h2/src/main/java/io/quarkus/devservices/h2/deployment/H2DevServicesProcessor.java @@ -53,6 +53,7 @@ public RunningDevServicesDatasource startDatabase(Optional username, Opt + ";DB_CLOSE_DELAY=-1" + additionalArgs.toString(); return new RunningDevServicesDatasource(null, connectionUrl, + null, "sa", "sa", new Closeable() { diff --git a/extensions/devservices/mariadb/src/main/java/io/quarkus/devservices/mariadb/deployment/MariaDBDevServicesProcessor.java b/extensions/devservices/mariadb/src/main/java/io/quarkus/devservices/mariadb/deployment/MariaDBDevServicesProcessor.java index d0d6bc62f6428..49eae61be13cc 100644 --- a/extensions/devservices/mariadb/src/main/java/io/quarkus/devservices/mariadb/deployment/MariaDBDevServicesProcessor.java +++ b/extensions/devservices/mariadb/src/main/java/io/quarkus/devservices/mariadb/deployment/MariaDBDevServicesProcessor.java @@ -54,6 +54,7 @@ public RunningDevServicesDatasource startDatabase(Optional username, Opt return new RunningDevServicesDatasource(container.getContainerId(), container.getEffectiveJdbcUrl(), + container.getReactiveUrl(), container.getUsername(), container.getPassword(), new Closeable() { @@ -108,5 +109,9 @@ public String getEffectiveJdbcUrl() { return super.getJdbcUrl(); } } + + public String getReactiveUrl() { + return getEffectiveJdbcUrl().replaceFirst("jdbc:", "vertx-reactive:"); + } } } diff --git a/extensions/devservices/mssql/src/main/java/io/quarkus/devservices/mssql/deployment/MSSQLDevServicesProcessor.java b/extensions/devservices/mssql/src/main/java/io/quarkus/devservices/mssql/deployment/MSSQLDevServicesProcessor.java index 412e0cf84c931..09e71cf2568dd 100644 --- a/extensions/devservices/mssql/src/main/java/io/quarkus/devservices/mssql/deployment/MSSQLDevServicesProcessor.java +++ b/extensions/devservices/mssql/src/main/java/io/quarkus/devservices/mssql/deployment/MSSQLDevServicesProcessor.java @@ -44,6 +44,7 @@ public RunningDevServicesDatasource startDatabase(Optional username, Opt return new RunningDevServicesDatasource(container.getContainerId(), container.getEffectiveJdbcUrl(), + container.getReactiveUrl(), container.getUsername(), container.getPassword(), new Closeable() { @@ -101,5 +102,15 @@ public String getEffectiveJdbcUrl() { return super.getJdbcUrl(); } } + + public String getReactiveUrl() { + StringBuilder url = new StringBuilder("vertx-reactive:sqlserver://"); + if (useSharedNetwork) { + url.append(hostName).append(":").append(MS_SQL_SERVER_PORT); + } else { + url.append(this.getHost()).append(":").append(this.getMappedPort(MS_SQL_SERVER_PORT)); + } + return url.toString(); + } } } diff --git a/extensions/devservices/mysql/src/main/java/io/quarkus/devservices/mysql/deployment/MySQLDevServicesProcessor.java b/extensions/devservices/mysql/src/main/java/io/quarkus/devservices/mysql/deployment/MySQLDevServicesProcessor.java index 2f6c834bff0eb..2a7754df872d2 100644 --- a/extensions/devservices/mysql/src/main/java/io/quarkus/devservices/mysql/deployment/MySQLDevServicesProcessor.java +++ b/extensions/devservices/mysql/src/main/java/io/quarkus/devservices/mysql/deployment/MySQLDevServicesProcessor.java @@ -54,6 +54,7 @@ public RunningDevServicesDatasource startDatabase(Optional username, Opt return new RunningDevServicesDatasource(container.getContainerId(), container.getEffectiveJdbcUrl(), + container.getReactiveUrl(), container.getUsername(), container.getPassword(), new Closeable() { @@ -111,5 +112,9 @@ public String getEffectiveJdbcUrl() { return super.getJdbcUrl(); } } + + public String getReactiveUrl() { + return getEffectiveJdbcUrl().replaceFirst("jdbc:", "vertx-reactive:"); + } } } diff --git a/extensions/devservices/oracle/src/main/java/io/quarkus/devservices/oracle/deployment/OracleDevServicesProcessor.java b/extensions/devservices/oracle/src/main/java/io/quarkus/devservices/oracle/deployment/OracleDevServicesProcessor.java index 80889956024fd..cc8692f03b2e7 100644 --- a/extensions/devservices/oracle/src/main/java/io/quarkus/devservices/oracle/deployment/OracleDevServicesProcessor.java +++ b/extensions/devservices/oracle/src/main/java/io/quarkus/devservices/oracle/deployment/OracleDevServicesProcessor.java @@ -60,6 +60,7 @@ public RunningDevServicesDatasource startDatabase(Optional username, Opt return new RunningDevServicesDatasource(container.getContainerId(), container.getEffectiveJdbcUrl(), + container.getReactiveUrl(), container.getUsername(), container.getPassword(), new Closeable() { @@ -117,5 +118,9 @@ public String getEffectiveJdbcUrl() { return super.getJdbcUrl(); } } + + public String getReactiveUrl() { + return getEffectiveJdbcUrl().replaceFirst("jdbc:", "vertx-reactive:"); + } } } diff --git a/extensions/devservices/postgresql/src/main/java/io/quarkus/devservices/postgresql/deployment/PostgresqlDevServicesProcessor.java b/extensions/devservices/postgresql/src/main/java/io/quarkus/devservices/postgresql/deployment/PostgresqlDevServicesProcessor.java index 575e6612ba1d6..7ef2f60d93e63 100644 --- a/extensions/devservices/postgresql/src/main/java/io/quarkus/devservices/postgresql/deployment/PostgresqlDevServicesProcessor.java +++ b/extensions/devservices/postgresql/src/main/java/io/quarkus/devservices/postgresql/deployment/PostgresqlDevServicesProcessor.java @@ -54,6 +54,7 @@ public RunningDevServicesDatasource startDatabase(Optional username, Opt return new RunningDevServicesDatasource(container.getContainerId(), container.getEffectiveJdbcUrl(), + container.getReactiveUrl(), container.getUsername(), container.getPassword(), new Closeable() { @@ -114,5 +115,9 @@ public String getEffectiveJdbcUrl() { return super.getJdbcUrl(); } } + + public String getReactiveUrl() { + return getEffectiveJdbcUrl().replaceFirst("jdbc:", "vertx-reactive:"); + } } } diff --git a/extensions/reactive-mssql-client/deployment/src/main/java/io/quarkus/reactive/mssql/client/deployment/ReactiveMSSQLClientProcessor.java b/extensions/reactive-mssql-client/deployment/src/main/java/io/quarkus/reactive/mssql/client/deployment/ReactiveMSSQLClientProcessor.java index f7e517d21257a..02a98ae5f3e0d 100644 --- a/extensions/reactive-mssql-client/deployment/src/main/java/io/quarkus/reactive/mssql/client/deployment/ReactiveMSSQLClientProcessor.java +++ b/extensions/reactive-mssql-client/deployment/src/main/java/io/quarkus/reactive/mssql/client/deployment/ReactiveMSSQLClientProcessor.java @@ -2,7 +2,6 @@ import java.util.List; import java.util.Optional; -import java.util.function.Function; import javax.enterprise.context.ApplicationScoped; @@ -84,16 +83,7 @@ ServiceStartBuildItem build(BuildProducer feature, @BuildStep DevServicesDatasourceConfigurationHandlerBuildItem devDbHandler() { - return DevServicesDatasourceConfigurationHandlerBuildItem.reactive(DatabaseKind.MSSQL, new Function() { - @Override - public String apply(String url) { - url = url.replaceFirst("jdbc:", "vertx-reactive:"); - if (url.endsWith(";encrypt=false")) { - return url.substring(0, url.length() - ";encrypt=false".length()); - } - return url; - } - }); + return DevServicesDatasourceConfigurationHandlerBuildItem.reactive(DatabaseKind.MSSQL); } @BuildStep