Author: davsclaus Date: Wed Jan 18 07:02:46 2012 New Revision: 1232762 URL: http://svn.apache.org/viewvc?rev=1232762&view=rev Log: Polished
Modified: camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/AvailablePortFinder.java camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/AvailablePortFinderTest.java Modified: camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/AvailablePortFinder.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/AvailablePortFinder.java?rev=1232762&r1=1232761&r2=1232762&view=diff ============================================================================== --- camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/AvailablePortFinder.java (original) +++ camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/AvailablePortFinder.java Wed Jan 18 07:02:46 2012 @@ -20,8 +20,14 @@ import java.io.IOException; import java.net.DatagramSocket; import java.net.ServerSocket; import java.util.NoSuchElementException; +import java.util.Random; +import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; +import org.apache.camel.util.IOHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Finds currently available server ports. @@ -29,6 +35,7 @@ import java.util.concurrent.atomic.Atomi * @see <a href="http://www.iana.org/assignments/currentMinPort-numbers">IANA.org</a> */ public final class AvailablePortFinder { + /** * The minimum server currentMinPort number. Set at 1100 to avoid returning privileged * currentMinPort numbers. @@ -40,6 +47,8 @@ public final class AvailablePortFinder { */ public static final int MAX_PORT_NUMBER = 49151; + private static final Logger LOG = LoggerFactory.getLogger(AvailablePortFinder.class); + /** * We'll hold open the lowest port in this process * so parallel processes won't use the same block @@ -83,14 +92,15 @@ public final class AvailablePortFinder { }); currentMinPort.set(port + 1); } - /** - * Gets the next available currentMinPort starting at the lowest currentMinPort number. This is the preferred + * Gets the next available port starting at the lowest number. This is the preferred * method to use. The port return is immediately marked in use and doesn't rely on the caller actually opening * the port. * + * @throws IllegalArgumentException is thrown if the port number is out of range * @throws NoSuchElementException if there are no ports available + * @return the available port */ public static synchronized int getNextAvailable() { int next = getNextAvailable(currentMinPort.get()); @@ -99,31 +109,36 @@ public final class AvailablePortFinder { } /** - * Gets the next available currentMinPort starting at a currentMinPort. + * Gets the next available port starting at a given from port. * - * @param fromPort the currentMinPort to scan for availability + * @param fromPort the from port to scan for availability + * @throws IllegalArgumentException is thrown if the port number is out of range * @throws NoSuchElementException if there are no ports available + * @return the available port */ public static synchronized int getNextAvailable(int fromPort) { if (fromPort < currentMinPort.get() || fromPort > MAX_PORT_NUMBER) { - throw new IllegalArgumentException("Invalid start currentMinPort: " + fromPort); + throw new IllegalArgumentException("From port number not in valid range: " + fromPort); } for (int i = fromPort; i <= MAX_PORT_NUMBER; i++) { if (available(i)) { + LOG.info("getNextAvailable({}) -> {}", fromPort, i); return i; } } - throw new NoSuchElementException("Could not find an available currentMinPort above " + fromPort); + throw new NoSuchElementException("Could not find an available port above " + fromPort); } /** - * Checks to see if a specific currentMinPort is available. + * Checks to see if a specific port is available. * - * @param port the currentMinPort to check for availability + * @param port the port number to check for availability + * @return <tt>true</tt> if the port is available, or <tt>false</tt> if not + * @throws IllegalArgumentException is thrown if the port number is out of range */ - public static boolean available(int port) { + public static boolean available(int port) throws IllegalArgumentException { if (port < currentMinPort.get() || port > MAX_PORT_NUMBER) { throw new IllegalArgumentException("Invalid start currentMinPort: " + port); } Modified: camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/AvailablePortFinderTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/AvailablePortFinderTest.java?rev=1232762&r1=1232761&r2=1232762&view=diff ============================================================================== --- camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/AvailablePortFinderTest.java (original) +++ camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/AvailablePortFinderTest.java Wed Jan 18 07:02:46 2012 @@ -23,7 +23,6 @@ import org.junit.Test; public class AvailablePortFinderTest { - @Test public void getNextAvailablePort() throws Exception { int p1 = AvailablePortFinder.getNextAvailable(); @@ -59,5 +58,4 @@ public class AvailablePortFinderTest { AvailablePortFinder.getNextAvailable(AvailablePortFinder.MAX_PORT_NUMBER + 1); } - }