KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > PreventExitSecurityManager


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util;
35
36 import java.security.*;
37
38 /** Not used anymore. <p>
39  * A security manager to prevent exiting the slaveVM indiscriminately. This manager disallows System.exit
40  * (unless you call {@link #exitVM}). It also disallows setting a security manager, since this would override
41  * the exit prevention! If this security manager is enabled and an exit is attempted, either via System.exit
42  * or via {@link #exitVM} when exiting is blocked, a {@link ExitingNotAllowedException} will be thrown.
43  *
44  * @version $Id: PreventExitSecurityManager.java 3901 2006-06-30 05:28:11Z rcartwright $
45  */

46 public class PreventExitSecurityManager extends SecurityManager JavaDoc {
47   
48   private static final Permission SET_MANAGER_PERM = new RuntimePermission JavaDoc("setSecurityManager");
49
50   private final SecurityManager JavaDoc _parent;
51
52   /** Has an unauthorized exit been attempted? */
53   private boolean _exitAttempted = false;
54
55   /** Is it time to exit, for real? */
56   private boolean _timeToExit = false;
57
58   /** Are we in exit blocking mode? */
59   private boolean _blockExit = false;
60
61   /** Is it time to unset this security manager? */
62   private boolean _timeToDeactivate = false;
63
64   /** Creates a PreventExitSecurityManager, delegating all permission checks except for exiting to the given parent
65    * manager.
66    * @param parent SecurityManager to delegate permission to. This may be null, signifying to allow all.
67    */

68   private PreventExitSecurityManager(final SecurityManager JavaDoc parent) { _parent = parent; }
69
70   /** Creates a new exit-preventing security manager, using the previous security manager to delegate to.
71    */

72   public static PreventExitSecurityManager activate() {
73     SecurityManager JavaDoc currentMgr = System.getSecurityManager();
74     if (currentMgr instanceof PreventExitSecurityManager) return (PreventExitSecurityManager) currentMgr;
75     
76     PreventExitSecurityManager mgr = new PreventExitSecurityManager(System.getSecurityManager());
77     System.setSecurityManager(mgr);
78     return mgr;
79   }
80
81   /** Removes this security manager. */
82   public void deactivate() {
83     _timeToDeactivate = true;
84     System.setSecurityManager(_parent);
85   }
86
87   /** Exits the VM unless exiting is presently blocked. Blocking exit is used in test cases that want to see if we
88    * try to exit.
89    */

90   public void exitVM(int status) {
91 // Utilities.showDebug("exitVm(" + status + ") called");
92
if (! _blockExit) _timeToExit = true;
93     System.exit(status);
94   }
95
96   /** Sets whether exiting the VM is unconditionally blocked or not. It's useful to block exiting to allow test cases
97    * to pretend to exit, just to make sure it would have exited under certain conditions.
98    * @param b If true, exiting will never be allowed until set false.
99    */

100   public void setBlockExit(boolean b) { _blockExit = b; }
101
102   /** Returns whether a System.exit was attempted since the last time this method was called. */
103   public boolean exitAttempted() {
104     boolean old = _exitAttempted;
105     _exitAttempted = false;
106     return old;
107   }
108
109   /** Disallow setting security manager, but otherwise delegate to parent. */
110   public void checkPermission(Permission perm) {
111     if (perm.equals(SET_MANAGER_PERM)) {
112       if (! _timeToDeactivate) throw new SecurityException JavaDoc("Can not reset security manager!");
113     }
114     else {
115       if (_parent != null) _parent.checkPermission(perm);
116     }
117   }
118
119   public void checkExit(int status) {
120     if (! _timeToExit) {
121       _exitAttempted = true;
122       throw new ExitingNotAllowedException();
123     }
124   }
125 }
126
127
128
Popular Tags