Class Host

java.lang.Object
org.refcodes.runtime.Host

public final class Host extends Object
The Host type abstracts the computer system on which this application runs on.
  • 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 Long
    Bad hack to get the JVM's (process TID) PID of the process running your JVM instance.
    static String
    Determines the temporary directory for scratch space.
    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 boolean
    kill(Long aPid)
    Bad hack to kill an OS thread by PID.
    static Process
    killProcess(long aPid)
    Bad hack to kill an OS thread by PID.
    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 Map<String,String>
    Gathers all available system information from this Artifac's point of view.
    static File
    Tries to determine the current directory from which the Java programm was started for example as of bash's pwd command.

    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.
    • 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.
    • getTempDir

      public static String getTempDir()
      Determines the temporary directory for scratch space.
      Returns:
      The according temporary directory.
    • 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.
    • getPid

      public static Long getPid()
      Bad hack to get the JVM's (process TID) PID of the process running your JVM instance.
      Returns:
      The PID (process TID) of the JVM running your thread.
      See Also:
      • "http://stackoverflow.com/questions/35842/how-can-a-java-program-get-its-own-process-id"
    • killProcess

      public static Process killProcess(long aPid) throws IOException
      Bad hack to kill an OS thread by PID. The current threads does not wait till the operation finished.
      Parameters:
      aPid - The process TID (PID) of the process to kill.
      Returns:
      The Process object representing the kill operation. This instance will let you wait till the operation finished Process.waitFor() and provides access to the Process.exitValue()
      Throws:
      IOException - Thrown in case of failing to successfully execute the kill operation.
      See Also:
      • "http://stackoverflow.com/questions/9573696/kill-a-process-based-on-pid-in-java"
      • "http://stackoverflow.com/questions/2950338/how-can-i-kill-a-linux-process-in-java-with-sigkill-process-destroy-does-sigte"
    • kill

      public static boolean kill(Long aPid) throws IOException, InterruptedException
      Bad hack to kill an OS thread by PID. The current threads does wait till the operation finished.
      Parameters:
      aPid - The process TID (PID) of the process to kill.
      Returns:
      True in case killing the process was successful (e.g. the kill operation returned an exit code 0), else false.
      Throws:
      IOException - Thrown in case of failing to successfully execute the kill operation.
      InterruptedException - the interrupted exception
      See Also:
      • "http://stackoverflow.com/questions/9573696/kill-a-process-based-on-pid-in-java"
      • "http://stackoverflow.com/questions/2950338/how-can-i-kill-a-linux-process-in-java-with-sigkill-process-destroy-does-sigte"
    • toUserWorkingDir

      public static File toUserWorkingDir()
      Tries to determine the current directory from which the Java programm was started for example as of bash's pwd command. Pass as JVM argument via "-Dcurrent.dir=/path/to/current/dir".
      Returns:
      The current path of the user invoking the Java programm or null if the current path cannot be determined.