1 11 12 package org.eclipse.osgi.internal.baseadaptor; 13 14 import java.io.IOException ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.security.*; 18 import java.security.cert.Certificate ; 19 import java.util.Enumeration ; 20 import org.eclipse.osgi.baseadaptor.BaseData; 21 import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry; 22 import org.eclipse.osgi.baseadaptor.bundlefile.BundleFile; 23 import org.eclipse.osgi.baseadaptor.loader.*; 24 import org.eclipse.osgi.framework.adaptor.BundleData; 25 import org.eclipse.osgi.framework.adaptor.ClassLoaderDelegate; 26 import org.eclipse.osgi.framework.debug.Debug; 27 import org.eclipse.osgi.internal.provisional.verifier.CertificateChain; 28 import org.eclipse.osgi.internal.provisional.verifier.CertificateVerifier; 29 30 36 public class DefaultClassLoader extends ClassLoader implements BaseClassLoader { 37 40 protected static final PermissionCollection ALLPERMISSIONS; 41 static { 42 AllPermission allPerm = new AllPermission(); 43 ALLPERMISSIONS = allPerm.newPermissionCollection(); 44 if (ALLPERMISSIONS != null) 45 ALLPERMISSIONS.add(allPerm); 46 } 47 48 protected ClassLoaderDelegate delegate; 49 protected ProtectionDomain domain; 50 protected ClasspathManager manager; 51 52 60 public DefaultClassLoader(ClassLoader parent, ClassLoaderDelegate delegate, ProtectionDomain domain, BaseData bundledata, String [] classpath) { 61 super(parent); 62 this.delegate = delegate; 63 this.domain = domain; 64 this.manager = new ClasspathManager(bundledata, classpath, this); 65 } 66 67 78 protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { 79 if (Debug.DEBUG && Debug.DEBUG_LOADER) 80 Debug.println("BundleClassLoader[" + delegate + "].loadClass(" + name + ")"); try { 82 Class clazz = delegate.findClass(name); 84 if (resolve) 86 resolveClass(clazz); 87 return (clazz); 88 } catch (Error e) { 89 if (Debug.DEBUG && Debug.DEBUG_LOADER) { 90 Debug.println("BundleClassLoader[" + delegate + "].loadClass(" + name + ") failed."); Debug.printStackTrace(e); 92 } 93 throw e; 94 } catch (ClassNotFoundException e) { 95 if (Debug.DEBUG && Debug.DEBUG_LOADER) { 98 Debug.println("BundleClassLoader[" + delegate + "].loadClass(" + name + ") failed."); Debug.printStackTrace(e); 100 } 101 throw e; 102 } 103 } 104 105 114 public URL getResource(String name) { 115 if (Debug.DEBUG && Debug.DEBUG_LOADER) { 116 Debug.println("BundleClassLoader[" + delegate + "].getResource(" + name + ")"); } 118 119 URL url = delegate.findResource(name); 120 if (url != null) 121 return (url); 122 123 if (Debug.DEBUG && Debug.DEBUG_LOADER) { 124 Debug.println("BundleClassLoader[" + delegate + "].getResource(" + name + ") failed."); } 126 127 return (null); 128 } 129 130 137 protected Enumeration findResources(String name) throws IOException { 138 return (delegate.findResources(name)); 139 } 140 141 147 protected String findLibrary(String libname) { 148 return manager.findLibrary(libname); 150 } 151 152 public ProtectionDomain getDomain() { 153 return domain; 154 } 155 156 public ClasspathEntry createClassPathEntry(BundleFile bundlefile, ProtectionDomain cpDomain) { 157 return new ClasspathEntry(bundlefile, createProtectionDomain(bundlefile, cpDomain)); 158 } 159 160 public Class defineClass(String name, byte[] classbytes, ClasspathEntry classpathEntry, BundleEntry entry) { 161 return defineClass(name, classbytes, 0, classbytes.length, classpathEntry.getDomain()); 162 } 163 164 public Class publicFindLoaded(String classname) { 165 return findLoadedClass(classname); 166 } 167 168 public Object publicGetPackage(String pkgname) { 169 return getPackage(pkgname); 170 } 171 172 public Object publicDefinePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) { 173 return definePackage(name, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor, sealBase); 174 } 175 176 public void initialize() { 177 manager.initialize(); 178 } 179 180 public URL findLocalResource(String resource) { 181 return manager.findLocalResource(resource); 182 } 183 184 public Enumeration findLocalResources(String resource) { 185 return manager.findLocalResources(resource); 186 } 187 188 public Class findLocalClass(String classname) throws ClassNotFoundException { 189 return manager.findLocalClass(classname); 190 } 191 192 public void close() { 193 manager.close(); 194 } 195 196 public void attachFragment(BundleData sourcedata, ProtectionDomain sourcedomain, String [] sourceclasspath) { 197 manager.attachFragment(sourcedata, sourcedomain, sourceclasspath); 198 } 199 200 public ClassLoaderDelegate getDelegate() { 201 return delegate; 202 } 203 204 210 public static ProtectionDomain createProtectionDomain(BundleFile bundlefile, ProtectionDomain baseDomain) { 211 try { 213 PermissionCollection permissions; 215 if (baseDomain != null) 216 permissions = baseDomain.getPermissions(); 217 else 218 permissions = ALLPERMISSIONS; 221 Certificate [] certs = null; 222 if (bundlefile instanceof CertificateVerifier) { 223 CertificateChain[] chains = ((CertificateVerifier) bundlefile).getChains(); 224 certs = chains == null || chains.length == 0 ? null : chains[0].getCertificates(); 225 } 226 return new ProtectionDomain(new CodeSource(bundlefile.getBaseFile().toURL(), certs), permissions); 227 } catch (MalformedURLException e) { 228 return baseDomain; 230 } 231 } 232 233 public ClasspathManager getClasspathManager() { 234 return manager; 235 } 236 } 237 | Popular Tags |