--- /dev/null
+/*
+ * 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.io.IOException;
+import java.net.ServerSocket;
+
+public class NetworkUtils {
+
+ public static int freePort() {
+ try {
+ ServerSocket s = new ServerSocket(0);
+ int port = s.getLocalPort();
+ s.close();
+ return port;
+ } catch (IOException e) {
+ throw new IllegalStateException("Can not find an open network port", e);
+ }
+ }
+}
--- /dev/null
+/*
+ * 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 java.net.ServerSocket;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class NetworkUtilsTest {
+
+
+ @Test
+ public void find_free_port() throws Exception {
+ int port = NetworkUtils.freePort();
+ assertThat(port).isGreaterThan(1024);
+ }
+
+ @Test
+ public void find_multiple_free_port() throws Exception {
+ int port1 = NetworkUtils.freePort();
+ int port2 = NetworkUtils.freePort();
+
+ assertThat(port1).isGreaterThan(1024);
+ assertThat(port2).isGreaterThan(1024);
+
+ assertThat(port1).isNotSameAs(port2);
+ }
+
+ @Test
+ public void find_multiple_free_non_adjacent_port() throws Exception {
+ int port1 = NetworkUtils.freePort();
+
+ ServerSocket socket = new ServerSocket(port1 + 1);
+
+ int port2 = NetworkUtils.freePort();
+
+ assertThat(port1).isGreaterThan(1024);
+ assertThat(port2).isGreaterThan(1024);
+
+ assertThat(port1).isNotSameAs(port2);
+ }
+}
\ No newline at end of file
+++ /dev/null
-/*
- * 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 private int lastPort = -1;
-
- static int freePort() {
- try {
- ServerSocket s = new ServerSocket(lastPort + 1);
- int port = s.getLocalPort();
- s.close();
- return port;
- } catch (IOException e) {
- throw new IllegalStateException("Can not find an open network port", e);
- }
- }
-
- private static boolean isValidPort(int port) {
- return port > 1023;
- }
-}
+++ /dev/null
-/*
- * 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 org.junit.Test;
-
-import java.net.ServerSocket;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class NetworkUtilsTest {
-
-
- @Test
- public void find_free_port() throws Exception {
- int port = NetworkUtils.freePort();
- assertThat(port).isGreaterThan(1024);
- }
-
- @Test
- public void find_multiple_free_port() throws Exception {
- int port1 = NetworkUtils.freePort();
- int port2 = NetworkUtils.freePort();
-
- assertThat(port1).isGreaterThan(1024);
- assertThat(port2).isGreaterThan(1024);
-
- assertThat(port1).isNotSameAs(port2);
- }
-
- @Test
- public void find_multiple_free_non_adjacent_port() throws Exception {
- int port1 = NetworkUtils.freePort();
-
- ServerSocket socket = new ServerSocket(port1 + 1);
-
- int port2 = NetworkUtils.freePort();
-
- assertThat(port1).isGreaterThan(1024);
- assertThat(port2).isGreaterThan(1024);
-
- assertThat(port1).isNotSameAs(port2);
- }
-}
\ No newline at end of file