001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.test;
018
019 import java.io.IOException;
020 import java.net.DatagramSocket;
021 import java.net.ServerSocket;
022 import java.util.NoSuchElementException;
023
024 /**
025 * Finds currently available server ports.
026 *
027 * @see <a href="http://www.iana.org/assignments/port-numbers">IANA.org</a>
028 */
029 public final class AvailablePortFinder {
030 /**
031 * The minimum server port number. Set at 1024 to avoid returning privileged
032 * port numbers.
033 */
034 public static final int MIN_PORT_NUMBER = 1024;
035
036 /**
037 * The maximum server port number.
038 */
039 public static final int MAX_PORT_NUMBER = 49151;
040
041 /**
042 * Creates a new instance.
043 */
044 private AvailablePortFinder() {
045 // Do nothing
046 }
047
048 /**
049 * Gets the next available port starting at the lowest port number.
050 *
051 * @throws NoSuchElementException if there are no ports available
052 */
053 public static int getNextAvailable() {
054 return getNextAvailable(MIN_PORT_NUMBER);
055 }
056
057 /**
058 * Gets the next available port starting at a port.
059 *
060 * @param fromPort the port to scan for availability
061 * @throws NoSuchElementException if there are no ports available
062 */
063 public static int getNextAvailable(int fromPort) {
064 if (fromPort < MIN_PORT_NUMBER || fromPort > MAX_PORT_NUMBER) {
065 throw new IllegalArgumentException("Invalid start port: " + fromPort);
066 }
067
068 for (int i = fromPort; i <= MAX_PORT_NUMBER; i++) {
069 if (available(i)) {
070 return i;
071 }
072 }
073
074 throw new NoSuchElementException("Could not find an available port above " + fromPort);
075 }
076
077 /**
078 * Checks to see if a specific port is available.
079 *
080 * @param port the port to check for availability
081 */
082 public static boolean available(int port) {
083 if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
084 throw new IllegalArgumentException("Invalid start port: " + port);
085 }
086
087 ServerSocket ss = null;
088 DatagramSocket ds = null;
089 try {
090 ss = new ServerSocket(port);
091 ss.setReuseAddress(true);
092 ds = new DatagramSocket(port);
093 ds.setReuseAddress(true);
094 return true;
095 } catch (IOException e) {
096 // Do nothing
097 } finally {
098 if (ds != null) {
099 ds.close();
100 }
101
102 if (ss != null) {
103 try {
104 ss.close();
105 } catch (IOException e) {
106 /* should not be thrown */
107 }
108 }
109 }
110
111 return false;
112 }
113 }