Class SystemUtility

java.lang.Object
org.refcodes.runtime.SystemUtility

public final class SystemUtility extends Object
Utility for acquiring system information on the machine this process is running in.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    exec(int aTimeoutMillis, String aCommandLine)
    Executes a command and returns the output.
    static String
    exec(int aTimeoutMillis, String... aCommandLine)
    Executes a command and returns the output.
    static String
    exec(String aCommandLine)
    Executes a command and returns the output.
    static String
    exec(String... aCommandLine)
    Executes a command and returns the output.
    static String
    Determines the computer's name.
    static String
    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[]
    Tries to determine a no-localhost IP-Address for this machine.
    static byte[]
    Tries to determine the host Mac-Address for this machine.
    static String
    Gathers all available system information from this Artifac's point of view.
    static String
    toPropertyValue(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 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.
    static Map<String,String>
    Gathers all available system information from this Artifac's point of view.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getComputerName

      public static String getComputerName()
      Determines the computer's name. First it tries to get it from the InetAddress, if it fails it tries to get it from the system's environment using the EnvironmentVariable.COMPUTERNAME (on Windows machines only) and if both fails, it returns the default Literal.LOCALHOST identifier.
      Returns:
      The computer's name, as fallback, Literal.LOCALHOST ("localhost") is returned.
    • getUname

      public static 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 String toPropertyValue(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. A SystemProperty elements wins over the EnvironmentVariable elements. The preceding EnvironmentVariable element wins over the succeeding EnvironmentVariable element. 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 via java -D<name>=<value>
      aEnvironmentProperties - The properties looked for in the system's environment variables.
      Returns:
      The best fitting value.
    • toPropertyValue

      public static 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. A SystemProperty elements wins over the EnvironmentVariable elements. The preceding EnvironmentVariable element wins over the succeeding EnvironmentVariable element. A null is taken if non property had a value (a String with length > 0).
      Parameters:
      aSystemProperty - The system-property passed via java -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 String exec(int aTimeoutMillis, String aCommandLine) throws IOException, InterruptedException
      Executes 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 String stands fur successful execution.
      Throws:
      IOException - in case there were problems executing the command.
      InterruptedException - thrown in case execution as been interrupted.
    • exec

      public static String exec(int aTimeoutMillis, String... aCommandLine) throws IOException, InterruptedException
      Executes 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 String stands fur successful execution.
      Throws:
      IOException - in case there were problems executing the command.
      InterruptedException - thrown in case execution as been interrupted.
    • exec

      public static String exec(String aCommandLine) throws IOException
      Executes a command and returns the output.
      Parameters:
      aCommandLine - the command
      Returns:
      Null if execution failed, else the according output. An empty String stands fur successful execution.
      Throws:
      IOException - in case there were problems executing the command.
    • exec

      public static String exec(String... aCommandLine) throws IOException
      Executes 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 String stands fur successful execution.
      Throws:
      IOException - in case there were problems executing the command.
    • toSystemInfo

      public static Map<String,String> toSystemInfo()
      Gathers all available system information from this Artifac's point of view.
      Returns:
      A Map containing the available information being gathered.
    • toPrettySystemInfo

      public static String toPrettySystemInfo()
      Gathers all available system information from this Artifac's point of view. This method may rely on the output of toSystemInfo().
      Returns:
      A String containing the available information being gathered.
    • toHostIpAddress

      public static byte[] toHostIpAddress() throws SocketException, UnknownHostException
      Tries 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 of InetAddress.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:
      SocketException - Thrown to indicate that accessing the network interfaces caused a problem.
      UnknownHostException - Thrown to indicate that the IP address of the local host could not be determined.
    • toHostMacAddress

      public static byte[] toHostMacAddress() throws SocketException, UnknownHostException
      Tries 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:
      SocketException - Thrown to indicate that accessing the network interfaces caused a problem.
      UnknownHostException - Thrown to indicate that the IP address of the local host could not be determined.