diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-05-12 12:04:26 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-05-12 15:43:47 +0200 |
commit | 8e8f9fc1cc8b9a113778027dfe9ac59c00d332d0 (patch) | |
tree | f81eba1e162387b76b984f25563883bea7b071dc /sonar-application/src/main/java | |
parent | f1994051ac83659f201160049afe39ae5b26dcfb (diff) | |
download | sonarqube-8e8f9fc1cc8b9a113778027dfe9ac59c00d332d0.tar.gz sonarqube-8e8f9fc1cc8b9a113778027dfe9ac59c00d332d0.zip |
SONAR-7580 jdbc_url default value is computed or set by JdbcSettings
it used to be hardcoded to jdbc:h2:tcp://localhost:9092/sonar, which did not take into account the optionaly specified embedded database port property
Diffstat (limited to 'sonar-application/src/main/java')
-rw-r--r-- | sonar-application/src/main/java/org/sonar/application/JdbcSettings.java | 55 |
1 files changed, 52 insertions, 3 deletions
diff --git a/sonar-application/src/main/java/org/sonar/application/JdbcSettings.java b/sonar-application/src/main/java/org/sonar/application/JdbcSettings.java index 00a9a87353e..2f00b97ebc6 100644 --- a/sonar-application/src/main/java/org/sonar/application/JdbcSettings.java +++ b/sonar-application/src/main/java/org/sonar/application/JdbcSettings.java @@ -26,11 +26,19 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.process.MessageException; import org.sonar.process.ProcessProperties; import org.sonar.process.Props; +import static org.apache.commons.lang.StringUtils.isEmpty; +import static org.apache.commons.lang.StringUtils.isNotEmpty; +import static org.sonar.api.database.DatabaseProperties.PROP_EMBEDDED_PORT; +import static org.sonar.api.database.DatabaseProperties.PROP_EMBEDDED_PORT_DEFAULT_VALUE; +import static org.sonar.api.database.DatabaseProperties.PROP_URL; +import static org.sonar.process.ProcessProperties.JDBC_URL; + public class JdbcSettings { enum Provider { @@ -45,8 +53,8 @@ public class JdbcSettings { } public void checkAndComplete(File homeDir, Props props) { - String url = props.nonNullValue(ProcessProperties.JDBC_URL); - Provider provider = driverProvider(url); + Provider provider = resolveProviderAndEnforceNonnullJdbcUrl(props); + String url = props.value(JDBC_URL); checkUrlParameters(provider, url); String driverPath = driverPath(homeDir, provider); props.set(ProcessProperties.JDBC_DRIVER_PATH, driverPath); @@ -68,7 +76,23 @@ public class JdbcSettings { return files.get(0).getAbsolutePath(); } - Provider driverProvider(String url) { + Provider resolveProviderAndEnforceNonnullJdbcUrl(Props props) { + String url = props.value(JDBC_URL); + String embeddedDatabasePort = props.value(PROP_EMBEDDED_PORT); + + if (isNotEmpty(embeddedDatabasePort)) { + String correctUrl = buildH2JdbcUrl(embeddedDatabasePort); + warnIfUrlIsSet(embeddedDatabasePort, url, correctUrl); + props.set(PROP_URL, correctUrl); + return Provider.H2; + } + + if (isEmpty(url)) { + props.set(PROP_URL, buildH2JdbcUrl(PROP_EMBEDDED_PORT_DEFAULT_VALUE)); + props.set(PROP_EMBEDDED_PORT, PROP_EMBEDDED_PORT_DEFAULT_VALUE); + return Provider.H2; + } + Pattern pattern = Pattern.compile("jdbc:(\\w+):.+"); Matcher matcher = pattern.matcher(url); if (!matcher.find()) { @@ -82,6 +106,10 @@ public class JdbcSettings { } } + private static String buildH2JdbcUrl(String embeddedDatabasePort) { + return "jdbc:h2:tcp://localhost:" + embeddedDatabasePort + "/sonar"; + } + void checkUrlParameters(Provider provider, String url) { if (Provider.MYSQL.equals(provider)) { checkRequiredParameter(url, "useUnicode=true"); @@ -91,6 +119,27 @@ public class JdbcSettings { } } + private static void warnIfUrlIsSet(String port, String existing, String expectedUrl) { + if (isNotEmpty(existing)) { + Logger logger = LoggerFactory.getLogger(JdbcSettings.class); + if (expectedUrl.equals(existing)) { + logger.warn("To change H2 database port, only property '{}' should be set (which current value is '{}'). " + + "Remove property '{}' from configuration to remove this warning.", + PROP_EMBEDDED_PORT, port, + PROP_URL); + } else { + logger.warn("Both '{}' and '{}' properties are set. " + + "The value of property '{}' ('{}') is not consistent with the value of property '{}' ('{}'). " + + "The value of property '{}' will be ignored and value '{}' will be used instead. " + + "To remove this warning, either remove property '{}' if your intent was to use the embedded H2 database, otherwise remove property '{}'.", + PROP_EMBEDDED_PORT, PROP_URL, + PROP_URL, existing, PROP_EMBEDDED_PORT, port, + PROP_URL, expectedUrl, + PROP_URL, PROP_EMBEDDED_PORT); + } + } + } + private static void checkRequiredParameter(String url, String val) { if (!url.contains(val)) { throw new MessageException(String.format("JDBC URL must have the property '%s'", val)); |