KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > execution > SecMan


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.execution;
21
22 import java.security.*;
23
24 import org.openide.execution.NbClassLoader;
25
26 /**
27  * A security manager for execution.
28  * @author Jesse Glick
29  */

30 public class SecMan extends SecurityManager JavaDoc {
31
32     public static SecurityManager JavaDoc DEFAULT = new SecMan();
33
34     private static Class JavaDoc nbClassLoaderClass = NbClassLoader.class;
35
36     /** thread group of executed classes */
37     private ThreadGroup JavaDoc base;
38
39     public SecMan() {
40         base = ExecutionEngine.base;
41     }
42
43     public void checkExit(int status) throws SecurityException JavaDoc {
44         PrivilegedCheck.checkExit(status, this);
45     }
46     
47     final void checkExitImpl(int status, AccessControlContext acc) throws SecurityException JavaDoc {
48         IOPermissionCollection iopc;
49         iopc = AccController.getIOPermissionCollection(acc);
50
51         if (iopc != null && iopc.grp != null) {
52             ExecutionEngine.getTaskIOs().free(iopc.grp, iopc.getIO()); // closes output
53
ExecutionEngine.closeGroup(iopc.grp);
54             stopTaskThreadGroup(iopc.grp);
55             throw new ExitSecurityException("Exit from within execution engine, normal"); // NOI18N
56
}
57
58         ThreadGroup JavaDoc g = Thread.currentThread().getThreadGroup ();
59         if (g instanceof TaskThreadGroup) {
60             throw new ExitSecurityException("Exit from within execution engine, normal"); // NOI18N
61
}
62         
63         if (isNbClassLoader()) {
64             throw new ExitSecurityException("Exit from within user-loaded code"); // NOI18N
65
}
66     }
67     
68     private void stopTaskThreadGroup(TaskThreadGroup old) {
69         synchronized(old) {
70             int count = old.activeCount();
71             int icurrent = -1;
72             Thread JavaDoc current = Thread.currentThread();
73             Thread JavaDoc[] thrs = new Thread JavaDoc[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             //throw new ExitSecurityException();
82
}
83     }
84     
85     public boolean checkTopLevelWindow(Object JavaDoc window) {
86         IOPermissionCollection iopc = AccController.getIOPermissionCollection();
87         if (iopc != null && iopc.grp != null && (window instanceof java.awt.Window JavaDoc)) {
88             ExecutionEngine.putWindow((java.awt.Window JavaDoc) window, iopc.grp);
89         }
90         return true;
91     }
92
93     /** @return true iff an instance of the NbClassLoader class is on the stack
94     */

95     protected boolean isNbClassLoader() {
96         Class JavaDoc[] ctx = getClassContext();
97         ClassLoader JavaDoc 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     /** mostly copied from TopSecurityManager */
109     private static final class PrivilegedCheck implements PrivilegedExceptionAction<Object JavaDoc> {
110         private final int action;
111         private final SecMan sm;
112         
113         // exit
114
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 JavaDoc run() throws Exception JavaDoc {
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 JavaDoc orig = e.getException();
147                 if (orig instanceof RuntimeException JavaDoc) {
148                     throw ((RuntimeException JavaDoc) orig);
149                 }
150                 orig.printStackTrace();
151             }
152         }
153     }
154     
155 }
156
Popular Tags