Class NetUtils

java.lang.Object
de.iip_ecosphere.platform.support.NetUtils

public class NetUtils extends Object
Some network utilities.
Author:
Holger Eichelberger, SSE
  • Field Details

  • Constructor Details

    • NetUtils

      private NetUtils()
      Preventing external creation.
  • Method Details

    • getEphemeralPort

      public static int getEphemeralPort()
      Returns a free ephemeral port. Such a port may be used for testing, e.g. to avoid clashes between multiple subsequent tests using the same static port.
      Returns:
      the port number
    • getOwnIP

      public static String getOwnIP(String netMask)
      Returns the own IP address filtered by the given decimal netMask/regular expression.
      Parameters:
      netMask - the net mask, regular expression; if NO_MASK, empty or null, return getOwnIP()
      Returns:
      the IP
    • isOwnAddress

      public static boolean isOwnAddress(String host)
      Returns whether host is one of the addresses of this computer.
      Parameters:
      host - the IP address/host name to look for
      Returns:
      true if host is one of the own addresses, false else
    • getOwnIP

      public static String getOwnIP()
      Returns the preferred own network address.
      Returns:
      the preferred own network address
    • findFallbackIP

      public static String findFallbackIP()
      Finds a fallback IP address. Must have an IPv4/IPv6 address. [public for testing]
      Returns:
      a fallback IP address, may be 127.0.0.1 if none was found
      See Also:
    • isContainerIp

      public static boolean isContainerIp(String address)
      Returns whether address is a container address. Currently, we only consider ipv4 addresses.
      Parameters:
      address - the address
      Returns:
      true for a container address, false else
    • isInContainer

      public static boolean isInContainer()
      Returns whether we are running inside a container. Can be overridden by Boolean system property/env PROP_INCONTAINER.
      Returns:
      true for container, false else
      See Also:
    • isRunningInsideDocker

      private static Boolean isRunningInsideDocker()
      Returns whether this JVM is running inside docker.
      Returns:
      true for docker environment, false else
    • getOwnHostname

      public static String getOwnHostname()
      Returns the own hostname.
      Returns:
      the hostname, potentially with a fallback to "localhost"
    • isAvailable

      public static boolean isAvailable(String host, int port)
      Returns whether port on host is available and some process is listening.
      Parameters:
      host - the host name
      port - the port number
      Returns:
      true for available, false else
    • isIpV4Addess

      public static boolean isIpV4Addess(String address)
      Returns whether the given string is an IPv4 address.
      Parameters:
      address - the address to validate
      Returns:
      true for a valid address, false else
    • getNetMask

      public static String getNetMask(String netMask, String fallbackIp)
      Returns a netmask for getOwnIP(String).
      Parameters:
      netMask - the netmask to use
      fallbackIp - the fallback IP address to be turned into a class C net (search) mask
      Returns:
      the netmask, the modified fallback address or an empty string
    • createURL

      public static URL createURL(String url) throws IllegalArgumentException
      Creates a new URL. [transition to JDK 21]
      Parameters:
      url - the URL as text
      Returns:
      the URL
      Throws:
      IllegalArgumentException - if the syntax is illegal
    • connectionOk

      public static boolean connectionOk(String url)
      Returns whether the connection to url is ok.
      Parameters:
      url - the URL
      Returns:
      true for ok, false else
    • connectionOk

      public static boolean connectionOk(URL url)
      Returns whether the connection to url is ok.
      Parameters:
      url - the URL
      Returns:
      true for ok, false else