KickJava   Java API By Example, From Geeks To Geeks.

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


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.ProtectionDomain JavaDoc;
23 import java.security.AccessControlContext JavaDoc;
24 import java.security.AccessController JavaDoc;
25 import java.security.PermissionCollection JavaDoc;
26 import java.lang.reflect.Field JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29
30 /** Tries to get an IOProtectionDomain from an AccessControlContext.
31 *
32 * @author Ales Novak
33 */

34 class AccController {
35
36     /** array of ProtectionDomains */
37     static Field JavaDoc context;
38
39     static Field JavaDoc getContextField() throws Exception JavaDoc {
40         if (context == null) {
41             Field JavaDoc ctx;
42             try {
43                 ctx = AccessControlContext JavaDoc.class.getDeclaredField("context"); // NOI18N
44
} catch (NoSuchFieldException JavaDoc nsfe) { // IBM JDK1.5 has different field
45
ctx = AccessControlContext JavaDoc.class.getDeclaredField("domainsArray"); // NOI18N
46
}
47             ctx.setAccessible(true);
48             context = ctx;
49         }
50         return context;
51     }
52
53
54     static ProtectionDomain JavaDoc[] getDomains(AccessControlContext JavaDoc acc) throws Exception JavaDoc {
55         Object JavaDoc o = getContextField().get(acc);
56         if (o.getClass() == Object JavaDoc[].class) { // 1.2.1 fix
57
Object JavaDoc[] array = (Object JavaDoc[]) o;
58             ProtectionDomain JavaDoc[] domains = new ProtectionDomain JavaDoc[array.length];
59             for (int i = 0; i < array.length; i++) {
60                 domains[i] = (ProtectionDomain JavaDoc) array[i];
61             }
62             return domains;
63         }
64         return (ProtectionDomain JavaDoc[]) o;
65     }
66
67     /** @return an IOPermissionCollection or <tt>null</tt> if not found */
68     static IOPermissionCollection getIOPermissionCollection() {
69         return getIOPermissionCollection(AccessController.getContext());
70     }
71     
72     /** @return an IOPermissionCollection or <tt>null</tt> if not found */
73     static IOPermissionCollection getIOPermissionCollection(AccessControlContext JavaDoc acc) {
74         try {
75             ProtectionDomain JavaDoc[] pds = getDomains(acc);
76             PermissionCollection JavaDoc pc;
77             for (int i = 0; i < pds.length; i++) {
78                 pc = pds[i].getPermissions();
79                 if (pc instanceof IOPermissionCollection) {
80                     return (IOPermissionCollection) pc;
81                 }
82             }
83             return null;
84         } catch (final Exception JavaDoc e) {
85             javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
86                 public void run() {
87                     Logger.getLogger(AccController.class.getName()).log(Level.WARNING, null, e);
88                 }
89             });
90             return null;
91         }
92     }
93 }
94
Popular Tags