1 19 20 package org.netbeans.core.execution; 21 22 import java.security.*; 23 24 import org.openide.execution.NbClassLoader; 25 26 30 public class SecMan extends SecurityManager { 31 32 public static SecurityManager DEFAULT = new SecMan(); 33 34 private static Class nbClassLoaderClass = NbClassLoader.class; 35 36 37 private ThreadGroup base; 38 39 public SecMan() { 40 base = ExecutionEngine.base; 41 } 42 43 public void checkExit(int status) throws SecurityException { 44 PrivilegedCheck.checkExit(status, this); 45 } 46 47 final void checkExitImpl(int status, AccessControlContext acc) throws SecurityException { 48 IOPermissionCollection iopc; 49 iopc = AccController.getIOPermissionCollection(acc); 50 51 if (iopc != null && iopc.grp != null) { 52 ExecutionEngine.getTaskIOs().free(iopc.grp, iopc.getIO()); ExecutionEngine.closeGroup(iopc.grp); 54 stopTaskThreadGroup(iopc.grp); 55 throw new ExitSecurityException("Exit from within execution engine, normal"); } 57 58 ThreadGroup g = Thread.currentThread().getThreadGroup (); 59 if (g instanceof TaskThreadGroup) { 60 throw new ExitSecurityException("Exit from within execution engine, normal"); } 62 63 if (isNbClassLoader()) { 64 throw new ExitSecurityException("Exit from within user-loaded code"); } 66 } 67 68 private void stopTaskThreadGroup(TaskThreadGroup old) { 69 synchronized(old) { 70 int count = old.activeCount(); 71 int icurrent = -1; 72 Thread current = Thread.currentThread(); 73 Thread [] thrs = new Thread [count]; 74 old.enumerate(thrs, true); 75 for (int i = 0; i < thrs.length; i++) { 76 if (thrs[i] == null) break; 77 if (thrs[i] == current) icurrent = i; 78 else thrs[i].stop(); 79 } 80 if (icurrent != -1) thrs[icurrent].stop(); 81 } 83 } 84 85 public boolean checkTopLevelWindow(Object window) { 86 IOPermissionCollection iopc = AccController.getIOPermissionCollection(); 87 if (iopc != null && iopc.grp != null && (window instanceof java.awt.Window )) { 88 ExecutionEngine.putWindow((java.awt.Window ) window, iopc.grp); 89 } 90 return true; 91 } 92 93 95 protected boolean isNbClassLoader() { 96 Class [] ctx = getClassContext(); 97 ClassLoader cloader; 98 99 for (int i = 0; i < ctx.length; i++) { 100 if ((nbClassLoaderClass.isInstance(ctx[i].getClassLoader())) && 101 (ctx[i].getProtectionDomain().getCodeSource() != null)) { 102 return true; 103 } 104 } 105 return false; 106 } 107 108 109 private static final class PrivilegedCheck implements PrivilegedExceptionAction<Object > { 110 private final int action; 111 private final SecMan sm; 112 113 private int status; 115 private AccessControlContext acc; 116 117 public PrivilegedCheck(int action, SecMan sm) { 118 this.action = action; 119 this.sm = sm; 120 121 if (action == 0) { 122 acc = AccessController.getContext(); 123 } 124 } 125 126 public Object run() throws Exception { 127 switch (action) { 128 case 0 : 129 sm.checkExitImpl(status, acc); 130 break; 131 default : 132 } 133 return null; 134 } 135 136 static void checkExit(int status, SecMan sm) { 137 PrivilegedCheck pea = new PrivilegedCheck(0, sm); 138 pea.status = status; 139 check(pea); 140 } 141 142 private static void check(PrivilegedCheck action) { 143 try { 144 AccessController.doPrivileged(action); 145 } catch (PrivilegedActionException e) { 146 Exception orig = e.getException(); 147 if (orig instanceof RuntimeException ) { 148 throw ((RuntimeException ) orig); 149 } 150 orig.printStackTrace(); 151 } 152 } 153 } 154 155 } 156 | Popular Tags |