You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by tj...@apache.org on 2011/04/18 18:37:09 UTC
svn commit: r1094645 - in /camel/trunk/components/camel-test/src:
main/java/org/apache/camel/test/AvailablePortFinder.java
test/java/org/apache/camel/test/AvailablePortFinderTest.java
Author: tjsnell
Date: Mon Apr 18 16:37:09 2011
New Revision: 1094645
URL: http://svn.apache.org/viewvc?rev=1094645&view=rev
Log:
CAMEL-3578 - Added AtomicInteger to track lowest used port.
Added:
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
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=1094645&r1=1094644&r2=1094645&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 Mon Apr 18 16:37:09 2011
@@ -20,24 +20,31 @@ import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.util.NoSuchElementException;
+import java.util.concurrent.atomic.AtomicInteger;
/**
* Finds currently available server ports.
*
- * @see <a href="http://www.iana.org/assignments/port-numbers">IANA.org</a>
+ * @see <a href="http://www.iana.org/assignments/currentMinPort-numbers">IANA.org</a>
*/
public final class AvailablePortFinder {
/**
- * The minimum server port number. Set at 1024 to avoid returning privileged
- * port numbers.
+ * The minimum server currentMinPort number. Set at 1024 to avoid returning privileged
+ * currentMinPort numbers.
*/
public static final int MIN_PORT_NUMBER = 1024;
/**
- * The maximum server port number.
+ * The maximum server currentMinPort number.
*/
public static final int MAX_PORT_NUMBER = 49151;
+
+ /**
+ * Incremented to the next lowest available port when getNextAvailable() is called.
+ */
+ private static AtomicInteger currentMinPort = new AtomicInteger(MIN_PORT_NUMBER);
+
/**
* Creates a new instance.
*/
@@ -46,23 +53,27 @@ public final class AvailablePortFinder {
}
/**
- * Gets the next available port starting at the lowest port number.
+ * Gets the next available currentMinPort starting at the lowest currentMinPort 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 NoSuchElementException if there are no ports available
*/
public static int getNextAvailable() {
- return getNextAvailable(MIN_PORT_NUMBER);
+ int next = getNextAvailable(currentMinPort.get());
+ currentMinPort.set(next + 1);
+ return next;
}
/**
- * Gets the next available port starting at a port.
+ * Gets the next available currentMinPort starting at a currentMinPort.
*
- * @param fromPort the port to scan for availability
+ * @param fromPort the currentMinPort to scan for availability
* @throws NoSuchElementException if there are no ports available
*/
public static int getNextAvailable(int fromPort) {
- if (fromPort < MIN_PORT_NUMBER || fromPort > MAX_PORT_NUMBER) {
- throw new IllegalArgumentException("Invalid start port: " + fromPort);
+ if (fromPort < currentMinPort.get() || fromPort > MAX_PORT_NUMBER) {
+ throw new IllegalArgumentException("Invalid start currentMinPort: " + fromPort);
}
for (int i = fromPort; i <= MAX_PORT_NUMBER; i++) {
@@ -71,17 +82,17 @@ public final class AvailablePortFinder {
}
}
- throw new NoSuchElementException("Could not find an available port above " + fromPort);
+ throw new NoSuchElementException("Could not find an available currentMinPort above " + fromPort);
}
/**
- * Checks to see if a specific port is available.
+ * Checks to see if a specific currentMinPort is available.
*
- * @param port the port to check for availability
+ * @param port the currentMinPort to check for availability
*/
public static boolean available(int port) {
- if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
- throw new IllegalArgumentException("Invalid start port: " + port);
+ if (port < currentMinPort.get() || port > MAX_PORT_NUMBER) {
+ throw new IllegalArgumentException("Invalid start currentMinPort: " + port);
}
ServerSocket ss = null;
Added: 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=1094645&view=auto
==============================================================================
--- camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/AvailablePortFinderTest.java (added)
+++ camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/AvailablePortFinderTest.java Mon Apr 18 16:37:09 2011
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class AvailablePortFinderTest {
+
+
+ @Test
+ public void getNextAvailablePort() throws Exception {
+ int p1 = AvailablePortFinder.getNextAvailable();
+ int p2 = AvailablePortFinder.getNextAvailable();
+ Assert.assertTrue(p1 != p2);
+ }
+
+ @Test (expected = IllegalArgumentException.class)
+ public void getMinOutOfRangePort() throws Exception {
+ int p1 = AvailablePortFinder.getNextAvailable(AvailablePortFinder.MIN_PORT_NUMBER - 1);
+ }
+
+ @Test (expected = IllegalArgumentException.class)
+ public void getMaxOutOfRangePort() throws Exception {
+ int p1 = AvailablePortFinder.getNextAvailable(AvailablePortFinder.MAX_PORT_NUMBER + 1);
+ }
+
+
+}