Skip to content

Commit

Permalink
Let DevServices processors provide a reactive datasource URL
Browse files Browse the repository at this point in the history
This is simpler than transforming a JDBC url string.
  • Loading branch information
tsegismont committed May 13, 2022
1 parent a2b1712 commit 1254a7e
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,15 +55,16 @@ public static DevServicesDatasourceConfigurationHandlerBuildItem jdbc(String dbK
@Override
public Map<String, String> 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);
}
}

Expand All @@ -86,31 +86,21 @@ private static String datasourceURLPropName(String dsName) {
}

public static DevServicesDatasourceConfigurationHandlerBuildItem reactive(String dbKind) {
return reactive(dbKind, new Function<String, String>() {
@Override
public String apply(String url) {
return url.replaceFirst("jdbc:", "vertx-reactive:");
}
});
}

public static DevServicesDatasourceConfigurationHandlerBuildItem reactive(String dbKind,
Function<String, String> jdbcUrlTransformer) {
return new DevServicesDatasourceConfigurationHandlerBuildItem(dbKind,
new BiFunction<String, DevServicesDatasourceProvider.RunningDevServicesDatasource, Map<String, String>>() {
@Override
public Map<String, String> 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<String>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt

return new RunningDevServicesDatasource(container.getContainerId(),
container.getEffectiveJdbcUrl(),
container.getReactiveUrl(),
container.getUsername(),
container.getPassword(),
new ContainerShutdownCloseable(container, "IBM Db2"));
Expand Down Expand Up @@ -97,5 +98,9 @@ public String getEffectiveJdbcUrl() {
return super.getJdbcUrl();
}
}

public String getReactiveUrl() {
return getEffectiveJdbcUrl().replaceFirst("jdbc:", "vertx-reactive:");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt
+ additionalArgs.toString(),
null,
null,
null,
new Closeable() {
@Override
public void close() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt
+ ";DB_CLOSE_DELAY=-1" + additionalArgs.toString();
return new RunningDevServicesDatasource(null,
connectionUrl,
null,
"sa",
"sa",
new Closeable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt

return new RunningDevServicesDatasource(container.getContainerId(),
container.getEffectiveJdbcUrl(),
container.getReactiveUrl(),
container.getUsername(),
container.getPassword(),
new ContainerShutdownCloseable(container, "MariaDB"));
Expand Down Expand Up @@ -101,5 +102,9 @@ public String getEffectiveJdbcUrl() {
return super.getJdbcUrl();
}
}

public String getReactiveUrl() {
return getEffectiveJdbcUrl().replaceFirst("jdbc:", "vertx-reactive:");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt

return new RunningDevServicesDatasource(container.getContainerId(),
container.getEffectiveJdbcUrl(),
container.getReactiveUrl(),
container.getUsername(),
container.getPassword(),
new ContainerShutdownCloseable(container, "Microsoft SQL Server"));
Expand Down Expand Up @@ -94,5 +95,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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt

return new RunningDevServicesDatasource(container.getContainerId(),
container.getEffectiveJdbcUrl(),
container.getReactiveUrl(),
container.getUsername(),
container.getPassword(),
new ContainerShutdownCloseable(container, "MySQL"));
Expand Down Expand Up @@ -104,5 +105,9 @@ public String getEffectiveJdbcUrl() {
return super.getJdbcUrl();
}
}

public String getReactiveUrl() {
return getEffectiveJdbcUrl().replaceFirst("jdbc:", "vertx-reactive:");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt

return new RunningDevServicesDatasource(container.getContainerId(),
container.getEffectiveJdbcUrl(),
container.getReactiveUrl(),
container.getUsername(),
container.getPassword(),
new ContainerShutdownCloseable(container, "Oracle"));
Expand Down Expand Up @@ -110,5 +111,9 @@ public String getEffectiveJdbcUrl() {
return super.getJdbcUrl();
}
}

public String getReactiveUrl() {
return getEffectiveJdbcUrl().replaceFirst("jdbc:", "vertx-reactive:");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt

return new RunningDevServicesDatasource(container.getContainerId(),
container.getEffectiveJdbcUrl(),
container.getReactiveUrl(),
container.getUsername(),
container.getPassword(),
new ContainerShutdownCloseable(container, "PostgreSQL"));
Expand Down Expand Up @@ -107,5 +108,9 @@ public String getEffectiveJdbcUrl() {
return super.getJdbcUrl();
}
}

public String getReactiveUrl() {
return getEffectiveJdbcUrl().replaceFirst("jdbc:", "vertx-reactive:");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import javax.enterprise.context.ApplicationScoped;

Expand Down Expand Up @@ -84,16 +83,7 @@ ServiceStartBuildItem build(BuildProducer<FeatureBuildItem> feature,

@BuildStep
DevServicesDatasourceConfigurationHandlerBuildItem devDbHandler() {
return DevServicesDatasourceConfigurationHandlerBuildItem.reactive(DatabaseKind.MSSQL, new Function<String, String>() {
@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
Expand Down

0 comments on commit 1254a7e

Please sign in to comment.