summaryrefslogtreecommitdiffstats
path: root/server/sonar-process
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 /server/sonar-process
parentfd36d975984c356164d7ca95f7a32ca7f339d2c7 (diff)
downloadsonarqube-b1e329a5182f8d611fdefe98ea7c3b5a54d2d7a3.tar.gz
sonarqube-b1e329a5182f8d611fdefe98ea7c3b5a54d2d7a3.zip
SONAR-6293 refactor loading of default values of sonar.properties
Diffstat (limited to 'server/sonar-process')
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java2
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/ProcessConstants.java72
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/ProcessProperties.java134
-rw-r--r--server/sonar-process/src/test/java/org/sonar/process/LogbackHelperTest.java2
-rw-r--r--server/sonar-process/src/test/java/org/sonar/process/ProcessPropertiesTest.java65
5 files changed, 201 insertions, 74 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java b/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java
index cb9bb6a8080..5ed5459c0c8 100644
--- a/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java
+++ b/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java
@@ -94,7 +94,7 @@ public class LogbackHelper {
public RollingPolicy createRollingPolicy(Context ctx, Props props, String filenamePrefix) {
String rollingPolicy = props.value(ROLLING_POLICY_PROPERTY, "time:yyyy-MM-dd");
int maxFiles = props.valueAsInt(MAX_FILES_PROPERTY, 7);
- File logsDir = props.nonNullValueAsFile(ProcessConstants.PATH_LOGS);
+ File logsDir = props.nonNullValueAsFile(ProcessProperties.PATH_LOGS);
if (rollingPolicy.startsWith("time:")) {
return new TimeRollingPolicy(ctx, filenamePrefix, logsDir, maxFiles, StringUtils.substringAfter(rollingPolicy, "time:"));
diff --git a/server/sonar-process/src/main/java/org/sonar/process/ProcessConstants.java b/server/sonar-process/src/main/java/org/sonar/process/ProcessConstants.java
deleted file mode 100644
index 4a078369d38..00000000000
--- a/server/sonar-process/src/main/java/org/sonar/process/ProcessConstants.java
+++ /dev/null
@@ -1,72 +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.process;
-
-/**
- * Constants shared by search, web server and monitor processes.
- * It represents more or less all the properties commented in conf/sonar.properties
- */
-public interface ProcessConstants {
-
- String CLUSTER_ACTIVATE = "sonar.cluster.activate";
- String CLUSTER_MASTER = "sonar.cluster.master";
- String CLUSTER_MASTER_HOST = "sonar.cluster.masterHost";
- String CLUSTER_NAME = "sonar.cluster.name";
- String CLUSTER_NODE_NAME = "sonar.node.name";
-
- String JDBC_URL = "sonar.jdbc.url";
- String JDBC_LOGIN = "sonar.jdbc.username";
- String JDBC_PASSWORD = "sonar.jdbc.password";
- String JDBC_DRIVER_PATH = "sonar.jdbc.driverPath";
- String JDBC_MAX_ACTIVE = "sonar.jdbc.maxActive";
- String JDBC_MAX_IDLE = "sonar.jdbc.maxIdle";
- String JDBC_MIN_IDLE = "sonar.jdbc.minIdle";
- String JDBC_MAX_WAIT = "sonar.jdbc.maxWait";
- String JDBC_MIN_EVICTABLE_IDLE_TIME_MILLIS = "sonar.jdbc.minEvictableIdleTimeMillis";
- String JDBC_TIME_BETWEEN_EVICTION_RUNS_MILLIS = "sonar.jdbc.timeBetweenEvictionRunsMillis";
-
- String PATH_DATA = "sonar.path.data";
- String PATH_HOME = "sonar.path.home";
- String PATH_LOGS = "sonar.path.logs";
- String PATH_TEMP = "sonar.path.temp";
- String PATH_WEB = "sonar.path.web";
-
- String SEARCH_HOST = "sonar.search.host";
- String SEARCH_PORT = "sonar.search.port";
- String SEARCH_JAVA_OPTS = "sonar.search.javaOpts";
- String SEARCH_JAVA_ADDITIONAL_OPTS = "sonar.search.javaAdditionalOpts";
- String SEARCH_TYPE = "sonar.search.type";
-
- String WEB_JAVA_OPTS = "sonar.web.javaOpts";
- String WEB_JAVA_ADDITIONAL_OPTS = "sonar.web.javaAdditionalOpts";
-
- /**
- * Used by Orchestrator to ask for shutdown of monitor process
- */
- String ENABLE_STOP_COMMAND = "sonar.enableStopCommand";
-
- // Constants declared by the ES plugin ListUpdate (see sonar-search)
- // that are used by sonar-server
- String ES_PLUGIN_LISTUPDATE_SCRIPT_NAME = "listUpdate";
- String ES_PLUGIN_LISTUPDATE_ID_FIELD = "idField";
- String ES_PLUGIN_LISTUPDATE_ID_VALUE = "idValue";
- String ES_PLUGIN_LISTUPDATE_FIELD = "field";
- String ES_PLUGIN_LISTUPDATE_VALUE = "value";
-}
diff --git a/server/sonar-process/src/main/java/org/sonar/process/ProcessProperties.java b/server/sonar-process/src/main/java/org/sonar/process/ProcessProperties.java
new file mode 100644
index 00000000000..9532d3bace2
--- /dev/null
+++ b/server/sonar-process/src/main/java/org/sonar/process/ProcessProperties.java
@@ -0,0 +1,134 @@
+/*
+ * 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.process;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Constants shared by search, web server and app processes.
+ * They are almost all the properties defined in conf/sonar.properties.
+ */
+public class ProcessProperties {
+
+ public static final String CLUSTER_ACTIVATE = "sonar.cluster.activate";
+ public static final String CLUSTER_MASTER = "sonar.cluster.master";
+ public static final String CLUSTER_MASTER_HOST = "sonar.cluster.masterHost";
+ public static final String CLUSTER_NAME = "sonar.cluster.name";
+ public static final String CLUSTER_NODE_NAME = "sonar.node.name";
+
+ public static final String JDBC_URL = "sonar.jdbc.url";
+ public static final String JDBC_LOGIN = "sonar.jdbc.username";
+ public static final String JDBC_PASSWORD = "sonar.jdbc.password";
+ public static final String JDBC_DRIVER_PATH = "sonar.jdbc.driverPath";
+ public static final String JDBC_MAX_ACTIVE = "sonar.jdbc.maxActive";
+ public static final String JDBC_MAX_IDLE = "sonar.jdbc.maxIdle";
+ public static final String JDBC_MIN_IDLE = "sonar.jdbc.minIdle";
+ public static final String JDBC_MAX_WAIT = "sonar.jdbc.maxWait";
+ public static final String JDBC_MIN_EVICTABLE_IDLE_TIME_MILLIS = "sonar.jdbc.minEvictableIdleTimeMillis";
+ public static final String JDBC_TIME_BETWEEN_EVICTION_RUNS_MILLIS = "sonar.jdbc.timeBetweenEvictionRunsMillis";
+
+ public static final String PATH_DATA = "sonar.path.data";
+ public static final String PATH_HOME = "sonar.path.home";
+ public static final String PATH_LOGS = "sonar.path.logs";
+ public static final String PATH_TEMP = "sonar.path.temp";
+ public static final String PATH_WEB = "sonar.path.web";
+
+ public static final String SEARCH_HOST = "sonar.search.host";
+ public static final String SEARCH_PORT = "sonar.search.port";
+ public static final String SEARCH_HTTP_PORT = "sonar.search.httpPort";
+ public static final String SEARCH_JAVA_OPTS = "sonar.search.javaOpts";
+ public static final String SEARCH_JAVA_ADDITIONAL_OPTS = "sonar.search.javaAdditionalOpts";
+
+ public static final String WEB_JAVA_OPTS = "sonar.web.javaOpts";
+ public static final String WEB_JAVA_ADDITIONAL_OPTS = "sonar.web.javaAdditionalOpts";
+
+ /**
+ * Used by Orchestrator to ask for shutdown of monitor process
+ */
+ public static final String ENABLE_STOP_COMMAND = "sonar.enableStopCommand";
+
+ // Constants declared by the ES plugin ListUpdate (see sonar-search)
+ // that are used by sonar-server
+ public static final String ES_PLUGIN_LISTUPDATE_SCRIPT_NAME = "listUpdate";
+ public static final String ES_PLUGIN_LISTUPDATE_ID_FIELD = "idField";
+ public static final String ES_PLUGIN_LISTUPDATE_ID_VALUE = "idValue";
+ public static final String ES_PLUGIN_LISTUPDATE_FIELD = "field";
+ public static final String ES_PLUGIN_LISTUPDATE_VALUE = "value";
+
+ public static final String WEB_ENFORCED_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 ProcessProperties() {
+ // only static stuff
+ }
+
+ public static void completeDefaults(Props props) {
+ // 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()));
+ }
+ }
+ }
+
+ public static Map<String, String> defaults() {
+ Map<String, String> defaults = new HashMap<>();
+ defaults.put(ProcessProperties.CLUSTER_NAME, "sonarqube");
+ defaults.put(ProcessProperties.CLUSTER_NODE_NAME, "sonar-" + System.currentTimeMillis());
+
+ defaults.put(ProcessProperties.SEARCH_HOST, "127.0.0.1");
+ defaults.put(ProcessProperties.SEARCH_JAVA_OPTS, "-Xmx1G -Xms256m -Xss256k -Djava.net.preferIPv4Stack=true " +
+ "-XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly " +
+ "-XX:+HeapDumpOnOutOfMemoryError");
+ defaults.put(ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS, "");
+
+ defaults.put(ProcessProperties.WEB_JAVA_OPTS, "-Xmx768m -XX:MaxPermSize=160m -XX:+HeapDumpOnOutOfMemoryError -Djava.net.preferIPv4Stack=true");
+ defaults.put(ProcessProperties.WEB_JAVA_ADDITIONAL_OPTS, "");
+ defaults.put(ProcessProperties.JDBC_URL, "jdbc:h2:tcp://localhost:9092/sonar");
+ defaults.put(ProcessProperties.JDBC_LOGIN, "sonar");
+ defaults.put(ProcessProperties.JDBC_PASSWORD, "sonar");
+ defaults.put(ProcessProperties.JDBC_MAX_ACTIVE, "50");
+ defaults.put(ProcessProperties.JDBC_MAX_IDLE, "5");
+ defaults.put(ProcessProperties.JDBC_MIN_IDLE, "2");
+ defaults.put(ProcessProperties.JDBC_MAX_WAIT, "5000");
+ defaults.put(ProcessProperties.JDBC_MIN_EVICTABLE_IDLE_TIME_MILLIS, "600000");
+ defaults.put(ProcessProperties.JDBC_TIME_BETWEEN_EVICTION_RUNS_MILLIS, "30000");
+ return defaults;
+ }
+
+ private static Map<String, Integer> defaultPorts() {
+ Map<String, Integer> defaults = new HashMap<>();
+ defaults.put(ProcessProperties.SEARCH_PORT, 9001);
+ return defaults;
+ }
+}
diff --git a/server/sonar-process/src/test/java/org/sonar/process/LogbackHelperTest.java b/server/sonar-process/src/test/java/org/sonar/process/LogbackHelperTest.java
index 667a5632ebe..753d06e67c4 100644
--- a/server/sonar-process/src/test/java/org/sonar/process/LogbackHelperTest.java
+++ b/server/sonar-process/src/test/java/org/sonar/process/LogbackHelperTest.java
@@ -54,7 +54,7 @@ public class LogbackHelperTest {
@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/server/sonar-process/src/test/java/org/sonar/process/ProcessPropertiesTest.java b/server/sonar-process/src/test/java/org/sonar/process/ProcessPropertiesTest.java
new file mode 100644
index 00000000000..2a8cdddd3d7
--- /dev/null
+++ b/server/sonar-process/src/test/java/org/sonar/process/ProcessPropertiesTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.process;
+
+import org.junit.Test;
+import org.sonar.test.TestUtils;
+
+import java.util.Properties;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ProcessPropertiesTest {
+
+ @Test
+ public void init_defaults() throws Exception {
+ Props props = new Props(new Properties());
+ ProcessProperties.completeDefaults(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);
+ ProcessProperties.completeDefaults(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);
+
+ ProcessProperties.completeDefaults(props);
+ assertThat(props.valueAsInt("sonar.search.port")).isGreaterThan(0);
+ }
+
+ @Test
+ public void private_constructor() throws Exception {
+ assertThat(TestUtils.hasOnlyPrivateConstructors(ProcessProperties.class)).isTrue();
+ }
+}