001package ca.uhn.hl7v2.hoh.util;
002
003import java.io.IOException;
004import java.net.ServerSocket;
005import java.util.ArrayList;
006import java.util.List;
007
008/**
009 * Provides server ports
010 */
011public class RandomServerPortProvider {
012
013        private static List<Integer> ourPorts = new ArrayList<Integer>(); 
014        
015        public static int findFreePort() {
016                ServerSocket server;
017                try {
018                        server = new ServerSocket(0);
019                        int port = server.getLocalPort();
020                        ourPorts.add(port);
021                        server.close();
022                        Thread.sleep(500);
023                        return port;
024                } catch (IOException e) {
025                        throw new Error(e);
026                } catch (InterruptedException e) {
027                        throw new Error(e);
028                }
029        }
030
031        public static List<Integer> list() {
032                return ourPorts;
033        }
034
035}
036