SocketUtils considers port range including maxPort

Issue: SPR-14211
(cherry picked from commit bf7b475)
This commit is contained in:
Juergen Hoeller
2016-04-26 17:03:00 +02:00
parent b9fa67e0e6
commit 002d6d70a5
2 changed files with 43 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -192,14 +192,14 @@ public class SocketUtils {
}
private static enum SocketType {
private enum SocketType {
TCP {
@Override
protected boolean isPortAvailable(int port) {
try {
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(port, 1,
InetAddress.getByName("localhost"));
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(
port, 1, InetAddress.getByName("localhost"));
serverSocket.close();
return true;
}
@@ -238,7 +238,7 @@ public class SocketUtils {
*/
private int findRandomPort(int minPort, int maxPort) {
int portRange = maxPort - minPort;
return minPort + random.nextInt(portRange);
return minPort + random.nextInt(portRange + 1);
}
/**
@@ -251,7 +251,7 @@ public class SocketUtils {
*/
int findAvailablePort(int minPort, int maxPort) {
Assert.isTrue(minPort > 0, "'minPort' must be greater than 0");
Assert.isTrue(maxPort > minPort, "'maxPort' must be greater than 'minPort'");
Assert.isTrue(maxPort >= minPort, "'maxPort' must be greater than or equals 'minPort'");
Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX);
int portRange = maxPort - minPort;
@@ -260,8 +260,8 @@ public class SocketUtils {
do {
if (++searchCounter > portRange) {
throw new IllegalStateException(String.format(
"Could not find an available %s port in the range [%d, %d] after %d attempts", name(), minPort,
maxPort, searchCounter));
"Could not find an available %s port in the range [%d, %d] after %d attempts",
name(), minPort, maxPort, searchCounter));
}
candidatePort = findRandomPort(minPort, maxPort);
}
@@ -285,18 +285,18 @@ public class SocketUtils {
Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX);
Assert.isTrue(numRequested > 0, "'numRequested' must be greater than 0");
Assert.isTrue((maxPort - minPort) >= numRequested,
"'numRequested' must not be greater than 'maxPort' - 'minPort'");
"'numRequested' must not be greater than 'maxPort' - 'minPort'");
final SortedSet<Integer> availablePorts = new TreeSet<Integer>();
SortedSet<Integer> availablePorts = new TreeSet<Integer>();
int attemptCount = 0;
while ((++attemptCount <= numRequested + 100) && (availablePorts.size() < numRequested)) {
while ((++attemptCount <= numRequested + 100) && availablePorts.size() < numRequested) {
availablePorts.add(findAvailablePort(minPort, maxPort));
}
if (availablePorts.size() != numRequested) {
throw new IllegalStateException(String.format(
"Could not find %d available %s ports in the range [%d, %d]", numRequested, name(), minPort,
maxPort));
"Could not find %d available %s ports in the range [%d, %d]",
numRequested, name(), minPort, maxPort));
}
return availablePorts;