From c510af5d7f29cc7ff04595308b0a47ed513844e9 Mon Sep 17 00:00:00 2001 From: robert-bor Date: Mon, 13 Feb 2017 20:46:01 +0100 Subject: [PATCH] #16 if no port is specified, the spring.datasource.url will be parsed for its port value. If it is not set, the default port (5432) is set. The default port setting in properties is changed from 5432 to null, so that it can pick up the spring.datasource.url. --- README.md | 5 ++-- .../postgres/DockerPostgresBootSequence.java | 26 ++++++++++++++++++- .../postgres/DockerPostgresProperties.java | 2 +- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 01d1a3b..cfcf6e0 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ You can tweak the configuration to use for your Docker run. | image-name | the image name to be used for deploying the container. Default: postgres | | image-version | the version of the image to be used for deploying the container. Advice is to specify wherever possible. Default: latest | | password | the password used to apply for the database on the container. Default: postgres | -| port | the port that you can access the database on. Will be mapped to the container's own 5432 port. Default: 5432 | +| port | the port that you can access the database on will be taken from spring's datasource URL. | | startup-verification-text | the text that will be searched within the Docker log. If found, the Postgres container is available. Default: "PostgreSQL init process complete; ready for start up." | | std-out-filename | the file to write the Docker output to. Default: "docker-std-out.log" | | std-err-filename | the file to write the Docker errors to. Default: docker-std-err.log | @@ -94,10 +94,9 @@ docker: image-version: 9.6 force-clean: true container-name: postgression2 - port: 5434 ``` -Be sure to point your datasource url to the same port. +Be sure to point your datasource url to another port, for example 5434 (instead of 5432). Note that the force-clean flag is useful for tests. There is no reason to keep the container in that case and you might as well remove it, when another version is found. diff --git a/src/main/java/nl/_42/boot/docker/postgres/DockerPostgresBootSequence.java b/src/main/java/nl/_42/boot/docker/postgres/DockerPostgresBootSequence.java index b496da5..11dedd2 100644 --- a/src/main/java/nl/_42/boot/docker/postgres/DockerPostgresBootSequence.java +++ b/src/main/java/nl/_42/boot/docker/postgres/DockerPostgresBootSequence.java @@ -2,17 +2,21 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import java.io.IOException; public class DockerPostgresBootSequence { + private static final Integer DEFAULT_PORT = 5432; private static final Logger LOGGER = LoggerFactory.getLogger(DockerPostgresBootSequence.class); private final DockerPostgresProperties properties; + private final DataSourceProperties dataSourceProperties; - public DockerPostgresBootSequence(DockerPostgresProperties properties) { + public DockerPostgresBootSequence(DockerPostgresProperties properties, DataSourceProperties dataSourceProperties) { this.properties = properties; + this.dataSourceProperties = dataSourceProperties; } public DockerPostgresContainer execute() throws IOException, InterruptedException { @@ -23,6 +27,14 @@ public DockerPostgresContainer execute() throws IOException, InterruptedExceptio LOGGER.info("| * Force clean: " + properties.isForceClean()); LOGGER.info("| * Timeout: " + properties.getTimeout()); LOGGER.info("| * Container name: " + properties.getContainerName()); + if (properties.getPort() == null) { + if (dataSourceProperties.getUrl() != null) { + properties.setPort(determinePort(dataSourceProperties.getUrl())); + // Scrap the port from the JDBC URL + } else { + properties.setPort(DEFAULT_PORT); + } + } LOGGER.info("| * Port: " + properties.getPort()); LOGGER.info("| * Password: " + properties.getPassword()); LOGGER.info("| * Startup Verification Text: [" + properties.getStartupVerificationText() + "]"); @@ -62,6 +74,18 @@ public DockerPostgresContainer execute() throws IOException, InterruptedExceptio return postgresContainer; } + private Integer determinePort(String url) { + if (url == null || url.length() == 0) { + throw new ExceptionInInitializerError("spring.datasource.url is empty. No port could be derived."); + } + int lastColonPos = url.lastIndexOf(':'); + int slashAfterPortPos = url.indexOf('/', lastColonPos); + if (lastColonPos == -1 || slashAfterPortPos == -1 || slashAfterPortPos < lastColonPos + 2) { + throw new ExceptionInInitializerError("spring.datasource.url does not have port information: [" + url + "]. No port could be derived."); + } + return Integer.parseInt(url.substring(lastColonPos + 1, slashAfterPortPos)); + } + private void applyAfterVerificationWait(Integer afterVerificationWait) throws InterruptedException { if (afterVerificationWait > 0) { LOGGER.info("| Applying after verification wait of " + afterVerificationWait + "ms"); diff --git a/src/main/java/nl/_42/boot/docker/postgres/DockerPostgresProperties.java b/src/main/java/nl/_42/boot/docker/postgres/DockerPostgresProperties.java index f3066c0..c81475a 100644 --- a/src/main/java/nl/_42/boot/docker/postgres/DockerPostgresProperties.java +++ b/src/main/java/nl/_42/boot/docker/postgres/DockerPostgresProperties.java @@ -16,7 +16,7 @@ public class DockerPostgresProperties { private String password = "postgres"; - private Integer port = 5432; + private Integer port = null; private String imageName = "postgres";