Skip to content

Commit

Permalink
Added JavaDoc to Exit* and GracefulExit classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vest committed Jan 11, 2025
1 parent acd1482 commit ec70b26
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions code/src/testcommon/util/ExitFunction.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package util;

/**
* A functional interface that can be used to exit the JVM.
*/
@FunctionalInterface
public interface ExitFunction
{
/**
* Is executed when the JVM should exit.
*
* @param status The status code to exit with
*/
void exit(int status);
}
9 changes: 9 additions & 0 deletions code/src/testcommon/util/ExitInterceptor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package util;

/**
* A functional interface that can be used to intercept the exit of the JVM.
*/
@FunctionalInterface
public interface ExitInterceptor
{
/**
* Is executed when the JVM should exit.
*
* @param status The status code to exit with
* @return true if the exit should be allowed, false if it should be intercepted
*/
boolean intercept(int status);
}
32 changes: 32 additions & 0 deletions code/src/testcommon/util/GracefulExit.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* This class provides a mechanism to intercept calls to System.exit and perform additional actions before the JVM exits.
* <p>
* This class is thread-safe.
*/
public class GracefulExit
{
private static final Logger LOG = Logger.getLogger(GracefulExit.class.getName());
Expand All @@ -24,6 +29,17 @@ private static class Lock

private static ExitFunction exitFunction = System::exit;

private GracefulExit()
{
}

/**
* Exits the JVM with the given status code. This method will call all registered interceptors before exiting.
* <p>
* If any interceptor returns false, the exit will be canceled.
*
* @param status The status code to exit with
*/
static void exit(int status)
{
synchronized (lock)
Expand Down Expand Up @@ -56,6 +72,12 @@ static void exit(int status)
}
}

/**
* Adds an interceptor to the list of interceptors. This method can be called before or after shutdown has started,
* otherwise an exception will be thrown.
*
* @param interceptor The interceptor to add
*/
public static void addExitInterceptor(ExitInterceptor interceptor)
{
synchronized (lock)
Expand All @@ -72,6 +94,10 @@ public static void addExitInterceptor(ExitInterceptor interceptor)
}
}

/**
* Clears all registered interceptors. This method can be called before or after shutdown has started, otherwise an
* exception will be thrown.
*/
public static void clearExitInterceptors()
{
synchronized (lock)
Expand All @@ -84,6 +110,12 @@ public static void clearExitInterceptors()
}
}

/**
* Registers an exit function to be called when the JVM exits. This method can be called before or after shutdown has
* started, otherwise an exception will be thrown.
*
* @param exitFunction The exit function to register
*/
public static void registerExitFunction(ExitFunction exitFunction)
{
synchronized (lock)
Expand Down

0 comments on commit ec70b26

Please sign in to comment.