Return-Path: X-Original-To: apmail-accumulo-commits-archive@www.apache.org Delivered-To: apmail-accumulo-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1A5E810099 for ; Mon, 8 Dec 2014 02:10:04 +0000 (UTC) Received: (qmail 78332 invoked by uid 500); 8 Dec 2014 02:10:04 -0000 Delivered-To: apmail-accumulo-commits-archive@accumulo.apache.org Received: (qmail 78300 invoked by uid 500); 8 Dec 2014 02:10:03 -0000 Mailing-List: contact commits-help@accumulo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@accumulo.apache.org Delivered-To: mailing list commits@accumulo.apache.org Received: (qmail 78291 invoked by uid 99); 8 Dec 2014 02:10:03 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Dec 2014 02:10:03 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 9E6CA9C01FC; Mon, 8 Dec 2014 02:10:03 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: elserj@apache.org To: commits@accumulo.apache.org Message-Id: <70a21f7ce17145e1ae0b3c1fc380f81e@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: accumulo git commit: ACCUMULO-3306 Simplify PortUtils.getRandomFreePort Date: Mon, 8 Dec 2014 02:10:03 +0000 (UTC) Repository: accumulo Updated Branches: refs/heads/master 175401f9e -> 53991fe7b ACCUMULO-3306 Simplify PortUtils.getRandomFreePort Remove the computation of a "random" port number and just use the OS provided feature of asking to bind the socket on '0'. Fixed a bug where the count variable was not incremented. Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/53991fe7 Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/53991fe7 Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/53991fe7 Branch: refs/heads/master Commit: 53991fe7b05023b0cea5894c14040fc213b2be4e Parents: 175401f Author: Josh Elser Authored: Sun Dec 7 20:55:42 2014 -0500 Committer: Josh Elser Committed: Sun Dec 7 20:55:42 2014 -0500 ---------------------------------------------------------------------- .../org/apache/accumulo/server/util/PortUtils.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/53991fe7/server/base/src/main/java/org/apache/accumulo/server/util/PortUtils.java ---------------------------------------------------------------------- diff --git a/server/base/src/main/java/org/apache/accumulo/server/util/PortUtils.java b/server/base/src/main/java/org/apache/accumulo/server/util/PortUtils.java index f710b0f..c50d05b 100644 --- a/server/base/src/main/java/org/apache/accumulo/server/util/PortUtils.java +++ b/server/base/src/main/java/org/apache/accumulo/server/util/PortUtils.java @@ -23,29 +23,26 @@ import java.util.Random; public class PortUtils { public static int getRandomFreePort() { - Random r = new Random(); int count = 0; - + while (count < 13) { - int port = r.nextInt((1 << 16) - 1024) + 1024; - ServerSocket so = null; try { - so = new ServerSocket(port); + so = new ServerSocket(0); so.setReuseAddress(true); - return port; + return so.getLocalPort(); } catch (IOException ioe) { - + } finally { if (so != null) try { so.close(); } catch (IOException e) {} } - + + count++; } - + throw new RuntimeException("Unable to find port"); } - }