From 48adfe626c624eeb721bbc7c33a3839778844a21 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 14 Jun 2019 13:42:01 +0100 Subject: [PATCH] Log H2 console path and JDBC URL on startup Closes gh-17063 --- .../h2/H2ConsoleAutoConfiguration.java | 25 ++++++++++++++++++- .../h2/H2ConsoleAutoConfigurationTests.java | 21 ++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java index 89e03abd5a..c857d486fc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java @@ -16,13 +16,23 @@ package org.springframework.boot.autoconfigure.h2; +import java.sql.Connection; +import java.sql.SQLException; + +import javax.sql.DataSource; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.h2.server.web.WebServlet; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; @@ -40,11 +50,15 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnClass(WebServlet.class) @ConditionalOnProperty(prefix = "spring.h2.console", name = "enabled", havingValue = "true", matchIfMissing = false) +@AutoConfigureAfter(DataSourceAutoConfiguration.class) @EnableConfigurationProperties(H2ConsoleProperties.class) public class H2ConsoleAutoConfiguration { + private static final Log logger = LogFactory.getLog(H2ConsoleAutoConfiguration.class); + @Bean - public ServletRegistrationBean h2Console(H2ConsoleProperties properties) { + public ServletRegistrationBean h2Console(H2ConsoleProperties properties, + ObjectProvider dataSource) { String path = properties.getPath(); String urlMapping = path + (path.endsWith("/") ? "*" : "/*"); ServletRegistrationBean registration = new ServletRegistrationBean<>(new WebServlet(), urlMapping); @@ -55,6 +69,15 @@ public class H2ConsoleAutoConfiguration { if (settings.isWebAllowOthers()) { registration.addInitParameter("webAllowOthers", ""); } + dataSource.ifAvailable((available) -> { + try (Connection connection = available.getConnection()) { + logger.info("H2 console available at '" + path + "'. Database available at '" + + connection.getMetaData().getURL() + "'"); + } + catch (SQLException ex) { + // Continue + } + }); return registration; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java index 9ad7481170..700f92415c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java @@ -16,11 +16,21 @@ package org.springframework.boot.autoconfigure.h2; +import java.sql.Connection; +import java.sql.SQLException; + +import javax.sql.DataSource; + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanCreationException; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext; @@ -113,4 +123,15 @@ class H2ConsoleAutoConfigurationTests { assertThat(registrationBean.getInitParameters()).containsEntry("webAllowOthers", ""); } + @Test + @ExtendWith(OutputCaptureExtension.class) + void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput capturedOutput) throws BeansException, SQLException { + this.context.register(DataSourceAutoConfiguration.class, H2ConsoleAutoConfiguration.class); + TestPropertyValues.of("spring.h2.console.enabled:true").applyTo(this.context); + this.context.refresh(); + try (Connection connection = this.context.getBean(DataSource.class).getConnection()) { + assertThat(capturedOutput).contains("Database available at '" + connection.getMetaData().getURL() + "'"); + } + } + }