This is an automated email from the ASF dual-hosted git repository. orpiske 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 4762b1fe990 CAMEL-20264: Removed unused `AvailablePortFinder`, simplified another one 4762b1fe990 is described below commit 4762b1fe99034316f99a3fe5606d4e8bf6c5de69 Author: Thomas Gantenbein <n...@none.ch> AuthorDate: Fri Jan 5 20:46:32 2024 +0100 CAMEL-20264: Removed unused `AvailablePortFinder`, simplified another one --- .../camel/management/util/AvailablePortFinder.java | 58 ---------------------- .../core/commands/process/AvailablePortFinder.java | 50 ++----------------- 2 files changed, 4 insertions(+), 104 deletions(-) diff --git a/core/camel-management/src/test/java/org/apache/camel/management/util/AvailablePortFinder.java b/core/camel-management/src/test/java/org/apache/camel/management/util/AvailablePortFinder.java deleted file mode 100644 index 26cf46d1a3a..00000000000 --- a/core/camel-management/src/test/java/org/apache/camel/management/util/AvailablePortFinder.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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.management.util; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.ServerSocket; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Finds currently available server ports. - */ -public final class AvailablePortFinder { - - private static final Logger LOG = LoggerFactory.getLogger(AvailablePortFinder.class); - - /** - * Creates a new instance. - */ - private AvailablePortFinder() { - // Do nothing - } - - /** - * Gets the next available port. - * - * @throws IllegalStateException if there are no ports available - * @return the available port - */ - public static int getNextAvailable() { - try (ServerSocket ss = new ServerSocket()) { - ss.setReuseAddress(true); - ss.bind(new InetSocketAddress((InetAddress) null, 0), 1); - int port = ss.getLocalPort(); - LOG.info("getNextAvailable() -> {}", port); - return port; - } catch (IOException e) { - throw new IllegalStateException("Cannot find free port", e); - } - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/AvailablePortFinder.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/AvailablePortFinder.java index a8f10df6978..80b7af8baeb 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/AvailablePortFinder.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/AvailablePortFinder.java @@ -20,44 +20,9 @@ import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.ServerSocket; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; class AvailablePortFinder { - private static final AvailablePortFinder INSTANCE = new AvailablePortFinder(); - - private class Port implements AutoCloseable { - final int port; - - public Port(int port) { - this.port = port; - } - - public int getPort() { - return port; - } - - public void release() { - AvailablePortFinder.this.release(this); - } - - public String toString() { - return Integer.toString(port); - } - - @Override - public void close() { - release(); - } - } - - private final Map<Integer, Port> portMapping = new ConcurrentHashMap<>(); - - synchronized void release(Port port) { - INSTANCE.portMapping.remove(port.getPort(), port); - } - /** * Gets the next available port in the given range. * @@ -67,21 +32,14 @@ class AvailablePortFinder { * @throws IllegalStateException if there are no ports available * @return the available port */ - public static int getNextAvailable(int fromPort, int toPort) { - try (Port port = INSTANCE.findPort(fromPort, toPort)) { - return port.getPort(); - } + static int getNextAvailable(int fromPort, int toPort) { + return findPort(fromPort, toPort); } - synchronized Port findPort(int fromPort, int toPort) { + private static int findPort(int fromPort, int toPort) { for (int i = fromPort; i <= toPort; i++) { try { - final int port = probePort(i); - Port p = new Port(port); - Port prv = INSTANCE.portMapping.putIfAbsent(port, p); - if (prv == null) { - return p; - } + return probePort(i); } catch (IllegalStateException e) { // do nothing, let's try the next port }