Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.36 KB | None | 0 0
  1.  
  2. Sign in
  3. Sign up
  4. spring-projects /
  5. spring-framework
  6. nes (272 sloc) 11.1 KB
  7. /*
  8. * Copyright 2002-2019 the original author or authors.
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * https://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22.  
  23. package org.springframework.util;
  24.  
  25. import java.net.DatagramSocket;
  26. import java.net.InetAddress;
  27. import java.net.ServerSocket;
  28. import java.util.Random;
  29. import java.util.SortedSet;
  30. import java.util.TreeSet;
  31.  
  32. import javax.net.ServerSocketFactory;
  33.  
  34. /**
  35. * Simple utility methods for working with network sockets — for example,
  36. * for finding available ports on {@code localhost}.
  37. *
  38. * <p>Within this class, a TCP port refers to a port for a {@link ServerSocket};
  39. * whereas, a UDP port refers to a port for a {@link DatagramSocket}.
  40. *
  41. public class SocketUtils {
  42.  
  43. /**
  44. * The default minimum value for port ranges used when finding an available
  45. * socket port.
  46. */
  47. public static final int PORT_RANGE_MIN = 1024;
  48.  
  49. /**
  50. * The default maximum value for port ranges used when finding an available
  51. * socket port.
  52. */
  53. public static final int PORT_RANGE_MAX = 65535;
  54.  
  55.  
  56. private static final Random random = new Random(System.currentTimeMillis());
  57.  
  58.  
  59. /**
  60. * Although {@code SocketUtils} consists solely of static utility methods,
  61. * this constructor is intentionally {@code public}.
  62. * <h4>Rationale</h4>
  63. * <p>Static methods from this class may be invoked from within XML
  64. * configuration files using the Spring Expression Language (SpEL) and the
  65. * following syntax.
  66. * <pre><code>&lt;bean id="bean1" ... p:port="#{T(org.springframework.util.SocketUtils).findAvailableTcpPort(12000)}" /&gt;</code></pre>
  67. * If this constructor were {@code private}, you would be required to supply
  68. * the fully qualified class name to SpEL's {@code T()} function for each usage.
  69. * Thus, the fact that this constructor is {@code public} allows you to reduce
  70. * boilerplate configuration with SpEL as can be seen in the following example.
  71. * <pre><code>&lt;bean id="socketUtils" class="org.springframework.util.SocketUtils" /&gt;
  72. * &lt;bean id="bean1" ... p:port="#{socketUtils.findAvailableTcpPort(12000)}" /&gt;
  73. * &lt;bean id="bean2" ... p:port="#{socketUtils.findAvailableTcpPort(30000)}" /&gt;</code></pre>
  74. */
  75. public SocketUtils() {
  76. }
  77.  
  78.  
  79. /**
  80. * Find an available TCP port randomly selected from the range
  81. * [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}].
  82. * @return an available TCP port number
  83. * @throws IllegalStateException if no available port could be found
  84. */
  85. public static int findAvailableTcpPort() {
  86. return findAvailableTcpPort(PORT_RANGE_MIN);
  87. }
  88.  
  89. /**
  90. * Find an available TCP port randomly selected from the range
  91. * [{@code minPort}, {@value #PORT_RANGE_MAX}].
  92. * @param minPort the minimum port number
  93. * @return an available TCP port number
  94. * @throws IllegalStateException if no available port could be found
  95. */
  96. public static int findAvailableTcpPort(int minPort) {
  97. return findAvailableTcpPort(minPort, PORT_RANGE_MAX);
  98. }
  99.  
  100. /**
  101. * Find an available TCP port randomly selected from the range
  102. * [{@code minPort}, {@code maxPort}].
  103. * @param minPort the minimum port number
  104. * @param maxPort the maximum port number
  105. * @return an available TCP port number
  106. * @throws IllegalStateException if no available port could be found
  107. */
  108. public static int findAvailableTcpPort(int minPort, int maxPort) {
  109. return SocketType.TCP.findAvailablePort(minPort, maxPort);
  110. }
  111.  
  112. /**
  113. * Find the requested number of available TCP ports, each randomly selected
  114. * from the range [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}].
  115. * @param numRequested the number of available ports to find
  116. * @return a sorted set of available TCP port numbers
  117. * @throws IllegalStateException if the requested number of available ports could not be found
  118. */
  119. public static SortedSet<Integer> findAvailableTcpPorts(int numRequested) {
  120. return findAvailableTcpPorts(numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX);
  121. }
  122.  
  123. /**
  124. * Find the requested number of available TCP ports, each randomly selected
  125. * from the range [{@code minPort}, {@code maxPort}].
  126. * @param numRequested the number of available ports to find
  127. * @param minPort the minimum port number
  128. * @param maxPort the maximum port number
  129. * @return a sorted set of available TCP port numbers
  130. * @throws IllegalStateException if the requested number of available ports could not be found
  131. */
  132. public static SortedSet<Integer> findAvailableTcpPorts(int numRequested, int minPort, int maxPort) {
  133. return SocketType.TCP.findAvailablePorts(numRequested, minPort, maxPort);
  134. }
  135.  
  136. /**
  137. * Find an available UDP port randomly selected from the range
  138. * [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}].
  139. * @return an available UDP port number
  140. * @throws IllegalStateException if no available port could be found
  141. */
  142. public static int findAvailableUdpPort() {
  143. return findAvailableUdpPort(PORT_RANGE_MIN);
  144. }
  145.  
  146. /**
  147. * Find an available UDP port randomly selected from the range
  148. * [{@code minPort}, {@value #PORT_RANGE_MAX}].
  149. * @param minPort the minimum port number
  150. * @return an available UDP port number
  151. * @throws IllegalStateException if no available port could be found
  152. */
  153. public static int findAvailableUdpPort(int minPort) {
  154. return findAvailableUdpPort(minPort, PORT_RANGE_MAX);
  155. }
  156.  
  157. /**
  158. * Find an available UDP port randomly selected from the range
  159. * [{@code minPort}, {@code maxPort}].
  160. * @param minPort the minimum port number
  161. * @param maxPort the maximum port number
  162. * @return an available UDP port number
  163. * @throws IllegalStateException if no available port could be found
  164. */
  165. public static int findAvailableUdpPort(int minPort, int maxPort) {
  166. return SocketType.UDP.findAvailablePort(minPort, maxPort);
  167. }
  168.  
  169. /**
  170. * Find the requested number of available UDP ports, each randomly selected
  171. * from the range [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}].
  172. * @param numRequested the number of available ports to find
  173. * @return a sorted set of available UDP port numbers
  174. * @throws IllegalStateException if the requested number of available ports could not be found
  175. */
  176. public static SortedSet<Integer> findAvailableUdpPorts(int numRequested) {
  177. return findAvailableUdpPorts(numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX);
  178. }
  179.  
  180. /**
  181. * Find the requested number of available UDP ports, each randomly selected
  182. * from the range [{@code minPort}, {@code maxPort}].
  183. * @param numRequested the number of available ports to find
  184. * @param minPort the minimum port number
  185. * @param maxPort the maximum port number
  186. * @return a sorted set of available UDP port numbers
  187. * @throws IllegalStateException if the requested number of available ports could not be found
  188. */
  189. public static SortedSet<Integer> findAvailableUdpPorts(int numRequested, int minPort, int maxPort) {
  190. return SocketType.UDP.findAvailablePorts(numRequested, minPort, maxPort);
  191. }
  192.  
  193.  
  194. private enum SocketType {
  195.  
  196. TCP {
  197. @Override
  198. protected boolean isPortAvailable(int port) {
  199. try {
  200. ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(
  201. port, 1, InetAddress.getByName("localhost"));
  202. serverSocket.close();
  203. return true;
  204. }
  205. catch (Exception ex) {
  206. return false;
  207. }
  208. }
  209. },
  210.  
  211. UDP {
  212. @Override
  213. protected boolean isPortAvailable(int port) {
  214. try {
  215. DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost"));
  216. socket.close();
  217. return true;
  218. }
  219. catch (Exception ex) {
  220. return false;
  221. }
  222. }
  223. };
  224.  
  225. /**
  226. * Determine if the specified port for this {@code SocketType} is
  227. * currently available on {@code localhost}.
  228. */
  229. protected abstract boolean isPortAvailable(int port);
  230.  
  231. /**
  232. * Find a pseudo-random port number within the range
  233. * [{@code minPort}, {@code maxPort}].
  234. * @param minPort the minimum port number
  235. * @param maxPort the maximum port number
  236. * @return a random port number within the specified range
  237. */
  238. private int findRandomPort(int minPort, int maxPort) {
  239. int portRange = maxPort - minPort;
  240. return minPort + random.nextInt(portRange + 1);
  241. }
  242.  
  243. /**
  244. * Find an available port for this {@code SocketType}, randomly selected
  245. * from the range [{@code minPort}, {@code maxPort}].
  246. * @param minPort the minimum port number
  247. * @param maxPort the maximum port number
  248. * @return an available port number for this socket type
  249. * @throws IllegalStateException if no available port could be found
  250. */
  251. int findAvailablePort(int minPort, int maxPort) {
  252. Assert.isTrue(minPort > 0, "'minPort' must be greater than 0");
  253. Assert.isTrue(maxPort >= minPort, "'maxPort' must be greater than or equal to 'minPort'");
  254. Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX);
  255.  
  256. int portRange = maxPort - minPort;
  257. int candidatePort;
  258. int searchCounter = 0;
  259. do {
  260. if (searchCounter > portRange) {
  261. throw new IllegalStateException(String.format(
  262. "Could not find an available %s port in the range [%d, %d] after %d attempts",
  263. name(), minPort, maxPort, searchCounter));
  264. }
  265. candidatePort = findRandomPort(minPort, maxPort);
  266. searchCounter++;
  267. }
  268. while (!isPortAvailable(candidatePort));
  269.  
  270. return candidatePort;
  271. }
  272.  
  273. /**
  274. * Find the requested number of available ports for this {@code SocketType},
  275. * each randomly selected from the range [{@code minPort}, {@code maxPort}].
  276. * @param numRequested the number of available ports to find
  277. * @param minPort the minimum port number
  278. * @param maxPort the maximum port number
  279. * @return a sorted set of available port numbers for this socket type
  280. * @throws IllegalStateException if the requested number of available ports could not be found
  281. */
  282. SortedSet<Integer> findAvailablePorts(int numRequested, int minPort, int maxPort) {
  283. Assert.isTrue(minPort > 0, "'minPort' must be greater than 0");
  284. Assert.isTrue(maxPort > minPort, "'maxPort' must be greater than 'minPort'");
  285. Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX);
  286. Assert.isTrue(numRequested > 0, "'numRequested' must be greater than 0");
  287. Assert.isTrue((maxPort - minPort) >= numRequested,
  288. "'numRequested' must not be greater than 'maxPort' - 'minPort'");
  289.  
  290. SortedSet<Integer> availablePorts = new TreeSet<>();
  291. int attemptCount = 0;
  292. while ((++attemptCount <= numRequested + 100) && availablePorts.size() < numRequested) {
  293. availablePorts.add(findAvailablePort(minPort, maxPort));
  294. }
  295.  
  296. if (availablePorts.size() != numRequested) {
  297. throw new IllegalStateException(String.format(
  298. "Could not find %d available %s ports in the range [%d, %d]",
  299. numRequested, name(), minPort, maxPort));
  300. }
  301.  
  302. return availablePorts;
  303. }
  304. }
  305.  
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement