1 19 20 package org.openide.execution; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.File ; 24 import java.io.PrintStream ; 25 import java.net.URL ; 26 import java.net.URLClassLoader ; 27 import java.net.URLConnection ; 28 import java.security.Permission ; 29 import java.util.Arrays ; 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.LocalFileSystem; 33 34 37 public class NbClassLoaderTest extends NbTestCase { 38 39 public NbClassLoaderTest(String name) { 40 super(name); 41 } 42 43 47 public void testUsingNbfsProtocol() throws Exception { 48 System.setProperty("org.netbeans.core.Plain.CULPRIT", "true"); 49 File here = new File (getClass().getProtectionDomain().getCodeSource().getLocation().getFile()); 50 assertTrue("Classpath really contains " + here, 51 new File (new File (new File (new File (here, "org"), "openide"), "execution"), "NbClassLoaderTest.class").canRead()); 52 53 File dataDir = new File (new File (new File (new File (here, "org"), "openide"), "execution"), "data"); 54 if(!dataDir.exists()) { 55 dataDir.mkdir(); 56 } 57 File fooFile = new File (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 cl = new NbClassLoader(new FileObject[] {lfs.getRoot()}, ClassLoader.getSystemClassLoader().getParent(), null); 66 System.setSecurityManager(new MySecurityManager()); 67 System.getProperty("foo"); 69 Class c = cl.loadClass("org.openide.execution.NbClassLoaderTest$User"); 70 assertEquals(cl, c.getClassLoader()); 71 try { 72 c.newInstance(); 73 } catch (ExceptionInInitializerError eiie) { 74 Throwable t = eiie.getException(); 75 if (t instanceof IllegalStateException ) { 76 fail(t.getMessage()); 77 } else if (t instanceof Exception ) { 78 throw (Exception )t; 79 } else { 80 throw new Exception (t.toString()); 81 } 82 } 83 } 84 85 public static final class User { 86 public User() throws Exception { 87 URLClassLoader ncl = (URLClassLoader )getClass().getClassLoader(); 88 URL [] urls = ncl.getURLs(); 89 if (urls.length != 1) throw new IllegalStateException ("Weird URLs: " + Arrays.asList(urls)); 90 URL manual = new URL (urls[0], "org/openide/execution/data/foo.xml"); 91 URLConnection uc = manual.openConnection(); 92 uc.connect(); 93 String ct = uc.getContentType(); 94 97 URL auto = getClass().getResource("data/foo.xml"); 98 if (auto == null) throw new IllegalStateException ("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 105 try { 107 System.getProperty("foo"); 108 throw new IllegalStateException ("Was permitted to access sys prop foo"); 109 } catch (SecurityException se) { 110 } 112 } 113 } 114 115 private static final class MySecurityManager extends SecurityManager { 116 public void checkPermission(Permission p) { 117 if (ok()) {return;} 119 try { 120 super.checkPermission(p); 121 } catch (SecurityException se) { 122 throw se; 125 } 126 } 127 public void checkPermission(Permission p, Object c) { 128 if (ok()) {return;} 129 super.checkPermission(p, c); 130 } 131 public void checkRead(String file) { 132 } 139 private boolean ok() { 140 Class [] 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 return true; 147 } 148 } 149 ByteArrayOutputStream baos = new ByteArrayOutputStream (1000); 150 new Exception ().printStackTrace(new PrintStream (baos)); 151 if (baos.toString().indexOf("\tat java.security.AccessController.doPrivileged") != -1) { 152 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
|