Preventing System.exit calls

When developing containers that run code written by other developers it is prudent to safe-guard against System.exit calls. If a developer inadvertently calls System.exit and deploys their code to be run by your container, it brings down the container process completely. This can be controlled using the checkExit function call in SecurityManager.

According to the reference for SecurityManager checkExit:

This method is invoked for the current security manager by the exit method of class Runtime. A status of 0 indicates success; other values indicate various errors.

Thus any call to exit invokes this method and we just have to throw an exception if we do not want the processing to continue further. We define our SecurityManager as below:

public class StopExitSecurityManager extends SecurityManager
    {
        private SecurityManager _prevMgr = System.getSecurityManager();

        public void checkPermission(Permission perm)
        {
        }

        public void checkExit(int status)
        {
            super.checkExit(status);
            throw new ExitTrappedException(); //This throws an exception if an exit is called.
        }

        public SecurityManager getPreviousMgr() { return _prevMgr; }
    }

Now, we can provide a ease of use CodeControl class as below:

public class CodeControl
{
    public CodeControl()
    {
        super();
    }

    public void disableSystemExit()
    {
        SecurityManager securityManager = new StopExitSecurityManager();
        System.setSecurityManager(securityManager) ;
    }    public void enableSystemExit()
    {
        SecurityManager mgr = System.getSecurityManager();
        if ((mgr != null) && (mgr instanceof StopExitSecurityManager))
        {
            StopExitSecurityManager smgr = (StopExitSecurityManager)mgr;
            System.setSecurityManager(smgr.getPreviousMgr());
        }
        else
            System.setSecurityManager(null);
    }
}

CodeControl can now be used as below:

CodeControl control = new CodeControl();
try
{
    control.disableSystemExit();
    //invoke the methods and other classes that are not allowed to call System.exit.
    Object ret = invokeExecute(_method, runWith, parms);
}
finally
{
    //finally enable exit
    control.enableSystemExit();
}

This will prevent the methods called within the disable and enable calls to call System.exit, but allow your code to call it without a problem.
 

Reference: Preventing System.exit calls from our JCG partner Raji Sankar at the Reflections blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Preventing System.exit calls"

  1. Techie Ram says:

    What is ExitTrappedException? where is it defined? Is it imported?

Leave a Reply


six + = 15



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close