This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new f642c3ff4375 CAMEL-23214: Deprecate
AvailablePortFinder.getNextAvailable() in favor of find() (#22130)
f642c3ff4375 is described below
commit f642c3ff4375d17b4fd2e9563daa9cc46b9496b7
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Mar 20 09:23:46 2026 +0100
CAMEL-23214: Deprecate AvailablePortFinder.getNextAvailable() in favor of
find() (#22130)
* CAMEL-23214: Deprecate AvailablePortFinder.getNextAvailable() in favor of
find()
The getNextAvailable() method immediately releases the reserved port,
creating a TOCTOU race condition where another process can grab the port
before the caller binds to it. The find() method returns a Port object
that stays reserved until released via @RegisterExtension or close().
Also deprecate getNextRandomAvailable(), getNextAvailable(int,int),
getSpecificPort(), and probePort() for the same reason.
Co-Authored-By: Claude Opus 4.6 <[email protected]>
* CAMEL-23214: Apply formatter to AvailablePortFinder deprecation javadoc
Co-Authored-By: Claude Opus 4.6 <[email protected]>
* CAMEL-23214: Make AvailablePort package-private
AvailablePort is an implementation detail used only by
AvailablePortFinder. Migrate the single external user
(GreeterClientTest) to inline the ServerSocket probe, then
change AvailablePort to final package-private class.
Co-Authored-By: Claude Opus 4.6 <[email protected]>
---------
Co-authored-by: Claude Opus 4.6 <[email protected]>
---
.../java/org/apache/camel/test/AvailablePort.java | 2 +-
.../org/apache/camel/test/AvailablePortFinder.java | 51 ++++++++++++++--------
.../java/org/apache/camel/test/AvailablePort.java | 2 +-
.../org/apache/camel/test/AvailablePortFinder.java | 51 ++++++++++++++--------
.../camel/itest/security/GreeterClientTest.java | 11 ++---
.../apache/camel/tooling/maven/AvailablePort.java | 2 +-
6 files changed, 71 insertions(+), 48 deletions(-)
diff --git
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePort.java
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePort.java
index 5140766b31c2..ee020f04ecb5 100644
---
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePort.java
+++
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePort.java
@@ -24,7 +24,7 @@ import java.net.ServerSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class AvailablePort {
+final class AvailablePort {
private static final Logger LOG =
LoggerFactory.getLogger(AvailablePort.class);
/**
diff --git
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePortFinder.java
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePortFinder.java
index d66dc0894c3a..aba383889c04 100644
---
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePortFinder.java
+++
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePortFinder.java
@@ -123,9 +123,14 @@ public final class AvailablePortFinder {
/**
* Gets the next available port.
*
- * @throws IllegalStateException if there are no ports available
- * @return the available port
+ * @throws IllegalStateException if there are no ports available
+ * @return the available port
+ * @deprecated use {@link #find()} instead, which
returns a {@link Port} that reserves the
+ * port until released. This method
immediately releases the port, creating a race
+ * condition where another process can
grab the port before the caller binds to
+ * it.
*/
+ @Deprecated
public static int getNextAvailable() {
try (Port port = INSTANCE.findPort()) {
return port.getPort();
@@ -135,9 +140,11 @@ public final class AvailablePortFinder {
/**
* Gets the next available port.
*
- * @throws IllegalStateException if there are no ports available
- * @return the available port
+ * @throws IllegalStateException if there are no ports available
+ * @return the available port
+ * @deprecated use {@link #find()} instead
*/
+ @Deprecated
public static int getNextRandomAvailable() {
Random random = new Random(); // NOSONAR
int fromPort = random.nextInt(10000, 65500);
@@ -150,12 +157,14 @@ public final class AvailablePortFinder {
/**
* Gets the next available port in the given range.
*
- * @param fromPort port number start range.
- * @param toPort port number end range.
+ * @param fromPort port number start range.
+ * @param toPort port number end range.
*
- * @throws IllegalStateException if there are no ports available
- * @return the available port
+ * @throws IllegalStateException if there are no ports available
+ * @return the available port
+ * @deprecated use {@link #find()} instead
*/
+ @Deprecated
public static int getNextAvailable(int fromPort, int toPort) {
try (Port port = INSTANCE.findPort(fromPort, toPort)) {
return port.getPort();
@@ -165,13 +174,15 @@ public final class AvailablePortFinder {
/**
* Gets the next available port in the given range.
*
- * @param portNumber port number start range.
- * @param failurePayload handover data in case port allocation
fails (i.e.: a default one to use)
- * @param failureHandler a handler in case the requested port is
not available
+ * @param portNumber port number start range.
+ * @param failurePayload handover data in case port allocation
fails (i.e.: a default one to use)
+ * @param failureHandler a handler in case the requested port
is not available
*
- * @throws IllegalStateException if there are no ports available
- * @return the available port
+ * @throws IllegalStateException if there are no ports available
+ * @return the available port
+ * @deprecated use {@link #find()} instead
*/
+ @Deprecated
public static <T> int getSpecificPort(int portNumber, T failurePayload,
Function<T, Integer> failureHandler) {
try (Port port = INSTANCE.findPort(portNumber, portNumber)) {
return port.getPort();
@@ -189,13 +200,15 @@ public final class AvailablePortFinder {
/**
* Probe a port to see if it is free
*
- * @param port an integer port number to be tested. If
port is 0, then the next available port is
- * returned.
- * @throws IllegalStateException if the port is not free or, in case of
port 0, if there are no ports available at
- * all.
- * @return the port number itself if the port is
free or, in case of port 0, the first
- * available port number.
+ * @param port an integer port number to be tested.
If port is 0, then the next available port
+ * is returned.
+ * @throws IllegalStateException if the port is not free or, in case
of port 0, if there are no ports available
+ * at all.
+ * @return the port number itself if the port is
free or, in case of port 0, the first
+ * available port number.
+ * @deprecated internal API, do not use directly
*/
+ @Deprecated
public static int probePort(int port) {
return AvailablePort.probePort(null, port);
}
diff --git
a/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/AvailablePort.java
b/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/AvailablePort.java
index 5140766b31c2..ee020f04ecb5 100644
---
a/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/AvailablePort.java
+++
b/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/AvailablePort.java
@@ -24,7 +24,7 @@ import java.net.ServerSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class AvailablePort {
+final class AvailablePort {
private static final Logger LOG =
LoggerFactory.getLogger(AvailablePort.class);
/**
diff --git
a/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/AvailablePortFinder.java
b/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/AvailablePortFinder.java
index d66dc0894c3a..aba383889c04 100644
---
a/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/AvailablePortFinder.java
+++
b/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/AvailablePortFinder.java
@@ -123,9 +123,14 @@ public final class AvailablePortFinder {
/**
* Gets the next available port.
*
- * @throws IllegalStateException if there are no ports available
- * @return the available port
+ * @throws IllegalStateException if there are no ports available
+ * @return the available port
+ * @deprecated use {@link #find()} instead, which
returns a {@link Port} that reserves the
+ * port until released. This method
immediately releases the port, creating a race
+ * condition where another process can
grab the port before the caller binds to
+ * it.
*/
+ @Deprecated
public static int getNextAvailable() {
try (Port port = INSTANCE.findPort()) {
return port.getPort();
@@ -135,9 +140,11 @@ public final class AvailablePortFinder {
/**
* Gets the next available port.
*
- * @throws IllegalStateException if there are no ports available
- * @return the available port
+ * @throws IllegalStateException if there are no ports available
+ * @return the available port
+ * @deprecated use {@link #find()} instead
*/
+ @Deprecated
public static int getNextRandomAvailable() {
Random random = new Random(); // NOSONAR
int fromPort = random.nextInt(10000, 65500);
@@ -150,12 +157,14 @@ public final class AvailablePortFinder {
/**
* Gets the next available port in the given range.
*
- * @param fromPort port number start range.
- * @param toPort port number end range.
+ * @param fromPort port number start range.
+ * @param toPort port number end range.
*
- * @throws IllegalStateException if there are no ports available
- * @return the available port
+ * @throws IllegalStateException if there are no ports available
+ * @return the available port
+ * @deprecated use {@link #find()} instead
*/
+ @Deprecated
public static int getNextAvailable(int fromPort, int toPort) {
try (Port port = INSTANCE.findPort(fromPort, toPort)) {
return port.getPort();
@@ -165,13 +174,15 @@ public final class AvailablePortFinder {
/**
* Gets the next available port in the given range.
*
- * @param portNumber port number start range.
- * @param failurePayload handover data in case port allocation
fails (i.e.: a default one to use)
- * @param failureHandler a handler in case the requested port is
not available
+ * @param portNumber port number start range.
+ * @param failurePayload handover data in case port allocation
fails (i.e.: a default one to use)
+ * @param failureHandler a handler in case the requested port
is not available
*
- * @throws IllegalStateException if there are no ports available
- * @return the available port
+ * @throws IllegalStateException if there are no ports available
+ * @return the available port
+ * @deprecated use {@link #find()} instead
*/
+ @Deprecated
public static <T> int getSpecificPort(int portNumber, T failurePayload,
Function<T, Integer> failureHandler) {
try (Port port = INSTANCE.findPort(portNumber, portNumber)) {
return port.getPort();
@@ -189,13 +200,15 @@ public final class AvailablePortFinder {
/**
* Probe a port to see if it is free
*
- * @param port an integer port number to be tested. If
port is 0, then the next available port is
- * returned.
- * @throws IllegalStateException if the port is not free or, in case of
port 0, if there are no ports available at
- * all.
- * @return the port number itself if the port is
free or, in case of port 0, the first
- * available port number.
+ * @param port an integer port number to be tested.
If port is 0, then the next available port
+ * is returned.
+ * @throws IllegalStateException if the port is not free or, in case
of port 0, if there are no ports available
+ * at all.
+ * @return the port number itself if the port is
free or, in case of port 0, the first
+ * available port number.
+ * @deprecated internal API, do not use directly
*/
+ @Deprecated
public static int probePort(int port) {
return AvailablePort.probePort(null, port);
}
diff --git
a/tests/camel-itest/src/test/java/org/apache/camel/itest/security/GreeterClientTest.java
b/tests/camel-itest/src/test/java/org/apache/camel/itest/security/GreeterClientTest.java
index 4f43eeb60962..7224dd5fd165 100644
---
a/tests/camel-itest/src/test/java/org/apache/camel/itest/security/GreeterClientTest.java
+++
b/tests/camel-itest/src/test/java/org/apache/camel/itest/security/GreeterClientTest.java
@@ -16,8 +16,6 @@
*/
package org.apache.camel.itest.security;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
@@ -27,7 +25,7 @@ import jakarta.xml.ws.soap.SOAPFaultException;
import javax.xml.namespace.QName;
import org.apache.camel.CamelContext;
-import org.apache.camel.test.AvailablePort;
+import org.apache.camel.test.AvailablePortFinder;
import org.apache.camel.test.spring.junit6.CamelSpringTest;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
@@ -129,11 +127,10 @@ public class GreeterClientTest {
public static boolean isPortAvailable() {
try {
- AvailablePort.probePort(InetAddress.getByName("localhost"), 9000);
- } catch (IllegalStateException | UnknownHostException e) {
+ AvailablePortFinder.probePort(9000);
+ return true;
+ } catch (IllegalStateException e) {
return false;
}
-
- return true;
}
}
diff --git
a/tooling/camel-tooling-maven/src/test/java/org/apache/camel/tooling/maven/AvailablePort.java
b/tooling/camel-tooling-maven/src/test/java/org/apache/camel/tooling/maven/AvailablePort.java
index f60312851c2e..9c44c609e179 100644
---
a/tooling/camel-tooling-maven/src/test/java/org/apache/camel/tooling/maven/AvailablePort.java
+++
b/tooling/camel-tooling-maven/src/test/java/org/apache/camel/tooling/maven/AvailablePort.java
@@ -24,7 +24,7 @@ import java.net.ServerSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class AvailablePort {
+final class AvailablePort {
private static final Logger LOG =
LoggerFactory.getLogger(AvailablePort.class);