Class PidFile

java.lang.Object
de.iip_ecosphere.platform.support.PidFile

public final class PidFile extends Object
Process ID (PID) file abstraction that writes the current PID into a file and optionally removes it on system exit. This class is taken from elastic search (adapted from http://home.apache.org/~rmuir/es-coverage/combined/org.elasticsearch.common/PidFile.java.html) as a dependency to elastic search (in particular on this level) is currently not intended.
Author:
elasicsearch
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final boolean
     
    private final Path
     
    private final long
     
    static final String
    Denotes the directory where PID files shall be created within.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    PidFile(Path path, boolean deleteOnExit, long pid)
    Creates an instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static void
    Adds a shutdown hook in order to get rid of the PID file when the JVM terminates.
    static PidFile
    create(Path path, boolean deleteOnExit)
    Creates a new PidFile and writes the current process ID into the provided path.
    (package private) static PidFile
    create(Path path, boolean deleteOnExit, long pid)
    Creates a new PidFile with given process identifier and writes the identifier into the provided path.
    static PidFile
    createInDefaultDir(String name, boolean deleteOnExit)
    Creates a new PidFile in the system's temporary directory as a file with given name and writes the current process ID into that file.
    static PidFile
    createInDefaultDir(String name, boolean deleteOnExit, boolean deleteIfExists)
    Creates a new PidFile in the system's temporary directory as a file with given name and writes the current process ID into that file.
    static long
    Returns the process identifier of the containing JVM.
    Returns the PID file path.
    long
    Returns the PID of the current JVM.
    static String
    Returns the path of the PID directory via PID_DIR_PROPERTY_NAME or temp directory.
    boolean
    Returns true iff the process id file is deleted on system exit.

    Methods inherited from class java.lang.Object

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

    • PID_DIR_PROPERTY_NAME

      public static final String PID_DIR_PROPERTY_NAME
      Denotes the directory where PID files shall be created within. If not specified, the temporary directory is used.
      See Also:
    • pid

      private final long pid
    • path

      private final Path path
    • deleteOnExit

      private final boolean deleteOnExit
  • Constructor Details

    • PidFile

      private PidFile(Path path, boolean deleteOnExit, long pid) throws IOException
      Creates an instance.
      Parameters:
      path - the path where to create the file within
      deleteOnExit - delete the file on exit of the JVM
      pid - the PID to write into the file
      Throws:
      IOException - if an I/O problem occurs while creating the PID file
      IllegalArgumentException - if the parent of path is not a directory or the denoted file is not a regular file
  • Method Details

    • create

      public static PidFile create(Path path, boolean deleteOnExit) throws IOException
      Creates a new PidFile and writes the current process ID into the provided path.
      Parameters:
      path - the path to the PID file. The file is newly created or truncated if it already exists.
      deleteOnExit - if true the PID file is deleted with best effort on system exit
      Returns:
      the PidFile instance
      Throws:
      IOException - if an I/O problem occurs
      IllegalArgumentException - if the parent of path is not a directory or the denoted file is not a regular file
    • createInDefaultDir

      public static PidFile createInDefaultDir(String name, boolean deleteOnExit) throws IOException
      Creates a new PidFile in the system's temporary directory as a file with given name and writes the current process ID into that file.
      Parameters:
      name - the name of the file within the system'S temporary directory. An existing file is deleted.
      deleteOnExit - if true the PID file is deleted with best effort on system exit
      Returns:
      the PidFile instance
      Throws:
      IOException - if an I/O problem occurs
      IllegalArgumentException - if the parent of path is not a directory or the denoted file is not a regular file
    • getPidDirectory

      public static String getPidDirectory()
      Returns the path of the PID directory via PID_DIR_PROPERTY_NAME or temp directory.
      Returns:
      the path
    • createInDefaultDir

      public static PidFile createInDefaultDir(String name, boolean deleteOnExit, boolean deleteIfExists) throws IOException
      Creates a new PidFile in the system's temporary directory as a file with given name and writes the current process ID into that file.
      Parameters:
      name - the name of the file within the system'S temporary directory.
      deleteOnExit - if true the PID file is deleted with best effort on system exit
      deleteIfExists - if true, an existing file is deleted; if false the file is newly created or truncated if it already exists.
      Returns:
      the PidFile instance
      Throws:
      IOException - if an I/O problem occurs
      IllegalArgumentException - if the parent of path is not a directory or the denoted file is not a regular file
    • create

      static PidFile create(Path path, boolean deleteOnExit, long pid) throws IOException
      Creates a new PidFile with given process identifier and writes the identifier into the provided path.
      Parameters:
      path - the path to the PID file. The file is newly created or truncated if it already exists.
      deleteOnExit - if true the pid file is deleted with best effort on system exit
      pid - the PID to write into the file
      Returns:
      the PidFile instance
      Throws:
      IOException - if an I/O problem occurs
      IllegalArgumentException - if the parent of path is not a directory or the denoted file is not a regular file
    • getPid

      public long getPid()
      Returns the PID of the current JVM.
      Returns:
      the PID, may be -1 if the PID cannot be determined
    • getPath

      public Path getPath()
      Returns the PID file path.
      Returns:
      PID file path
    • isDeleteOnExit

      public boolean isDeleteOnExit()
      Returns true iff the process id file is deleted on system exit.
      Returns:
      true of deletion of system exit is enabled, otherwise false.
    • addShutdownHook

      private static void addShutdownHook(Path path)
      Adds a shutdown hook in order to get rid of the PID file when the JVM terminates.
      Parameters:
      path - the path to the file
    • getJvmPid

      public static long getJvmPid()
      Returns the process identifier of the containing JVM.
      Returns:
      the process identifier, may be -1 if not available