MCPcopy Create free account
hub / github.com/PCGen/pcgen / GracefulExit

Class GracefulExit

code/src/java/pcgen/util/GracefulExit.java:17–142  ·  view source on GitHub ↗

This class provides a mechanism to intercept calls to System.exit and perform additional actions before the JVM exits. This class is thread-safe.

Source from the content-addressed store, hash-verified

15 * This class is thread-safe.
16 */
17public final class GracefulExit
18{
19 private static final Logger LOG = Logger.getLogger(GracefulExit.class.getName());
20
21 /* This lock protects the preceding static fields */
22 private static final ReentrantLock lock = new ReentrantLock();
23
24 private static final List<ExitInterceptor> interceptors = new ArrayList<>();
25
26 private static boolean isShuttingDown;
27
28 private static ExitFunction exitFunction = System::exit;
29
30 private GracefulExit()
31 {
32 }
33
34 /**
35 * Exits the JVM with the given status code. This method will call all registered interceptors before exiting.
36 * <p>
37 * If any interceptor returns false, the exit will be canceled.
38 *
39 * @param status The status code to exit with
40 */
41 public static void exit(int status)
42 {
43 try
44 {
45 lock.lock();
46 LOG.log(Level.FINEST, () -> MessageFormat.format(
47 "Started exiting with the status {0}. There are {1} interceptors.", status,
48 interceptors.size()));
49 isShuttingDown = true;
50
51 boolean shouldExit = interceptors.stream()
52 .map((ExitInterceptor interceptor) -> {
53 boolean intercepted = interceptor.intercept(status);
54 LOG.log(Level.FINEST,
55 MessageFormat.format("Intercepted exit: {0} from registered interceptor: {1}",
56 intercepted, interceptor.getClass().getName()));
57 return intercepted;
58 })
59 .filter(Predicate.isEqual(Boolean.FALSE))
60 .findAny()
61 .orElse(Boolean.TRUE);
62
63 if (shouldExit)
64 {
65 LOG.info(() -> MessageFormat.format("Exiting gracefully with status {0}", status));
66 exitFunction.exit(status);
67 }
68 } finally
69 {
70 isShuttingDown = false;
71 lock.unlock();
72 }
73 }
74

Callers

nothing calls this directly

Calls 2

getLoggerMethod · 0.80
getNameMethod · 0.65

Tested by

no test coverage detected