...
Code Block | ||
---|---|---|
| ||
public class InterceptExit { public static void main(String[] args) { // ... System.exit(1); // Abrupt exit System.out.println("This is never executedexecutes"); } } |
Compliant Solution
This compliant solution installs a custom security manager PasswordSecurityManager
that overrides the checkExit()
method defined in the SecurityManager
class. This is necessary in cases where some clean up actions must be performed prior to allowing the exit. The default checkExit()
method in the SecurityManager
class does not offer this facility.
In the overridden implementation, an . An internal flag is used to keep track of whether the exit is permitted or not. The method setExitAllowed()
is used to set this flag. If the flag is not set (false
), a SecurityException
is thrown. The System.exit()
call is not allowed to execute by catching the SecurityException
in a try-catch
block. After intercepting and performing mandatory clean-up operations, the setExitAllowed()
method is invoked. As a result, the program exits gracefully.
Code Block | ||
---|---|---|
| ||
class PasswordSecurityManager extends SecurityManager { private boolean isExitAllowedFlag; public PasswordSecurityManager(){ super(); isExitAllowedFlag = false; } public boolean isExitAllowed(){ return isExitAllowedFlag; } @Override public void checkExit(int status) { if(!isExitAllowed()) throw new SecurityException(); } public void setExitAllowed(boolean f) { isExitAllowedFlag = f; } } public class InterceptExit { public static void main(String[] args) { PasswordSecurityManager secManager = new PasswordSecurityManager(); System.setSecurityManager(secManager); try { // ... System.exit(1); // Abrupt exit call } catch (Throwable x) { if (x instanceof SecurityException) { System.out.println("Intercepted System.exit()"); } else { // Forward to exception handler } } // ... secManager.setExitAllowed(true); // Permit exit // System.exit() will work subsequently // ... } } |
...
- Hook threads should be light-weight and simple
- They should be thread safe
- They should hold locks when accessing data and release them when done
Wiki Markup They should not rely on system services as the services themselves may be shutting down (for example, the logger may shutdown from another hook). Instead of one service it may be better to run a series of shutdown tasks from one thread by using a single shutdown hook. \[[Goetz 06|AA. Java References#Goetz 06]\]
This compliant solution shows the standard method to install a hook.
...