1 33 34 package edu.rice.cs.util; 35 36 import java.security.*; 37 38 46 public class PreventExitSecurityManager extends SecurityManager { 47 48 private static final Permission SET_MANAGER_PERM = new RuntimePermission ("setSecurityManager"); 49 50 private final SecurityManager _parent; 51 52 53 private boolean _exitAttempted = false; 54 55 56 private boolean _timeToExit = false; 57 58 59 private boolean _blockExit = false; 60 61 62 private boolean _timeToDeactivate = false; 63 64 68 private PreventExitSecurityManager(final SecurityManager parent) { _parent = parent; } 69 70 72 public static PreventExitSecurityManager activate() { 73 SecurityManager 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 82 public void deactivate() { 83 _timeToDeactivate = true; 84 System.setSecurityManager(_parent); 85 } 86 87 90 public void exitVM(int status) { 91 if (! _blockExit) _timeToExit = true; 93 System.exit(status); 94 } 95 96 100 public void setBlockExit(boolean b) { _blockExit = b; } 101 102 103 public boolean exitAttempted() { 104 boolean old = _exitAttempted; 105 _exitAttempted = false; 106 return old; 107 } 108 109 110 public void checkPermission(Permission perm) { 111 if (perm.equals(SET_MANAGER_PERM)) { 112 if (! _timeToDeactivate) throw new SecurityException ("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 |