accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From els...@apache.org
Subject accumulo git commit: ACCUMULO-3306 Simplify PortUtils.getRandomFreePort
Date Mon, 08 Dec 2014 02:10:03 GMT
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 <elserj@apache.org>
Authored: Sun Dec 7 20:55:42 2014 -0500
Committer: Josh Elser <elserj@apache.org>
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");
   }
-  
 }


Mime
View raw message