aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-application
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-03-30 23:00:55 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-03-30 23:02:01 +0200
commitb1e329a5182f8d611fdefe98ea7c3b5a54d2d7a3 (patch)
tree4b2cff21b1b9bb0b42ad3b5a7a255cd3c5fae984 /sonar-application
parentfd36d975984c356164d7ca95f7a32ca7f339d2c7 (diff)
downloadsonarqube-b1e329a5182f8d611fdefe98ea7c3b5a54d2d7a3.tar.gz
sonarqube-b1e329a5182f8d611fdefe98ea7c3b5a54d2d7a3.zip
SONAR-6293 refactor loading of default values of sonar.properties
Diffstat (limited to 'sonar-application')
-rw-r--r--sonar-application/src/main/assembly/conf/sonar.properties7
-rw-r--r--sonar-application/src/main/java/org/sonar/application/App.java26
-rw-r--r--sonar-application/src/main/java/org/sonar/application/DefaultSettings.java89
-rw-r--r--sonar-application/src/main/java/org/sonar/application/JdbcSettings.java6
-rw-r--r--sonar-application/src/main/java/org/sonar/application/PropsBuilder.java20
-rw-r--r--sonar-application/src/test/java/org/sonar/application/AppLoggingTest.java4
-rw-r--r--sonar-application/src/test/java/org/sonar/application/AppTest.java10
-rw-r--r--sonar-application/src/test/java/org/sonar/application/DefaultSettingsTest.java66
-rw-r--r--sonar-application/src/test/java/org/sonar/application/JdbcSettingsTest.java10
9 files changed, 44 insertions, 194 deletions
diff --git a/sonar-application/src/main/assembly/conf/sonar.properties b/sonar-application/src/main/assembly/conf/sonar.properties
index 9235874fa2e..d5209054161 100644
--- a/sonar-application/src/main/assembly/conf/sonar.properties
+++ b/sonar-application/src/main/assembly/conf/sonar.properties
@@ -220,7 +220,8 @@
# Elasticsearch port. Default is 9001. Use 0 to get a free port.
# This port must be private and must not be exposed to the Internet.
#sonar.search.port=9001
-# Elasticsearch host. The search server will bind this address and the search client will connect to it. Default ist 127.0.0.1.
+# Elasticsearch host. The search server will bind this address and the search client will connect to it.
+# Default is 127.0.0.1.
#sonar.search.host=127.0.0.1
@@ -306,6 +307,6 @@
# plugins not supported).
#sonar.web.dev.sources=/path/to/server/sonar-web/src/main/webapp
-# Uncomment to enable the Elasticsearch HTTP connector, so that ES can be directly requested through
+# Elasticsearch HTTP connector, for example for KOPF:
# http://lmenezes.com/elasticsearch-kopf/?location=http://localhost:9010
-#sonar.search.httpPort=9010
+#sonar.search.httpPort=-1
diff --git a/sonar-application/src/main/java/org/sonar/application/App.java b/sonar-application/src/main/java/org/sonar/application/App.java
index 68cb982a845..26130401dac 100644
--- a/sonar-application/src/main/java/org/sonar/application/App.java
+++ b/sonar-application/src/main/java/org/sonar/application/App.java
@@ -23,7 +23,7 @@ import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.process.MinimumViableSystem;
import org.sonar.process.ProcessCommands;
-import org.sonar.process.ProcessConstants;
+import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
import org.sonar.process.StopWatcher;
import org.sonar.process.Stoppable;
@@ -52,8 +52,8 @@ public class App implements Stoppable {
}
public void start(Props props) {
- if (props.valueAsBoolean(ProcessConstants.ENABLE_STOP_COMMAND, false)) {
- File tempDir = props.nonNullValueAsFile(ProcessConstants.PATH_TEMP);
+ if (props.valueAsBoolean(ProcessProperties.ENABLE_STOP_COMMAND, false)) {
+ File tempDir = props.nonNullValueAsFile(ProcessProperties.PATH_TEMP);
ProcessCommands commands = new ProcessCommands(tempDir, 0);
stopWatcher = new StopWatcher(commands, this);
stopWatcher.start();
@@ -64,14 +64,14 @@ public class App implements Stoppable {
List<JavaCommand> createCommands(Props props) {
List<JavaCommand> commands = new ArrayList<>();
- File homeDir = props.nonNullValueAsFile(ProcessConstants.PATH_HOME);
- File tempDir = props.nonNullValueAsFile(ProcessConstants.PATH_TEMP);
+ File homeDir = props.nonNullValueAsFile(ProcessProperties.PATH_HOME);
+ File tempDir = props.nonNullValueAsFile(ProcessProperties.PATH_TEMP);
JavaCommand elasticsearch = new JavaCommand("search");
elasticsearch
.setWorkDir(homeDir)
.addJavaOptions("-Djava.awt.headless=true")
- .addJavaOptions(props.nonNullValue(ProcessConstants.SEARCH_JAVA_OPTS))
- .addJavaOptions(props.nonNullValue(ProcessConstants.SEARCH_JAVA_ADDITIONAL_OPTS))
+ .addJavaOptions(props.nonNullValue(ProcessProperties.SEARCH_JAVA_OPTS))
+ .addJavaOptions(props.nonNullValue(ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS))
.setTempDir(tempDir.getAbsoluteFile())
.setClassName("org.sonar.search.SearchServer")
.setArguments(props.rawProperties())
@@ -80,20 +80,20 @@ public class App implements Stoppable {
commands.add(elasticsearch);
// do not yet start SQ on elasticsearch slaves
- if (StringUtils.isBlank(props.value(ProcessConstants.CLUSTER_MASTER_HOST))) {
+ if (StringUtils.isBlank(props.value(ProcessProperties.CLUSTER_MASTER_HOST))) {
JavaCommand webServer = new JavaCommand("web")
.setWorkDir(homeDir)
- .addJavaOptions(DefaultSettings.WEB_SERVER_FORCED_JVM_ARGS)
- .addJavaOptions(props.nonNullValue(ProcessConstants.WEB_JAVA_OPTS))
- .addJavaOptions(props.nonNullValue(ProcessConstants.WEB_JAVA_ADDITIONAL_OPTS))
+ .addJavaOptions(ProcessProperties.WEB_ENFORCED_JVM_ARGS)
+ .addJavaOptions(props.nonNullValue(ProcessProperties.WEB_JAVA_OPTS))
+ .addJavaOptions(props.nonNullValue(ProcessProperties.WEB_JAVA_ADDITIONAL_OPTS))
.setTempDir(tempDir.getAbsoluteFile())
// required for logback tomcat valve
- .setEnvVariable(ProcessConstants.PATH_LOGS, props.nonNullValue(ProcessConstants.PATH_LOGS))
+ .setEnvVariable(ProcessProperties.PATH_LOGS, props.nonNullValue(ProcessProperties.PATH_LOGS))
.setClassName("org.sonar.server.app.WebServer")
.setArguments(props.rawProperties())
.addClasspath("./lib/common/*")
.addClasspath("./lib/server/*");
- String driverPath = props.value(ProcessConstants.JDBC_DRIVER_PATH);
+ String driverPath = props.value(ProcessProperties.JDBC_DRIVER_PATH);
if (driverPath != null) {
webServer.addClasspath(driverPath);
}
diff --git a/sonar-application/src/main/java/org/sonar/application/DefaultSettings.java b/sonar-application/src/main/java/org/sonar/application/DefaultSettings.java
deleted file mode 100644
index f06d5fed675..00000000000
--- a/sonar-application/src/main/java/org/sonar/application/DefaultSettings.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.application;
-
-import org.sonar.process.NetworkUtils;
-import org.sonar.process.ProcessConstants;
-import org.sonar.process.Props;
-
-import java.util.HashMap;
-import java.util.Map;
-
-class DefaultSettings {
-
- public static final String WEB_SERVER_FORCED_JVM_ARGS = "-Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djruby.management.enabled=false " +
- // jruby is slow with java 8: https://jira.codehaus.org/browse/SONAR-6115
- "-Djruby.compile.invokedynamic=false";
-
- private DefaultSettings() {
- // only static stuff
- }
-
- static void init(Props props) {
- // forced property
- props.set(ProcessConstants.SEARCH_TYPE, "TRANSPORT");
-
- // init string properties
- for (Map.Entry<String, String> entry : defaults().entrySet()) {
- props.setDefault(entry.getKey(), entry.getValue());
- }
-
- // init ports
- for (Map.Entry<String, Integer> entry : defaultPorts().entrySet()) {
- String key = entry.getKey();
- int port = props.valueAsInt(key, -1);
- if (port == -1) {
- // default port
- props.set(key, String.valueOf((int) entry.getValue()));
- } else if (port == 0) {
- // pick one available port
- props.set(key, String.valueOf(NetworkUtils.freePort()));
- }
- }
- }
-
- private static Map<String, String> defaults() {
- Map<String, String> defaults = new HashMap<>();
- defaults.put(ProcessConstants.CLUSTER_NAME, "sonarqube");
- defaults.put(ProcessConstants.SEARCH_JAVA_OPTS, "-Xmx1G -Xms256m -Xss256k -Djava.net.preferIPv4Stack=true " +
- "-XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly " +
- "-XX:+HeapDumpOnOutOfMemoryError");
- defaults.put(ProcessConstants.SEARCH_JAVA_ADDITIONAL_OPTS, "");
- defaults.put(ProcessConstants.CLUSTER_NODE_NAME, "sonar-" + System.currentTimeMillis());
- defaults.put(ProcessConstants.WEB_JAVA_OPTS, "-Xmx768m -XX:MaxPermSize=160m -XX:+HeapDumpOnOutOfMemoryError -Djava.net.preferIPv4Stack=true");
- defaults.put(ProcessConstants.WEB_JAVA_ADDITIONAL_OPTS, "");
- defaults.put(ProcessConstants.JDBC_URL, "jdbc:h2:tcp://localhost:9092/sonar");
- defaults.put(ProcessConstants.JDBC_LOGIN, "sonar");
- defaults.put(ProcessConstants.JDBC_PASSWORD, "sonar");
- defaults.put(ProcessConstants.JDBC_MAX_ACTIVE, "50");
- defaults.put(ProcessConstants.JDBC_MAX_IDLE, "5");
- defaults.put(ProcessConstants.JDBC_MIN_IDLE, "2");
- defaults.put(ProcessConstants.JDBC_MAX_WAIT, "5000");
- defaults.put(ProcessConstants.JDBC_MIN_EVICTABLE_IDLE_TIME_MILLIS, "600000");
- defaults.put(ProcessConstants.JDBC_TIME_BETWEEN_EVICTION_RUNS_MILLIS, "30000");
- return defaults;
- }
-
- private static Map<String, Integer> defaultPorts() {
- Map<String, Integer> defaults = new HashMap<>();
- defaults.put(ProcessConstants.SEARCH_PORT, 9001);
- return defaults;
- }
-}
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 c949b1fae45..ea867c74b6a 100644
--- a/sonar-application/src/main/java/org/sonar/application/JdbcSettings.java
+++ b/sonar-application/src/main/java/org/sonar/application/JdbcSettings.java
@@ -23,7 +23,7 @@ import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.sonar.process.MessageException;
-import org.sonar.process.ProcessConstants;
+import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
import java.io.File;
@@ -46,11 +46,11 @@ public class JdbcSettings {
}
public void checkAndComplete(File homeDir, Props props) {
- String url = props.nonNullValue(ProcessConstants.JDBC_URL);
+ String url = props.nonNullValue(ProcessProperties.JDBC_URL);
Provider provider = driverProvider(url);
checkUrlParameters(provider, url);
String driverPath = driverPath(homeDir, provider);
- props.set(ProcessConstants.JDBC_DRIVER_PATH, driverPath);
+ props.set(ProcessProperties.JDBC_DRIVER_PATH, driverPath);
}
String driverPath(File homeDir, Provider provider) {
diff --git a/sonar-application/src/main/java/org/sonar/application/PropsBuilder.java b/sonar-application/src/main/java/org/sonar/application/PropsBuilder.java
index 652dd4cf814..167b8a3e0c7 100644
--- a/sonar-application/src/main/java/org/sonar/application/PropsBuilder.java
+++ b/sonar-application/src/main/java/org/sonar/application/PropsBuilder.java
@@ -21,8 +21,9 @@ package org.sonar.application;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
import org.sonar.process.ConfigurationUtils;
-import org.sonar.process.ProcessConstants;
+import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
import java.io.File;
@@ -56,19 +57,19 @@ class PropsBuilder {
Props build() throws IOException {
Properties p = loadPropertiesFile(homeDir);
p.putAll(rawProperties);
- p.setProperty(ProcessConstants.PATH_HOME, homeDir.getAbsolutePath());
+ p.setProperty(ProcessProperties.PATH_HOME, homeDir.getAbsolutePath());
p = ConfigurationUtils.interpolateVariables(p, System.getenv());
// the difference between Properties and Props is that the latter
// supports decryption of values, so it must be used when values
// are accessed
Props props = new Props(p);
- DefaultSettings.init(props);
+ ProcessProperties.completeDefaults(props);
// init file system
- initExistingDir(props, ProcessConstants.PATH_DATA, "data");
- initExistingDir(props, ProcessConstants.PATH_WEB, "web");
- initExistingDir(props, ProcessConstants.PATH_LOGS, "logs");
+ initExistingDir(props, ProcessProperties.PATH_DATA, "data");
+ initExistingDir(props, ProcessProperties.PATH_WEB, "web");
+ initExistingDir(props, ProcessProperties.PATH_LOGS, "logs");
initTempDir(props);
// check JDBC properties and set path to driver
@@ -86,15 +87,18 @@ class PropsBuilder {
Properties p = new Properties();
File propsFile = new File(homeDir, "conf/sonar.properties");
if (propsFile.exists()) {
- try (Reader reader = new InputStreamReader(new FileInputStream(propsFile), Charsets.UTF_8)) {
+ Reader reader = new InputStreamReader(new FileInputStream(propsFile), Charsets.UTF_8);
+ try {
p.load(reader);
+ } finally {
+ IOUtils.closeQuietly(reader);
}
}
return p;
}
private void initTempDir(Props props) throws IOException {
- File dir = configureDir(props, ProcessConstants.PATH_TEMP, "temp");
+ File dir = configureDir(props, ProcessProperties.PATH_TEMP, "temp");
FileUtils.deleteQuietly(dir);
FileUtils.forceMkdir(dir);
}
diff --git a/sonar-application/src/test/java/org/sonar/application/AppLoggingTest.java b/sonar-application/src/test/java/org/sonar/application/AppLoggingTest.java
index e2c902aaf39..fc187f3e4a4 100644
--- a/sonar-application/src/test/java/org/sonar/application/AppLoggingTest.java
+++ b/sonar-application/src/test/java/org/sonar/application/AppLoggingTest.java
@@ -31,7 +31,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.process.LogbackHelper;
-import org.sonar.process.ProcessConstants;
+import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
import java.io.File;
@@ -50,7 +50,7 @@ public class AppLoggingTest {
@Before
public void setUp() throws Exception {
File dir = temp.newFolder();
- props.set(ProcessConstants.PATH_LOGS, dir.getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, dir.getAbsolutePath());
}
@AfterClass
diff --git a/sonar-application/src/test/java/org/sonar/application/AppTest.java b/sonar-application/src/test/java/org/sonar/application/AppTest.java
index d5bbf56d6bc..027e00eea97 100644
--- a/sonar-application/src/test/java/org/sonar/application/AppTest.java
+++ b/sonar-application/src/test/java/org/sonar/application/AppTest.java
@@ -24,7 +24,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentCaptor;
-import org.sonar.process.ProcessConstants;
+import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
import org.sonar.process.monitor.JavaCommand;
import org.sonar.process.monitor.Monitor;
@@ -122,10 +122,10 @@ public class AppTest {
private Props initDefaultProps() throws IOException {
Props props = new Props(new Properties());
- DefaultSettings.init(props);
- props.set(ProcessConstants.PATH_HOME, temp.newFolder().getAbsolutePath());
- props.set(ProcessConstants.PATH_TEMP, temp.newFolder().getAbsolutePath());
- props.set(ProcessConstants.PATH_LOGS, temp.newFolder().getAbsolutePath());
+ ProcessProperties.completeDefaults(props);
+ props.set(ProcessProperties.PATH_HOME, temp.newFolder().getAbsolutePath());
+ props.set(ProcessProperties.PATH_TEMP, temp.newFolder().getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, temp.newFolder().getAbsolutePath());
return props;
}
}
diff --git a/sonar-application/src/test/java/org/sonar/application/DefaultSettingsTest.java b/sonar-application/src/test/java/org/sonar/application/DefaultSettingsTest.java
deleted file mode 100644
index 65825455863..00000000000
--- a/sonar-application/src/test/java/org/sonar/application/DefaultSettingsTest.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.application;
-
-import org.junit.Test;
-import org.sonar.process.Props;
-import org.sonar.test.TestUtils;
-
-import java.util.Properties;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class DefaultSettingsTest {
-
- @Test
- public void init_defaults() throws Exception {
- Props props = new Props(new Properties());
- DefaultSettings.init(props);
-
- assertThat(props.value("sonar.search.javaOpts")).contains("-Xmx");
- assertThat(props.value("sonar.jdbc.username")).isEqualTo("sonar");
- assertThat(props.valueAsInt("sonar.jdbc.maxActive")).isEqualTo(50);
- }
-
- @Test
- public void do_not_override_existing_properties() throws Exception {
- Properties p = new Properties();
- p.setProperty("sonar.jdbc.username", "angela");
- Props props = new Props(p);
- DefaultSettings.init(props);
-
- assertThat(props.value("sonar.jdbc.username")).isEqualTo("angela");
- }
-
- @Test
- public void use_random_port_if_zero() throws Exception {
- Properties p = new Properties();
- p.setProperty("sonar.search.port", "0");
- Props props = new Props(p);
-
- DefaultSettings.init(props);
- assertThat(props.valueAsInt("sonar.search.port")).isGreaterThan(0);
- }
-
- @Test
- public void private_constructor() throws Exception {
- assertThat(TestUtils.hasOnlyPrivateConstructors(DefaultSettings.class)).isTrue();
- }
-}
diff --git a/sonar-application/src/test/java/org/sonar/application/JdbcSettingsTest.java b/sonar-application/src/test/java/org/sonar/application/JdbcSettingsTest.java
index c68e3e1d465..d6d575bfad1 100644
--- a/sonar-application/src/test/java/org/sonar/application/JdbcSettingsTest.java
+++ b/sonar-application/src/test/java/org/sonar/application/JdbcSettingsTest.java
@@ -24,7 +24,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.process.MessageException;
-import org.sonar.process.ProcessConstants;
+import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
import java.io.File;
@@ -88,7 +88,7 @@ public class JdbcSettingsTest {
Props props = new Props(new Properties());
props.set("sonar.jdbc.url", "jdbc:oracle:thin:@localhost/XE");
settings.checkAndComplete(home, props);
- assertThat(props.nonNullValueAsFile(ProcessConstants.JDBC_DRIVER_PATH)).isEqualTo(driverFile);
+ assertThat(props.nonNullValueAsFile(ProcessProperties.JDBC_DRIVER_PATH)).isEqualTo(driverFile);
}
@Test
@@ -100,7 +100,7 @@ public class JdbcSettingsTest {
Props props = new Props(new Properties());
props.set("sonar.jdbc.url", "jdbc:h2:tcp://localhost:9092/sonar");
settings.checkAndComplete(home, props);
- assertThat(props.nonNullValueAsFile(ProcessConstants.JDBC_DRIVER_PATH)).isEqualTo(driverFile);
+ assertThat(props.nonNullValueAsFile(ProcessProperties.JDBC_DRIVER_PATH)).isEqualTo(driverFile);
}
@Test
@@ -112,7 +112,7 @@ public class JdbcSettingsTest {
Props props = new Props(new Properties());
props.set("sonar.jdbc.url", "jdbc:postgresql://localhost/sonar");
settings.checkAndComplete(home, props);
- assertThat(props.nonNullValueAsFile(ProcessConstants.JDBC_DRIVER_PATH)).isEqualTo(driverFile);
+ assertThat(props.nonNullValueAsFile(ProcessProperties.JDBC_DRIVER_PATH)).isEqualTo(driverFile);
}
@Test
@@ -124,7 +124,7 @@ public class JdbcSettingsTest {
Props props = new Props(new Properties());
props.set("sonar.jdbc.url", "jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor");
settings.checkAndComplete(home, props);
- assertThat(props.nonNullValueAsFile(ProcessConstants.JDBC_DRIVER_PATH)).isEqualTo(driverFile);
+ assertThat(props.nonNullValueAsFile(ProcessProperties.JDBC_DRIVER_PATH)).isEqualTo(driverFile);
}
@Test