Class NetUtils
java.lang.Object
de.iip_ecosphere.platform.support.NetUtils
Some network utilities.
- Author:
- Holger Eichelberger, SSE
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanconnectionOk(String url) Returns whether the connection tourlis ok.static booleanconnectionOk(URL url) Returns whether the connection tourlis ok.static URLCreates a new URL.private static StringFinds a fallback IP address.static intReturns a free ephemeral port.static StringgetNetMask(String netMask, String fallbackIp) Returns a netmask forgetOwnIP(String).static StringReturns the own hostname.static StringgetOwnIP()Returns the preferred own network address.static StringReturns the own IP address filtered by the given decimal netMask/regular expression.static booleanisAvailable(String host, int port) Returns whetherportonhostis available and some process is listening.static booleanisContainerIp(String address) Returns whetheraddressis a container address.static booleanReturns whether we are running inside a container.static booleanisIpV4Addess(String address) Returns whether the given string is an IPv4 address.static booleanisOwnAddress(String host) Returns whetherhostis one of the addresses of this computer.private static BooleanReturns whether this JVM is running inside docker.
-
Field Details
-
PROP_INCONTAINER
- See Also:
-
NO_MASK
- See Also:
-
-
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
Returns the own IP address filtered by the given decimal netMask/regular expression.- Parameters:
netMask- the net mask, regular expression; ifNO_MASK, empty or null, returngetOwnIP()- Returns:
- the IP
-
isOwnAddress
Returns whetherhostis one of the addresses of this computer.- Parameters:
host- the IP address/host name to look for- Returns:
trueifhostis one of the own addresses,falseelse
-
getOwnIP
Returns the preferred own network address.- Returns:
- the preferred own network address
-
findFallbackIP
Finds a fallback IP address. Must have an IPv4/IPv6 address (docker addresses so far have only- Returns:
- a fallback IP address, may be
127.0.0.1if none was found - See Also:
-
isContainerIp
Returns whetheraddressis a container address. Currently, we only consider ipv4 addresses.- Parameters:
address- the address- Returns:
truefor a container address,falseelse
-
isInContainer
public static boolean isInContainer()Returns whether we are running inside a container. Can be overridden by Boolean system property/envPROP_INCONTAINER.- Returns:
truefor container,falseelse- See Also:
-
isRunningInsideDocker
Returns whether this JVM is running inside docker.- Returns:
truefor docker environment,falseelse
-
getOwnHostname
Returns the own hostname.- Returns:
- the hostname, potentially with a fallback to "localhost"
-
isAvailable
Returns whetherportonhostis available and some process is listening.- Parameters:
host- the host nameport- the port number- Returns:
truefor available,falseelse
-
isIpV4Addess
Returns whether the given string is an IPv4 address.- Parameters:
address- the address to validate- Returns:
truefor a valid address,falseelse
-
getNetMask
Returns a netmask forgetOwnIP(String).- Parameters:
netMask- the netmask to usefallbackIp- 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
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
Returns whether the connection tourlis ok.- Parameters:
url- the URL- Returns:
truefor ok,falseelse
-
connectionOk
Returns whether the connection tourlis ok.- Parameters:
url- the URL- Returns:
truefor ok,falseelse
-