Class PidFile
java.lang.Object
de.iip_ecosphere.platform.support.PidFile
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidaddShutdownHook(Path path) Adds a shutdown hook in order to get rid of the PID file when the JVM terminates.static PidFileCreates a new PidFile and writes the current process ID into the provided path.(package private) static PidFileCreates a new PidFile with given process identifier and writes the identifier into the provided path.static PidFilecreateInDefaultDir(String name, boolean deleteOnExit) Creates a new PidFile in the system's temporary directory as a file with givennameand writes the current process ID into that file.static PidFilecreateInDefaultDir(String name, boolean deleteOnExit, boolean deleteIfExists) Creates a new PidFile in the system's temporary directory as a file with givennameand writes the current process ID into that file.static longReturns the process identifier of the containing JVM.getPath()Returns the PID file path.longgetPid()Returns the PID of the current JVM.static StringReturns the path of the PID directory viaPID_DIR_PROPERTY_NAMEor temp directory.booleanReturnstrueiff the process id file is deleted on system exit.
-
Field Details
-
PID_DIR_PROPERTY_NAME
Denotes the directory where PID files shall be created within. If not specified,the temporary directoryis used.- See Also:
-
pid
private final long pid -
path
-
deleteOnExit
private final boolean deleteOnExit
-
-
Constructor Details
-
PidFile
Creates an instance.- Parameters:
path- the path where to create the file withindeleteOnExit- delete the file on exit of the JVMpid- the PID to write into the file- Throws:
IOException- if an I/O problem occurs while creating the PID fileIllegalArgumentException- if the parent ofpathis not a directory or the denoted file is not a regular file
-
-
Method Details
-
create
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- iftruethe PID file is deleted with best effort on system exit- Returns:
- the PidFile instance
- Throws:
IOException- if an I/O problem occursIllegalArgumentException- if the parent ofpathis not a directory or the denoted file is not a regular file
-
createInDefaultDir
Creates a new PidFile in the system's temporary directory as a file with givennameand 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- iftruethe PID file is deleted with best effort on system exit- Returns:
- the PidFile instance
- Throws:
IOException- if an I/O problem occursIllegalArgumentException- if the parent ofpathis not a directory or the denoted file is not a regular file
-
getPidDirectory
Returns the path of the PID directory viaPID_DIR_PROPERTY_NAMEor 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 givennameand writes the current process ID into that file.- Parameters:
name- the name of the file within the system'S temporary directory.deleteOnExit- iftruethe PID file is deleted with best effort on system exitdeleteIfExists- iftrue, an existing file is deleted; iffalsethe file is newly created or truncated if it already exists.- Returns:
- the PidFile instance
- Throws:
IOException- if an I/O problem occursIllegalArgumentException- if the parent ofpathis not a directory or the denoted file is not a regular file
-
create
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- iftruethe pid file is deleted with best effort on system exitpid- the PID to write into the file- Returns:
- the PidFile instance
- Throws:
IOException- if an I/O problem occursIllegalArgumentException- if the parent ofpathis 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
-1if the PID cannot be determined
-
getPath
Returns the PID file path.- Returns:
- PID file path
-
isDeleteOnExit
public boolean isDeleteOnExit()Returnstrueiff the process id file is deleted on system exit.- Returns:
trueof deletion of system exit is enabled, otherwisefalse.
-
addShutdownHook
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
-1if not available
-