-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathAvailablePortFinder.java
156 lines (138 loc) · 4.44 KB
/
AvailablePortFinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* @(#) $Id$
*
* Copyright 2004 The Apache Software Foundation
*
* Licensed 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.mina.util;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.TreeSet;
/**
* Finds currently available server ports.
*
* @author The Apache Directory Project (mina-dev@directory.apache.org)
* @version $Rev$
* @see <a href="http://www.iana.org/assignments/port-numbers">IANA.org</a>
*/
public class AvailablePortFinder {
/**
* The minimum number of server port number.
*/
public static final int MIN_PORT_NUMBER = 1;
/**
* The maximum number of server port number.
*/
public static final int MAX_PORT_NUMBER = 49151;
/**
* Checks to see if a specific port is available.
*
* @param port the port to check for availability
*/
public static boolean available(final int port) {
if ((port < MIN_PORT_NUMBER) || (port > MAX_PORT_NUMBER)) {
throw new IllegalArgumentException("Invalid start port: " + port);
}
ServerSocket ss = null;
DatagramSocket ds = null;
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
ds = new DatagramSocket(port);
ds.setReuseAddress(true);
return true;
} catch (final IOException e) {
} finally {
if (ds != null) {
ds.close();
}
if (ss != null) {
try {
ss.close();
} catch (final IOException e) {
/* should not be thrown */
}
}
}
return false;
}
/**
* Returns the {@link Set} of currently available port numbers ({@link Integer}). This method is identical to
* <code>getAvailablePorts(MIN_PORT_NUMBER, MAX_PORT_NUMBER)</code>.
*
* WARNING: this can take a very long time.
*/
public static Set<Integer> getAvailablePorts() {
return getAvailablePorts(MIN_PORT_NUMBER, MAX_PORT_NUMBER);
}
/**
* Returns the {@link Set} of currently avaliable port numbers ({@link Integer}) between the specified port range.
*
* @throws IllegalArgumentException if port range is not between {@link #MIN_PORT_NUMBER} and {@link #MAX_PORT_NUMBER}
* or <code>fromPort</code> if greater than <code>toPort</code>.
*/
public static Set<Integer> getAvailablePorts(final int fromPort, final int toPort) {
if ((fromPort < MIN_PORT_NUMBER) || (toPort > MAX_PORT_NUMBER) || (fromPort > toPort)) {
throw new IllegalArgumentException("Invalid port range: " + fromPort + " ~ " + toPort);
}
final Set<Integer> result = new TreeSet<Integer>();
for (int i = fromPort; i <= toPort; i++) {
ServerSocket s = null;
try {
s = new ServerSocket(i);
result.add(new Integer(i));
} catch (final IOException e) {
} finally {
if (s != null) {
try {
s.close();
} catch (final IOException e) {
/* should not be thrown */
}
}
}
}
return result;
}
/**
* Gets the next available port starting at the lowest port number.
*
* @throws NoSuchElementException if there are no ports available
*/
public static int getNextAvailable() {
return getNextAvailable(MIN_PORT_NUMBER);
}
/**
* Gets the next available port starting at a port.
*
* @param fromPort the port to scan for availability
* @throws NoSuchElementException if there are no ports available
*/
public static int getNextAvailable(final int fromPort) {
if ((fromPort < MIN_PORT_NUMBER) || (fromPort > MAX_PORT_NUMBER)) {
throw new IllegalArgumentException("Invalid start port: " + fromPort);
}
for (int i = fromPort; i <= MAX_PORT_NUMBER; i++) {
if (available(i)) {
return i;
}
}
throw new NoSuchElementException("Could not find an available port " + "above " + fromPort);
}
/**
* Creates a new instance.
*/
private AvailablePortFinder() {
}
}