KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > execution > NbClassLoaderTest


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.openide.execution;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLClassLoader JavaDoc;
27 import java.net.URLConnection JavaDoc;
28 import java.security.Permission JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.LocalFileSystem;
33
34 /** Test NbClassLoader.
35  * @author Jesse Glick
36  */

37 public class NbClassLoaderTest extends NbTestCase {
38
39     public NbClassLoaderTest(String JavaDoc name) {
40         super(name);
41     }
42     
43     /** Ensure that a user-mode class can at least use findResource() to access
44      * resources in filesystems.
45      * @see "#13038"
46      */

47     public void testUsingNbfsProtocol() throws Exception JavaDoc {
48         System.setProperty("org.netbeans.core.Plain.CULPRIT", "true");
49         File JavaDoc here = new File JavaDoc(getClass().getProtectionDomain().getCodeSource().getLocation().getFile());
50         assertTrue("Classpath really contains " + here,
51                 new File JavaDoc(new File JavaDoc(new File JavaDoc(new File JavaDoc(here, "org"), "openide"), "execution"), "NbClassLoaderTest.class").canRead());
52         
53         File JavaDoc dataDir = new File JavaDoc(new File JavaDoc(new File JavaDoc(new File JavaDoc(here, "org"), "openide"), "execution"), "data");
54         if(!dataDir.exists()) {
55             dataDir.mkdir();
56         }
57         File JavaDoc fooFile = new File JavaDoc(dataDir, "foo.xml");
58         if(!fooFile.exists()) {
59             fooFile.createNewFile();
60         }
61         
62         LocalFileSystem lfs = new LocalFileSystem();
63         lfs.setRootDirectory(here);
64         lfs.setReadOnly(true);
65         ClassLoader JavaDoc cl = new NbClassLoader(new FileObject[] {lfs.getRoot()}, ClassLoader.getSystemClassLoader().getParent(), null);
66         System.setSecurityManager(new MySecurityManager());
67         // Ensure this class at least has free access:
68
System.getProperty("foo");
69         Class JavaDoc c = cl.loadClass("org.openide.execution.NbClassLoaderTest$User");
70         assertEquals(cl, c.getClassLoader());
71         try {
72             c.newInstance();
73         } catch (ExceptionInInitializerError JavaDoc eiie) {
74             Throwable JavaDoc t = eiie.getException();
75             if (t instanceof IllegalStateException JavaDoc) {
76                 fail(t.getMessage());
77             } else if (t instanceof Exception JavaDoc) {
78                 throw (Exception JavaDoc)t;
79             } else {
80                 throw new Exception JavaDoc(t.toString());
81             }
82         }
83     }
84     
85     public static final class User {
86         public User() throws Exception JavaDoc {
87             URLClassLoader JavaDoc ncl = (URLClassLoader JavaDoc)getClass().getClassLoader();
88             URL JavaDoc[] urls = ncl.getURLs();
89             if (urls.length != 1) throw new IllegalStateException JavaDoc("Weird URLs: " + Arrays.asList(urls));
90             URL JavaDoc manual = new URL JavaDoc(urls[0], "org/openide/execution/data/foo.xml");
91             URLConnection JavaDoc uc = manual.openConnection();
92             uc.connect();
93             String JavaDoc ct = uc.getContentType();
94             /* May now be a file: URL, in which case content type is hard to control:
95             if (!"text/xml".equals(ct)) throw new IllegalStateException("Wrong content type (manual): " + ct);
96              */

97             URL JavaDoc auto = getClass().getResource("data/foo.xml");
98             if (auto == null) throw new IllegalStateException JavaDoc("Could not load data/foo.xml; try uncommenting se.printStackTrace() in MySecurityManager.checkPermission");
99             uc = auto.openConnection();
100             uc.connect();
101             ct = uc.getContentType();
102             /* Ditto:
103             if (!"text/xml".equals(ct)) throw new IllegalStateException("Wrong content type (auto): " + ct);
104              */

105             // Ensure this class does *not* have free access to random permissions:
106
try {
107                 System.getProperty("foo");
108                 throw new IllegalStateException JavaDoc("Was permitted to access sys prop foo");
109             } catch (SecurityException JavaDoc se) {
110                 // good
111
}
112         }
113     }
114     
115     private static final class MySecurityManager extends SecurityManager JavaDoc {
116         public void checkPermission(Permission JavaDoc p) {
117             //System.err.println("cP: " + p);
118
if (ok()) {/*System.err.println("ok");*/return;}
119             try {
120                 super.checkPermission(p);
121             } catch (SecurityException JavaDoc se) {
122                 //se.printStackTrace();
123
//System.err.println("classes: " + Arrays.asList(getClassContext()));
124
throw se;
125             }
126         }
127         public void checkPermission(Permission JavaDoc p, Object JavaDoc c) {
128             if (ok()) {/*System.err.println("ok");*/return;}
129             super.checkPermission(p, c);
130         }
131         public void checkRead(String JavaDoc file) {
132             // Do not honor file read checks. TopSecurityManager actually leaves
133
// this blank for performance, but in fact very little would work if
134
// we restricted reads. nbfs: protocols would be useless, meaning user
135
// classes would not be able to load resources. This could be solved if
136
// necessary by creating a special kind of FilePermission returned from
137
// FileURL; it would be recognized by TSM.checkPermission and accepted.
138
}
139         private boolean ok() {
140             Class JavaDoc[] cs = getClassContext();
141             int i = 0;
142             while (i < cs.length && cs[i] == MySecurityManager.class) i++;
143             for (; i < cs.length; i++) {
144                 if (cs[i] == MySecurityManager.class) {
145                     // avoid recursion
146
return true;
147                 }
148             }
149             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(1000);
150             new Exception JavaDoc().printStackTrace(new PrintStream JavaDoc(baos));
151             if (baos.toString().indexOf("\tat java.security.AccessController.doPrivileged") != -1) {
152                 // Cheap check for privileged actions.
153
// For some reason AccessController does not appear in the classContext
154
// (perhaps because it is a native method?).
155
return true;
156             }
157             for (int j = 0; j < cs.length; j++) {
158                 if (cs[j].getClassLoader() instanceof NbClassLoader) {
159                     return false;
160                 }
161             }
162             return true;
163         }
164     }
165     
166 }
167
Popular Tags