java.lang.Object
org.refcodes.web.PortManagerImpl
- All Implemented Interfaces:
PortManager
- Direct Known Subclasses:
PortManagerSingleton
public class PortManagerImpl extends Object implements PortManager
Implementation of the
PortManager interface. For system wide
management of ports, see PortManagerSingleton.-
Constructor Summary
Constructors Constructor Description PortManagerImpl() -
Method Summary
Modifier and Type Method Description IntegerbindAnyPort()Any free port in the range as defined byPortRange.DYNAMIC_PORTSis bound (reserved) and returned.IntegerbindNextPort(int aBeginPortIndex, int aEndPortIndex)The first free port in the range of the given begin Port-Number index and the given end Port-Number index is bound (reserved) and returned.IntegerbindPort(int aPortNumber)The port is bound and returned.booleanisPortAvaialble(int aPortNumber)Tests whether there is already some Server-Socket technically bound to the given port.booleanisPortBound(int aPortNumber)Determines whether the given Port-Number is already in use.booleanunbindPort(Integer aPortNumber)Unbinds a port for the given Port-Number using theIntegeras handle for unbinding.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.refcodes.web.PortManager
bindNextPort, bindNextPort
-
Constructor Details
-
PortManagerImpl
public PortManagerImpl()
-
-
Method Details
-
isPortBound
public boolean isPortBound(int aPortNumber)Determines whether the given Port-Number is already in use. This is achieved by testing whether another resource has reserved the port manually (viaPortManager.bindPort(int),PortManager.bindAnyPort()orPortManager.bindNextPort(int)) or if the port is already in use and cannot be bound, e.g. there is already some Server-Socket technically bound to the given port.- Specified by:
isPortBoundin interfacePortManager- Parameters:
aPortNumber- The port number to be tested if it is already bound.- Returns:
- True in case the port is being bound already, else false if it still available.
-
bindAnyPort
Any free port in the range as defined byPortRange.DYNAMIC_PORTSis bound (reserved) and returned. A port is aught to be free whenPortManager.isPortBound(int)returns false.- Specified by:
bindAnyPortin interfacePortManager- Returns:
- The free port being reserved. The
Integerinstance is the handle for unbinding a port viaPortManager.unbindPort(Integer). - Throws:
PortNotFoundRuntimeException- thrown in case no port can be determined.
-
bindPort
The port is bound and returned. A port is aught to be free whenPortManager.isPortBound(int)returns false.- Specified by:
bindPortin interfacePortManager- Parameters:
aPortNumber- The Port-Number which to bind (reserve).- Returns:
- The port being reserved. The
Integerinstance is the handle for unbinding a port viaPortManager.unbindPort(Integer). - Throws:
PortAlreadyBoundException- the port already bound exception
-
unbindPort
Unbinds a port for the given Port-Number using theIntegeras handle for unbinding. This means that anotherIntegerinstance with another identity fails to unbind the given port.- Specified by:
unbindPortin interfacePortManager- Parameters:
aPortNumber- The Port-Number for the port to be unbound.- Returns:
- True in case the port has been unbound or false if the port was not bound at all.
- Throws:
IllegalArgumentException- thrown in case the ginvenIntegeridentity is not allowed to unbind the given port.
-
bindNextPort
public Integer bindNextPort(int aBeginPortIndex, int aEndPortIndex) throws PortNotFoundRuntimeExceptionThe first free port in the range of the given begin Port-Number index and the given end Port-Number index is bound (reserved) and returned. A port is aught to be free whenPortManager.isPortBound(int)returns false.- Specified by:
bindNextPortin interfacePortManager- Parameters:
aBeginPortIndex- The begin index (included) of the Port-Number which to bind (reserve).aEndPortIndex- The end index (included) of the Port-Number which to bind (reserve).- Returns:
- The next free port being reserved. The
Integerinstance is the handle for unbinding a port viaPortManager.unbindPort(Integer). - Throws:
PortNotFoundRuntimeException- thrown in case no port can be determined.
-
isPortAvaialble
public boolean isPortAvaialble(int aPortNumber)Tests whether there is already some Server-Socket technically bound to the given port. This method does not test if the port is reserved, it just tests if a Server-Socket could still technically bind to this port.- Specified by:
isPortAvaialblein interfacePortManager- Parameters:
aPortNumber- The Port-Number to be technically tested if a Server-Socket could still bind to it.- Returns:
- True if the port could technically be bound to, else false if some Server-Socket is already bound to this port (or the Security-Manager prevents binding to the port).
-