Class ProcessUtils

java.lang.Object
com.sun.enterprise.universal.process.ProcessUtils

public final class ProcessUtils extends Object
Includes a somewhat kludgy way to get the pid for "me". Another casualty of the JDK catering to the LEAST common denominator. Some obscure OS might not have a pid! The name returned from the JMX method is like so: 12345
Author:
bnevins, David Matejcek
  • Method Details

    • waitWhileIsAlive

      public static boolean waitWhileIsAlive(long pid, Duration timeout, boolean printDots)
      Blocks until the pid's handle exits or timeout comes first.
      Parameters:
      pid - process identifier
      timeout -
      printDots - true to print dots to STDOUT while waiting. One dot per second.
      Returns:
      true if the handle was not found or exited before timeout. False otherwise.
    • isAlive

      public static boolean isAlive(File pidFile)
      Parameters:
      pidFile -
      Returns:
      true if the pid file exists and the process with the pid inside is alive.
    • isAlive

      public static boolean isAlive(long pid)
      Parameters:
      pid -
      Returns:
      true if the process with is alive.
    • isAlive

      public static boolean isAlive(ProcessHandle process)
      The Process.isAlive() returns true even for zombies so we implemented this method which considers zombies as dead.
      Parameters:
      process -
      Returns:
      true if the process with is alive.
    • waitWhileListening

      public static boolean waitWhileListening(HostAndPort endpoint, Duration timeout, boolean printDots)
      Blocks until the endpoint closes the connection or timeout comes first.
      Parameters:
      endpoint - endpoint host and port to use.
      timeout -
      printDots - true to print dots to STDOUT while waiting. One dot per second.
      Returns:
      true if the connection was closed before timeout. False otherwise.
    • isListening

      public static boolean isListening(HostAndPort endpoint)
      Parameters:
      endpoint - endpoint host and port to use.
      Returns:
      true if the endpoint is listening on socket
    • kill

      public static void kill(File pidFile, Duration timeout, boolean printDots) throws KillNotPossibleException, KillTimeoutException
      Kill the process with the given Process ID and wait until it's gone - that means that the watchedPidFile is deleted OR the process is not resolved as alive by the ProcessHandle.isAlive() OR we cannot retrieve the command line of the process via ProcessHandle.Info.commandLine().
      Parameters:
      pidFile - - used to load pid
      timeout - - timeout to wait until to meet conditions meaning that the process stopped
      printDots - - print one dot per second when waiting.
      Throws:
      KillNotPossibleException - It wasn't possible to send the kill signal to the process.
      KillTimeoutException - Signal was sent, but process is still alive after the timeout.
    • waitFor

      public static boolean waitFor(Supplier<Boolean> sign, Duration timeout, boolean printDots)
      Parameters:
      sign - logic defining what we are waiting for.
      timeout -
      printDots - print dot each second and new line in the end.
      Returns:
      true if the sign returned true before timeout.
    • getExe

      public static File getExe(String name)
      Look for name in the Path. If it is found and if it is executable then return a File object pointing to it. Otherwise return nu
      Parameters:
      name - the name of the file with no path
      Returns:
      the File object or null
    • saveCurrentPid

      public static void saveCurrentPid(File pidFile) throws IOException
      Saves current pid file to the file.
      Parameters:
      pidFile -
      Throws:
      IOException
    • loadPid

      public static Long loadPid(File pidFile) throws IllegalArgumentException
      Parameters:
      pidFile - file containing pid.
      Returns:
      pid from the file or null if the file does not exist or is null
      Throws:
      IllegalArgumentException - for unparseable file