Enum NetStat.Platform

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<NetStat.Platform>
    Enclosing class:
    NetStat

    private static enum NetStat.Platform
    extends java.lang.Enum<NetStat.Platform>
    Specifies the platform-dependent support for obtaining netstat-like information.
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
      LINUX
      Handles processing for Linux variants.
      MAC
      Handles processing for Mac OS X.
      WINDOWS
      Handles processing for Windows variants.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String osPlatform  
      private static java.lang.String[] PS_COMMAND  
      private static java.util.regex.Pattern PS_PATTERN  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Platform​(java.lang.String osPlatform)  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      static NetStat.Platform getPlatform()
      Gets the Platform constant for the current operating system.
      private static java.util.List<NetStat.BusyPort> mergeCommands​(java.util.List<NetStat.BusyPort> busyPorts)
      Creates an updated BusyPort list merging in a more complete command string from the ps command.
      abstract java.util.List<NetStat.BusyPort> netstat()
      Gets the list of TCP connections joined with information about the process that owns each connection.
      abstract java.util.List<NetStat.BusyPort> netstat​(int port)
      Gets the list of TCP connections for a single local port joined with information about the process that owns each connection.
      private static java.lang.String[] prepareCommand​(java.lang.String[] commandTemplate, java.lang.Object... args)
      Prepares a command array using String.format to substitute arguments.
      private static <T> java.util.List<T> runCommand​(java.lang.String[] command, java.util.function.Function<java.util.stream.Stream<java.lang.String>,​java.util.List<T>> conversion)  
      static NetStat.Platform valueOf​(java.lang.String name)
      Returns the enum constant of this type with the specified name.
      static NetStat.Platform[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Enum Constant Detail

      • WINDOWS

        public static final NetStat.Platform WINDOWS
        Handles processing for Windows variants. For successful operation, powershell must be available via PATH.
      • MAC

        public static final NetStat.Platform MAC
        Handles processing for Mac OS X. This uses the nettop command in single-sample mode and augments the output with a ps command. Unlike lsof, nettop does not require elevated privileges to observe the open ports of processes opened by "other" users.
      • LINUX

        public static final NetStat.Platform LINUX
        Handles processing for Linux variants. This uses the lsof for discovering open ports and augments the lsof output with output from ps. lsof requires elevated privileges to see all ports and executed under the control of sudo with the password prompt suppressed.

        On Linux implementations which have sudo and lsof but do not have full representation of the network stack in the /proc filesystem, the netstat() method will return an empty list.

    • Field Detail

      • osPlatform

        private final java.lang.String osPlatform
      • PS_COMMAND

        private static final java.lang.String[] PS_COMMAND
      • PS_PATTERN

        private static final java.util.regex.Pattern PS_PATTERN
    • Constructor Detail

      • Platform

        private Platform​(java.lang.String osPlatform)
    • Method Detail

      • values

        public static NetStat.Platform[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (NetStat.Platform c : NetStat.Platform.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static NetStat.Platform valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • netstat

        public abstract java.util.List<NetStat.BusyPort> netstat()
                                                          throws NetStat.HostExecutionException
        Gets the list of TCP connections joined with information about the process that owns each connection. Administrator privileges may be required for complete output.
        Returns:
        a list of BusyPort instances describing the active ports; this list may include both IPv4 and IPv6 connections
        Throws:
        NetStat.HostExecutionException - if there was a failure in obtaining the TCP connections or host processes lists
      • netstat

        public abstract java.util.List<NetStat.BusyPort> netstat​(int port)
                                                          throws NetStat.HostExecutionException
        Gets the list of TCP connections for a single local port joined with information about the process that owns each connection. Administrator privileges may be required for complete output.
        Parameters:
        port - the target port
        Returns:
        a list of BusyPort instances describing the active connections for port; this list may include both IPv4 and IPv6 connections
        Throws:
        NetStat.HostExecutionException - if there was a failure in obtaining the TCP connections or host processes lists
      • getPlatform

        public static NetStat.Platform getPlatform()
                                            throws java.lang.EnumConstantNotPresentException
        Gets the Platform constant for the current operating system.
        Returns:
        the current Platform constant
        Throws:
        java.lang.EnumConstantNotPresentException - if the current operating system is not supported
      • mergeCommands

        private static java.util.List<NetStat.BusyPort> mergeCommands​(java.util.List<NetStat.BusyPort> busyPorts)
                                                               throws NetStat.HostExecutionException
        Creates an updated BusyPort list merging in a more complete command string from the ps command.
        Parameters:
        busyPorts - a list of BusyPort instances to update
        Returns:
        a new list of updated BusyPort instances
        Throws:
        NetStat.HostExecutionException - if an error is raised trying to run the ps command
      • prepareCommand

        private static java.lang.String[] prepareCommand​(java.lang.String[] commandTemplate,
                                                         java.lang.Object... args)
        Prepares a command array using String.format to substitute arguments. If the argument list contains more than one argument, commandTemplate should use format specifiers using the argument index form.
        Parameters:
        commandTemplate - the array composing the command to prepare
        args - the arguments to substitute into the command array strings
        Returns:
        the prepared command array