Package org.refcodes.runtime
Class SystemUtility
- java.lang.Object
-
- org.refcodes.runtime.SystemUtility
-
public final class SystemUtility extends java.lang.ObjectUtility for acquiring system information on the machine this process is running in.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.Stringexec(int aTimeOutMillis, java.lang.String aCommandLine)Executes a command and returns the output.static java.lang.Stringexec(int aTimeOutMillis, java.lang.String... aCommandLine)Executes a command and returns the output.static java.lang.Stringexec(java.lang.String aCommandLine)Executes a command and returns the output.static java.lang.Stringexec(java.lang.String... aCommandLine)Executes a command and returns the output.static java.lang.StringgetComputerName()Determines the computer's name.static java.lang.StringgetUname()If on a *nix alike system, this method returns the output of the "uname -a" command: "uname" prints system information, "-a" instructs it to print all information.static byte[]toHostIpAddress()Tries to determine a no-localhost IP-Address for this machine.static byte[]toHostMacAddress()Tries to determine the host Mac-Address for this machine.static java.lang.StringtoPrettySystemInfo()Gathers all available system information from this Artifac's point of view.static java.lang.StringtoPropertyValue(java.lang.String aDefaultValue, org.refcodes.data.SystemProperty aSystemProperty, org.refcodes.data.EnvironmentVariable... aEnvironmentProperties)Gets the value for the provided properties, if non was found then the default value is taken.static java.lang.StringtoPropertyValue(org.refcodes.data.SystemProperty aSystemProperty, org.refcodes.data.EnvironmentVariable... aEnvironmentProperties)Gets the value for the provided properties, if non was found then null is returned.static java.util.Map<java.lang.String,java.lang.String>toSystemInfo()Gathers all available system information from this Artifac's point of view.
-
-
-
Method Detail
-
getComputerName
public static java.lang.String getComputerName()
Determines the computer's name. First it tries to get it from theInetAddress, if it fails it tries to get it from the system's environment using theEnvironmentVariable.COMPUTERNAME(on Windows machines only) and if both fails, it returns the defaultLiteral.LOCALHOSTidentifier.- Returns:
- The computer's name, as fallback,
Literal.LOCALHOST("localhost") is returned.
-
getUname
public static java.lang.String getUname()
If on a *nix alike system, this method returns the output of the "uname -a" command: "uname" prints system information, "-a" instructs it to print all information.- Returns:
- The "uname -a" output or null if "uname" is not known.
-
toPropertyValue
public static java.lang.String toPropertyValue(java.lang.String aDefaultValue, org.refcodes.data.SystemProperty aSystemProperty, org.refcodes.data.EnvironmentVariable... aEnvironmentProperties)Gets the value for the provided properties, if non was found then the default value is taken. ASystemPropertyelements wins over theEnvironmentVariableelements. The precedingEnvironmentVariableelement wins over the succeedingEnvironmentVariableelement. The default value is taken if non property had a value (a String with length > 0).- Parameters:
aDefaultValue- The default value to take when none other value was set.aSystemProperty- The system-property passed viajava -D<name>=<value>aEnvironmentProperties- The properties looked for in the system's environment variables.- Returns:
- The best fitting value.
-
toPropertyValue
public static java.lang.String toPropertyValue(org.refcodes.data.SystemProperty aSystemProperty, org.refcodes.data.EnvironmentVariable... aEnvironmentProperties)Gets the value for the provided properties, if non was found then null is returned. ASystemPropertyelements wins over theEnvironmentVariableelements. The precedingEnvironmentVariableelement wins over the succeedingEnvironmentVariableelement. A null is taken if non property had a value (a String with length > 0).- Parameters:
aSystemProperty- The system-property passed viajava -D<name>=<value>aEnvironmentProperties- The properties looked for in the system's environment variables.- Returns:
- The best fitting value or null if none was detected.
-
exec
public static java.lang.String exec(int aTimeOutMillis, java.lang.String aCommandLine) throws java.io.IOException, java.lang.InterruptedExceptionExecutes a command and returns the output.- Parameters:
aTimeOutMillis- The time in milliseconds to wait till the process is killed when not terminated yet.aCommandLine- the command- Returns:
- Null if execution failed, else the according output. An empty
Stringstands fur successful execution. - Throws:
java.io.IOException- in case there were problems executing the command.java.lang.InterruptedException- thrown in case execution as been interrupted.
-
exec
public static java.lang.String exec(int aTimeOutMillis, java.lang.String... aCommandLine) throws java.io.IOException, java.lang.InterruptedExceptionExecutes a command and returns the output.- Parameters:
aTimeOutMillis- The time in milliseconds to wait till the process is killed when not terminated yet.aCommandLine- the command with the arguments to be passed to the command.- Returns:
- Null if execution failed, else the according output. An empty
Stringstands fur successful execution. - Throws:
java.io.IOException- in case there were problems executing the command.java.lang.InterruptedException- thrown in case execution as been interrupted.
-
exec
public static java.lang.String exec(java.lang.String aCommandLine) throws java.io.IOExceptionExecutes a command and returns the output.- Parameters:
aCommandLine- the command- Returns:
- Null if execution failed, else the according output. An empty
Stringstands fur successful execution. - Throws:
java.io.IOException- in case there were problems executing the command.
-
exec
public static java.lang.String exec(java.lang.String... aCommandLine) throws java.io.IOExceptionExecutes a command and returns the output.- Parameters:
aCommandLine- the command with the arguments to be passed to the command.- Returns:
- Null if execution failed, else the according output. An empty
Stringstands fur successful execution. - Throws:
java.io.IOException- in case there were problems executing the command.
-
toSystemInfo
public static java.util.Map<java.lang.String,java.lang.String> toSystemInfo()
Gathers all available system information from this Artifac's point of view.- Returns:
- A
Mapcontaining the available information being gathered.
-
toPrettySystemInfo
public static java.lang.String toPrettySystemInfo()
Gathers all available system information from this Artifac's point of view. This method may rely on the output oftoSystemInfo().- Returns:
- A
Stringcontaining the available information being gathered.
-
toHostIpAddress
public static byte[] toHostIpAddress() throws java.net.SocketException, java.net.UnknownHostExceptionTries to determine a no-localhost IP-Address for this machine. The best guess is returned. If none no-localhost address is found, then the localhost's IP-Address may be returned (as ofInetAddress.getLocalHost()).- Returns:
- The best guest for a no-localhost IP-Address or as a fall back
the localhost's IP-Address (as of
InetAddress.getLocalHost()may be returned. - Throws:
java.net.SocketException- Thrown to indicate that accessing the network interfaces caused a problem.java.net.UnknownHostException- Thrown to indicate that the IP address of the local host could not be determined.
-
toHostMacAddress
public static byte[] toHostMacAddress() throws java.net.SocketException, java.net.UnknownHostExceptionTries to determine the host Mac-Address for this machine. The best guess is returned.- Returns:
- The best guest for a Mac-Address is returned.
- Throws:
java.net.SocketException- Thrown to indicate that accessing the network interfaces caused a problem.java.net.UnknownHostException- Thrown to indicate that the IP address of the local host could not be determined.
-
-