Package org.zeroturnaround.exec.listener
Class ShutdownHookProcessDestroyer
- java.lang.Object
-
- org.zeroturnaround.exec.listener.ShutdownHookProcessDestroyer
-
- All Implemented Interfaces:
java.lang.Runnable
,ProcessDestroyer
public class ShutdownHookProcessDestroyer extends java.lang.Object implements ProcessDestroyer, java.lang.Runnable
Destroys all registeredProcess
es when the VM exits.This class is copied from
Commons Exec
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
ShutdownHookProcessDestroyer.ProcessDestroyerImpl
-
Field Summary
Fields Modifier and Type Field Description private boolean
added
Whether or not this ProcessDestroyer has been registered as a shutdown hookprivate ShutdownHookProcessDestroyer.ProcessDestroyerImpl
destroyProcessThread
The thread registered at the JVM to execute the shutdown handlerstatic ProcessDestroyer
INSTANCE
Singleton instance of theShutdownHookProcessDestroyer
.private static org.slf4j.Logger
log
private java.util.Vector<java.lang.Process>
processes
the list of currently running processesprivate boolean
running
Whether or not this ProcessDestroyer is currently running as shutdown hookprivate boolean
shutDownHookExecuted
Whether the shut down hook routine was already run
-
Constructor Summary
Constructors Constructor Description ShutdownHookProcessDestroyer()
Constructs aProcessDestroyer
and obtainsRuntime.addShutdownHook()
andRuntime.removeShutdownHook()
through reflection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(java.lang.Process process)
Returnstrue
if the specifiedProcess
was successfully added to the list of processes to destroy upon VM exit.private void
addShutdownHook()
Registers thisProcessDestroyer
as a shutdown hook, uses reflection to ensure pre-JDK 1.3 compatibility.private void
destroy(java.lang.Process process)
boolean
isAddedAsShutdownHook()
Returns whether or not the ProcessDestroyer is registered as as shutdown hookboolean
remove(java.lang.Process process)
Returnstrue
if the specifiedProcess
was successfully removed from the list of processes to destroy upon VM exit.private void
removeShutdownHook()
Removes thisProcessDestroyer
as a shutdown hook, uses reflection to ensure pre-JDK 1.3 compatibilityvoid
run()
Invoked by the VM when it is exiting.int
size()
Returns the number of registered processes.
-
-
-
Field Detail
-
log
private static final org.slf4j.Logger log
-
INSTANCE
public static final ProcessDestroyer INSTANCE
Singleton instance of theShutdownHookProcessDestroyer
.
-
processes
private final java.util.Vector<java.lang.Process> processes
the list of currently running processes
-
destroyProcessThread
private ShutdownHookProcessDestroyer.ProcessDestroyerImpl destroyProcessThread
The thread registered at the JVM to execute the shutdown handler
-
added
private boolean added
Whether or not this ProcessDestroyer has been registered as a shutdown hook
-
shutDownHookExecuted
private volatile boolean shutDownHookExecuted
Whether the shut down hook routine was already run
-
running
private volatile boolean running
Whether or not this ProcessDestroyer is currently running as shutdown hook
-
-
Constructor Detail
-
ShutdownHookProcessDestroyer
public ShutdownHookProcessDestroyer()
Constructs aProcessDestroyer
and obtainsRuntime.addShutdownHook()
andRuntime.removeShutdownHook()
through reflection. The ProcessDestroyer manages a list of processes to be destroyed when the VM exits. If a process is added when the list is empty, thisProcessDestroyer
is registered as a shutdown hook. If removing a process results in an empty list, theProcessDestroyer
is removed as a shutdown hook.
-
-
Method Detail
-
addShutdownHook
private void addShutdownHook()
Registers thisProcessDestroyer
as a shutdown hook, uses reflection to ensure pre-JDK 1.3 compatibility.
-
removeShutdownHook
private void removeShutdownHook()
Removes thisProcessDestroyer
as a shutdown hook, uses reflection to ensure pre-JDK 1.3 compatibility
-
isAddedAsShutdownHook
public boolean isAddedAsShutdownHook()
Returns whether or not the ProcessDestroyer is registered as as shutdown hook- Returns:
- true if this is currently added as shutdown hook
-
add
public boolean add(java.lang.Process process)
Returnstrue
if the specifiedProcess
was successfully added to the list of processes to destroy upon VM exit.- Specified by:
add
in interfaceProcessDestroyer
- Parameters:
process
- the process to add- Returns:
true
if the specifiedProcess
was successfully added
-
remove
public boolean remove(java.lang.Process process)
Returnstrue
if the specifiedProcess
was successfully removed from the list of processes to destroy upon VM exit.- Specified by:
remove
in interfaceProcessDestroyer
- Parameters:
process
- the process to remove- Returns:
true
if the specifiedProcess
was successfully removed
-
size
public int size()
Returns the number of registered processes.- Specified by:
size
in interfaceProcessDestroyer
- Returns:
- the number of register process
-
run
public void run()
Invoked by the VM when it is exiting.- Specified by:
run
in interfacejava.lang.Runnable
-
destroy
private void destroy(java.lang.Process process)
-
-