Class PortManager

  • All Implemented Interfaces:
    Serializable

    public class PortManager
    extends Object
    implements Serializable
    Keeps track of used ports.
    NB: Using this class is no guarantee that the port isn't being used already, as not all code fragments might use this class!
    Version:
    $Revision$
    Author:
    FracPete (fracpete at waikato dot ac dot nz)
    See Also:
    Serialized Form
    • Constructor Detail

      • PortManager

        protected PortManager()
        Initializes the port manager.
    • Method Detail

      • initialize

        protected void initialize()
        Initializes the members.
      • size

        public int size()
        Returns the number of ports used.
        Returns:
        the number of ports
      • clear

        public void clear()
        Removes all ports.
      • next

        public Integer next​(Object obj)
        Returns the next available port.
        Parameters:
        obj - the object requesting the port
        Returns:
        the next available port
        Throws:
        IllegalArgumentException - if object is null
      • next

        public Integer next​(Class cls)
        Returns the next available port.
        Parameters:
        cls - the class requesting the port
        Returns:
        the next available port
        Throws:
        IllegalArgumentException - if class is null
      • bind

        public void bind​(Object obj,
                         int port)
        Sets a port as used.
        Parameters:
        obj - the object binding the port
        port - the port in use
        Throws:
        IllegalArgumentException - if object is null
      • bind

        public void bind​(Class cls,
                         int port)
        Sets a port as used.
        Parameters:
        cls - the class binding the port
        port - the port in use
        Throws:
        IllegalArgumentException - if object is null
      • next

        public Integer next​(Object obj,
                            int start)
        Returns the next available port, starting with the given starting port.
        Parameters:
        obj - the object requesting the port
        start - the first port to start with
        Returns:
        the next available port,
        Throws:
        IllegalArgumentException - if object is null
      • next

        public Integer next​(Class cls,
                            int start)
        Returns the next available port, starting with the given starting port.
        Parameters:
        cls - the class requesting the port
        start - the first port to start with
        Returns:
        the next available port,
        Throws:
        IllegalArgumentException - if class is null
      • isAvailable

        public boolean isAvailable​(int port)
        Checks whether the specified port is already in use.
        Parameters:
        port - the port to check
      • release

        public void release​(int port)
        Removes the specified port from the internal list of used ports.
        Parameters:
        port - the port to free
      • usedBy

        public Class usedBy​(int port)
        Returns the associated class for the port.
        Parameters:
        port - the port to get the class for
        Returns:
        the class, null if not available
      • uses

        public List<Integer> uses​(Class cls)
        Returns the ports associated with a class.
        Parameters:
        cls - the class to get the used ports for
        Returns:
        the ports
      • getSingleton

        public static PortManager getSingleton()
        Returns the singleton instance of the port manager.
        Returns:
        the port manager