1 19 20 package org.openide.execution; 21 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.net.URLClassLoader ; 26 import java.security.AllPermission ; 27 import java.security.CodeSource ; 28 import java.security.Permission ; 29 import java.security.PermissionCollection ; 30 import java.util.Enumeration ; 31 import java.util.HashMap ; 32 import java.util.jar.Manifest ; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileStateInvalidException; 35 import org.openide.filesystems.FileSystem; 36 import org.openide.filesystems.URLMapper; 37 import org.openide.util.Exceptions; 38 import org.openide.util.Lookup; 39 import org.openide.windows.InputOutput; 40 41 47 public class NbClassLoader extends URLClassLoader { 48 49 protected InputOutput inout; 50 51 private HashMap permissionCollections; 52 53 private PermissionCollection defaultPermissions; 54 55 private static ClassLoader systemClassLoader() { 56 return (ClassLoader )Lookup.getDefault().lookup(ClassLoader .class); 57 } 58 59 64 public NbClassLoader () { 65 super(new URL [0], systemClassLoader()); 66 } 67 68 74 public NbClassLoader(InputOutput io) { 75 super(new URL [0], systemClassLoader()); 76 inout = io; 77 } 78 79 87 public NbClassLoader(FileObject[] roots, ClassLoader parent, InputOutput io) throws FileStateInvalidException { 88 super(createRootURLs(roots), parent); 89 inout = io; 90 } 91 92 96 public NbClassLoader (FileSystem[] fileSystems) { 97 super(new URL [0], systemClassLoader(), null); 98 Thread.dumpStack(); 99 } 100 101 106 public NbClassLoader (FileSystem[] fileSystems, ClassLoader parent) { 107 super(new URL [0], parent); 108 Thread.dumpStack(); 109 } 110 111 116 public URL getResource (String name) { 117 return super.getResource (name.startsWith ("/") ? name.substring (1) : name); } 119 120 126 protected Class findClass (final String name) throws ClassNotFoundException { 127 if (name.indexOf ('.') != -1) { 128 String pkg = name.substring (0, name.lastIndexOf ('.')); 129 if (getPackage (pkg) == null) { 130 String resource = name.replace ('.', '/') + ".class"; URL [] urls = getURLs (); 132 for (int i = 0; i < urls.length; i++) { 133 FileObject root = URLMapper.findFileObject(urls[i]); 135 136 if (root == null) { 137 continue; 138 } 139 try { 140 FileObject fo = root.getFileObject(resource); 141 142 if (fo != null) { 143 FileObject manifo = root.getFileObject("META-INF/MANIFEST.MF"); 145 146 if (manifo == null) 147 manifo = root.getFileObject("meta-inf/manifest.mf"); 148 if (manifo != null) { 149 Manifest mani = new Manifest (); 151 InputStream is = manifo.getInputStream(); 152 153 try { 154 mani.read(is); 155 } 156 finally { 157 is.close(); 158 } 159 definePackage(pkg, mani, urls[i]); 160 } 161 break; 162 } 163 } 164 catch (IOException ioe) { 165 Exceptions.attachLocalizedMessage(ioe, 166 urls[i].toString()); 167 Exceptions.printStackTrace(ioe); 168 continue; 169 } 170 } 171 } 172 } 173 return super.findClass (name); 174 } 175 176 181 public void setDefaultPermissions(PermissionCollection defaultPerms) { 182 if (defaultPerms != null && !defaultPerms.isReadOnly()) { 183 defaultPerms.setReadOnly(); 184 } 185 this.defaultPermissions = defaultPerms; 186 } 187 188 189 protected final synchronized PermissionCollection getPermissions(CodeSource cs) { 190 191 if (permissionCollections != null) { 192 PermissionCollection pc = (PermissionCollection ) permissionCollections.get(cs); 193 if (pc != null) { 194 return pc; 195 } 196 } 197 198 return createPermissions(cs, inout); 199 } 200 201 206 private PermissionCollection createPermissions(CodeSource cs, InputOutput inout) { 207 PermissionCollection pc; 208 if (inout == null) { 209 if (defaultPermissions != null) { 210 pc = defaultPermissions; 211 } else { 212 pc = super.getPermissions(cs); 213 } 214 } else { 215 ExecutionEngine engine = ExecutionEngine.getDefault(); 216 pc = engine.createPermissions(cs, inout); 217 if (defaultPermissions != null) { 218 addAllPermissions(pc, defaultPermissions); 219 } else { 220 pc.add(new AllPermission ()); 221 } 222 } 223 if (permissionCollections == null) { 224 permissionCollections = new HashMap (7); 225 } 226 permissionCollections.put(cs, pc); 227 return pc; 228 } 229 230 236 private static void addAllPermissions(PermissionCollection target, PermissionCollection src) { 237 Enumeration e = src.elements(); 238 239 while (e.hasMoreElements()) { 240 target.add((Permission ) e.nextElement()); 241 } 242 } 243 244 245 250 private static URL [] createRootURLs(FileObject[] roots) throws FileStateInvalidException { 251 URL [] urls = new URL [roots.length]; 252 for (int i = 0; i < roots.length; i++) { 253 urls[i] = roots[i].getURL(); 254 } 255 return urls; 256 } 257 } 258 | Popular Tags |