aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-process/src
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2014-12-30 20:44:23 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2014-12-30 21:03:22 +0100
commit42785ab206ff561da7e9d5b6ac6e23ccd589124a (patch)
treefe7500999d08b31d98b3b202006ea3406eeddb1e /server/sonar-process/src
parent8e4c0d557d1c3e29eceba28c64eec0327b7812dc (diff)
downloadsonarqube-42785ab206ff561da7e9d5b6ac6e23ccd589124a.tar.gz
sonarqube-42785ab206ff561da7e9d5b6ac6e23ccd589124a.zip
Improve stability of randomly allocated ports (when sonar.web.port or sonar.search.port are set to zero)
Diffstat (limited to 'server/sonar-process/src')
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/NetworkUtils.java20
1 files changed, 13 insertions, 7 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/NetworkUtils.java b/server/sonar-process/src/main/java/org/sonar/process/NetworkUtils.java
index 0443d5148be..44fcfb7677d 100644
--- a/server/sonar-process/src/main/java/org/sonar/process/NetworkUtils.java
+++ b/server/sonar-process/src/main/java/org/sonar/process/NetworkUtils.java
@@ -21,6 +21,8 @@ package org.sonar.process;
import org.apache.commons.io.IOUtils;
+import java.io.IOException;
+import java.net.InetSocketAddress;
import java.net.ServerSocket;
public class NetworkUtils {
@@ -33,14 +35,18 @@ public class NetworkUtils {
* Get an unused port
*/
public static int freePort() {
- ServerSocket s = null;
- try {
- s = new ServerSocket(0);
- return s.getLocalPort();
- } catch (Exception e) {
- throw new IllegalStateException("Can not find an open network port", e);
+ ServerSocket socket = null;
+ try {
+ socket = new ServerSocket();
+ socket.setReuseAddress(true);
+ socket.bind(new InetSocketAddress(0));
+ return socket.getLocalPort();
+
+ } catch (IOException e) {
+ throw new IllegalStateException("Can not find a free network port", e);
+
} finally {
- IOUtils.closeQuietly(s);
+ IOUtils.closeQuietly(socket);
}
}
}