aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-start
diff options
context:
space:
mode:
authorStephane Gamard <stephane.gamard@searchbox.com>2014-07-11 00:56:41 +0200
committerStephane Gamard <stephane.gamard@searchbox.com>2014-07-11 00:56:41 +0200
commit5176c669e21dbb2de66c8c984b4f4de4a9347bcc (patch)
treea68deb4c409df1764513a90d2552c64ca827762b /sonar-start
parentc6e9206b43e5e95a2b09109e77068842667d80d5 (diff)
downloadsonarqube-5176c669e21dbb2de66c8c984b4f4de4a9347bcc.tar.gz
sonarqube-5176c669e21dbb2de66c8c984b4f4de4a9347bcc.zip
SONAR-4898 - Using NetworkUtils for ES port selection
Diffstat (limited to 'sonar-start')
-rw-r--r--sonar-start/src/main/java/org/sonar/start/NetworkUtils.java38
-rw-r--r--sonar-start/src/main/java/org/sonar/start/StartServer.java4
2 files changed, 41 insertions, 1 deletions
diff --git a/sonar-start/src/main/java/org/sonar/start/NetworkUtils.java b/sonar-start/src/main/java/org/sonar/start/NetworkUtils.java
new file mode 100644
index 00000000000..d9dd72e74b2
--- /dev/null
+++ b/sonar-start/src/main/java/org/sonar/start/NetworkUtils.java
@@ -0,0 +1,38 @@
+/*
+ * 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.start;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+
+class NetworkUtils {
+
+ static int freePort() {
+ try {
+ return new ServerSocket(0).getLocalPort();
+ } catch (IOException e) {
+ throw new IllegalStateException("Can not find an open network port", e);
+ }
+ }
+
+ private static boolean isValidPort(int port) {
+ return port > 1023;
+ }
+}
diff --git a/sonar-start/src/main/java/org/sonar/start/StartServer.java b/sonar-start/src/main/java/org/sonar/start/StartServer.java
index 07659dfcc00..997b5269c6f 100644
--- a/sonar-start/src/main/java/org/sonar/start/StartServer.java
+++ b/sonar-start/src/main/java/org/sonar/start/StartServer.java
@@ -57,7 +57,9 @@ public final class StartServer {
final ProcessWrapper elasticsearch = new ProcessWrapper(
"org.sonar.search.ElasticSearch",
new String[]{env.rootDir().getAbsolutePath() + "/lib/search/sonar-search-4.5-SNAPSHOT.jar"},
- ImmutableMap.of("esPort", "9200", "esHome", env.rootDir().getAbsolutePath()),
+ ImmutableMap.of(
+ "esPort", Integer.toString(NetworkUtils.freePort()),
+ "esHome", env.rootDir().getAbsolutePath()),
"ES", monitor.getMonitoringPort());
//Register processes to monitor