You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by sj...@apache.org on 2014/12/19 13:28:07 UTC

[3/5] incubator-brooklyn git commit: nextAvailablePort, give better error messages

nextAvailablePort, give better error messages

Validate that the given argument is in the valid range for port numbers.
If not possible to find a port in the valid range, throw a descriptive
exception message (instead of trying an invalid port number and getting
an IllegalArgumentException from the JRE)


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/22e1c68d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/22e1c68d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/22e1c68d

Branch: refs/heads/master
Commit: 22e1c68d8e0561e6e9152e1ab0c3c2083af5b32e
Parents: c4d907a
Author: Richard Downer <ri...@apache.org>
Authored: Thu Dec 18 14:01:06 2014 +0000
Committer: Richard Downer <ri...@apache.org>
Committed: Fri Dec 19 09:33:24 2014 +0000

----------------------------------------------------------------------
 utils/common/src/main/java/brooklyn/util/net/Networking.java | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/22e1c68d/utils/common/src/main/java/brooklyn/util/net/Networking.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/brooklyn/util/net/Networking.java b/utils/common/src/main/java/brooklyn/util/net/Networking.java
index d338080..98fd828 100644
--- a/utils/common/src/main/java/brooklyn/util/net/Networking.java
+++ b/utils/common/src/main/java/brooklyn/util/net/Networking.java
@@ -48,6 +48,8 @@ import com.google.common.base.Throwables;
 import com.google.common.net.HostAndPort;
 import com.google.common.primitives.UnsignedBytes;
 
+import static com.google.common.base.Preconditions.checkArgument;
+
 public class Networking {
 
     private static final Logger log = LoggerFactory.getLogger(Networking.class);
@@ -148,7 +150,11 @@ public class Networking {
     }
     /** returns the first port available on the local machine >= the port supplied */
     public static int nextAvailablePort(int port) {
-        while (!isPortAvailable(port)) port++;
+        checkArgument(port >= MIN_PORT_NUMBER && port <= MAX_PORT_NUMBER, "requested port %s is outside the valid range of %s to %s", port, MIN_PORT_NUMBER, MAX_PORT_NUMBER);
+        int originalPort = port;
+        while (!isPortAvailable(port) && port <= MAX_PORT_NUMBER) port++;
+        if (port > MAX_PORT_NUMBER)
+            throw new RuntimeException("unable to find a free port at or above " + originalPort);
         return port;
     }