1 package ca.uhn.hl7v2.hoh.util;
2
3 import java.io.IOException;
4 import java.net.ServerSocket;
5 import java.util.ArrayList;
6 import java.util.List;
7
8
9
10
11 public class RandomServerPortProvider {
12
13 private static List<Integer> ourPorts = new ArrayList<Integer>();
14
15 public static int findFreePort() {
16 ServerSocket server;
17 try {
18 server = new ServerSocket(0);
19 int port = server.getLocalPort();
20 ourPorts.add(port);
21 server.close();
22 Thread.sleep(500);
23 return port;
24 } catch (IOException e) {
25 throw new Error(e);
26 } catch (InterruptedException e) {
27 throw new Error(e);
28 }
29 }
30
31 public static List<Integer> list() {
32 return ourPorts;
33 }
34
35 }
36