// ExitSecurityManager.java
//
// Created: November 2005
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

package org.biomoby.service.dashboard;

import org.apache.tools.ant.ExitException;
import java.security.Permission;

/**
 * A security manager allowing to enable and disable usage of
 * System.exit() calls. <p>
 *
 * Everybody can enable and disable it - so it is not strictly
 * speaking too secure but the purpose is not to prevent against
 * malicious classes but to allow to embed code and libraries that use
 * System.exit() - such as the Ant classes - without breaking wrapping
 * code. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: ExitSecurityManager.java,v 1.2 2006/02/19 18:42:55 senger Exp $
 */
public class ExitSecurityManager
    extends SecurityManager {

    private static org.apache.commons.logging.Log log =
       org.apache.commons.logging.LogFactory.getLog (ExitSecurityManager.class);

    private boolean exitForbidden = true;

    /*********************************************************************
     * Default constructor. By default, the System.exit() calls are
     * disabled.
     ********************************************************************/
    public ExitSecurityManager() {
	super();
    }

    /*********************************************************************
     * Create and install an instance of this exit manager as a new
     * security manager. It prints a warning message (into a log) if
     * installing was notr allowed because there is already another
     * security manager installed. <p>
     *
     * @return a new security manager (even if it was not possible to
     * install it; it never returns null)
     ********************************************************************/
    public static ExitSecurityManager createAndInstall() {

	ExitSecurityManager exitman = new ExitSecurityManager();
	try {
	    System.setSecurityManager (exitman);
	} catch (SecurityException e) {
	    log.warn ("Security manager already set.");
	}
	return exitman;
    }

    /*********************************************************************
     * Enable od disable usage of System.exit(). If it is disbaled and
     * then called, the call will raise an <tt>ExitException</tt>.
     *
     * @param enabled true enables System.exit() calls
     ********************************************************************/
    public void setExitForbidden (boolean enabled) {
	exitForbidden = enabled;
    }

    /*********************************************************************
     * @throws ExitException if System.exit() was called and was not
     * allowed. The exception has an exit code taken from this
     * System.exit() call.
     ********************************************************************/
    public void checkExit (int status) {
	if (exitForbidden)
	    throw new ExitException (status);
    }

    //
    // Methods that always return normally to allow operations
    //

    /*********************************************************************
     *
     ********************************************************************/
    public void checkPermission(Permission perm, Object context) { }

    /*********************************************************************
     *
     ********************************************************************/
    public void checkPermission(Permission perm) { }

}
