org.ops4j.net
Class FreePort

java.lang.Object
  extended by org.ops4j.net.FreePort

public class FreePort
extends Object

Find a not-taken port on localhost. First call to getPort will try to find a free port in range given by contructor. Each subsequent call will return the same value. Exception: If no port has been found (you'll get a RuntimeException on getPort) a subsequent call to getPort() will search again.

Since:
Jul 25, 2008
Author:
Toni Menzel (tonit)

Constructor Summary
FreePort(int from, int to)
           
 
Method Summary
 int getPort()
          Finds a free socket upon first calll and returns the same for every next call.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FreePort

public FreePort(int from,
                int to)
Parameters:
from - Begin of range to search for free port (including)
to - End of range to search for free port (including)
Method Detail

getPort

public int getPort()
Finds a free socket upon first calll and returns the same for every next call.

Returns:
a free port (from first call)
Throws:
RuntimeException - if no port has been found. (TODO change this to an apropriate checked exception)

toString

public String toString()
Overrides:
toString in class Object
Returns:
Human readably String representation


Copyright © 2006-2013 OPS4J - Open Participation Software for Java. All Rights Reserved.