diff options
3 files changed, 52 insertions, 60 deletions
diff --git a/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/core/BundleLoader.java b/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/core/BundleLoader.java index fdfba85b5..dd0a8254f 100644 --- a/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/core/BundleLoader.java +++ b/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/core/BundleLoader.java @@ -31,6 +31,8 @@ import org.osgi.framework.*; */ public class BundleLoader implements ClassLoaderDelegate { public final static String DEFAULT_PACKAGE = "."; //$NON-NLS-1$ + public final static String JAVA_CLASS = "java."; //$NON-NLS-1$ + public final static String JAVA_RESOURCE = "java/"; //$NON-NLS-1$ /* the proxy */ BundleLoaderProxy proxy; @@ -38,6 +40,7 @@ public class BundleLoader implements ClassLoaderDelegate { BundleHost bundle; /* The is the BundleClassLoader for the bundle */ BundleClassLoader classloader; + ClassLoader parent; /* cache of imported packages. Key is packagename, Value is PackageSource */ KeyedHashSet importedSources; @@ -284,7 +287,9 @@ public class BundleLoader implements ClassLoaderDelegate { try { String[] classpath = bundle.getBundleData().getClassPath(); if (classpath != null) { - classloader = createBCLPrevileged(bundle.getProtectionDomain(), classpath); + BundleClassLoader bcl = createBCLPrevileged(bundle.getProtectionDomain(), classpath); + parent = getParentPrivileged((ClassLoader) bcl); + classloader = bcl; } else { bundle.framework.publishFrameworkEvent(FrameworkEvent.ERROR, bundle, new BundleException(Msg.BUNDLE_NO_CLASSPATH_MATCH)); //$NON-NLS-1$ } @@ -304,8 +309,16 @@ public class BundleLoader implements ClassLoaderDelegate { Class findLocalClass(String name) { if (Debug.DEBUG && Debug.DEBUG_LOADER) Debug.println("BundleLoader[" + this + "].findLocalClass(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + BundleClassLoader bcl = createClassLoader(); + // TODO add flag to check system "buddy" + if (parent != null) + try { + return parent.loadClass(name); + } catch (ClassNotFoundException e) { + // do nothing and continue + } try { - Class clazz = createClassLoader().findLocalClass(name); + Class clazz = bcl.findLocalClass(name); if (Debug.DEBUG && Debug.DEBUG_LOADER && clazz != null) Debug.println("BundleLoader[" + this + "] found local class " + name); //$NON-NLS-1$ //$NON-NLS-2$ return clazz; @@ -322,6 +335,12 @@ public class BundleLoader implements ClassLoaderDelegate { throw new ClassNotFoundException(name); if (Debug.DEBUG && Debug.DEBUG_LOADER) Debug.println("BundleLoader[" + this + "].loadBundleClass(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + if (name.startsWith(JAVA_CLASS)) { + // First check the parent classloader for system classes. + if (parent != null) + // we want to throw ClassNotFoundExceptions if a java.* class cannot be loaded from the parent. + return parent.loadClass(name); + } String pkgName = getPackageName(name); Class result = null; PackageSource source = findImportedSource(pkgName); @@ -351,6 +370,12 @@ public class BundleLoader implements ClassLoaderDelegate { return null; if ((name.length() > 1) && (name.charAt(0) == '/')) /* if name has a leading slash */ name = name.substring(1); /* remove leading slash before search */ + if (name.startsWith(JAVA_RESOURCE)) { + // First check the parent classloader for system resources, if it is a java resource. + if (parent != null) + // we never delegate java resource requests past the parent + return parent.getResource(name); + } String pkgName = getResourcePackageName(name); URL result = null; PackageSource source = findImportedSource(pkgName); @@ -374,7 +399,6 @@ public class BundleLoader implements ClassLoaderDelegate { return null; if ((name.length() > 1) && (name.charAt(0) == '/')) /* if name has a leading slash */ name = name.substring(1); /* remove leading slash before search */ - String pkgName = getResourcePackageName(name); Enumeration result = null; PackageSource source = findImportedSource(pkgName); @@ -396,7 +420,14 @@ public class BundleLoader implements ClassLoaderDelegate { * @return The URL to the resource or null if the resource is not found. */ URL findLocalResource(final String name) { - return createClassLoader().findLocalResource(name); + BundleClassLoader bcl = createClassLoader(); + // TODO add flag to check system "buddy" + if (parent != null) { + URL url = parent.getResource(name); + if (url != null) + return url; + } + return bcl.findLocalResource(name); } /** @@ -482,7 +513,6 @@ public class BundleLoader implements ClassLoaderDelegate { // finish the initialization of the classloader. bcl.initialize(); - return bcl; } @@ -758,4 +788,15 @@ public class BundleLoader implements ClassLoaderDelegate { return result; return createMultiSource(pkgName, new PackageSource[] {result, localSource}); } + + private ClassLoader getParentPrivileged(final ClassLoader cl) { + if (System.getSecurityManager() == null) + return cl.getParent(); + + return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + return cl.getParent(); + } + }); + } } diff --git a/bundles/org.eclipse.osgi/defaultAdaptor/src/org/eclipse/osgi/framework/adaptor/core/AbstractClassLoader.java b/bundles/org.eclipse.osgi/defaultAdaptor/src/org/eclipse/osgi/framework/adaptor/core/AbstractClassLoader.java index d8d5d2810..78db2ba3c 100644 --- a/bundles/org.eclipse.osgi/defaultAdaptor/src/org/eclipse/osgi/framework/adaptor/core/AbstractClassLoader.java +++ b/bundles/org.eclipse.osgi/defaultAdaptor/src/org/eclipse/osgi/framework/adaptor/core/AbstractClassLoader.java @@ -23,10 +23,6 @@ import org.eclipse.osgi.framework.debug.Debug; * class lookups to a parent classloader and the to a ClassLoaderDelegate. */ public abstract class AbstractClassLoader extends ClassLoader implements BundleClassLoader { - - public static final String JAVA_CLASS = "java."; //$NON-NLS-1$ - public static final String JAVA_RESOURCE = "java/"; //$NON-NLS-1$ - /** * The delegate used to get classes and resources from. The delegate * must always be queried first before the local ClassLoader is searched for @@ -81,17 +77,8 @@ public abstract class AbstractClassLoader extends ClassLoader implements BundleC if (Debug.DEBUG && Debug.DEBUG_LOADER) Debug.println("BundleClassLoader[" + delegate + "].loadClass(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$ try { - Class clazz = null; - if (name.startsWith(JAVA_CLASS)) { - // First check the parent classloader for system classes. - ClassLoader parent = getParentPrivileged(); - if (parent != null) - // we want to throw ClassNotFoundExceptions if a java.* class cannot be loaded from the parent. - clazz = parent.loadClass(name); - } // Just ask the delegate. This could result in findLocalClass(name) being called. - if (clazz == null) - clazz = delegate.findClass(name); + Class clazz = delegate.findClass(name); // resolve the class if asked to. if (resolve) resolveClass(clazz); @@ -141,17 +128,7 @@ public abstract class AbstractClassLoader extends ClassLoader implements BundleC Debug.println("BundleClassLoader[" + delegate + "].getResource(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } - URL url = null; - if (name.startsWith(JAVA_RESOURCE)) { - // First check the parent classloader for system resources, if it is a java resource. - ClassLoader parent = getParentPrivileged(); - if (parent != null) - // we never delegate java resource requests past the parent - return parent.getResource(name); - } - if (url != null) - return (url); - url = delegate.findResource(name); + URL url = delegate.findResource(name); if (url != null) return (url); @@ -177,10 +154,6 @@ public abstract class AbstractClassLoader extends ClassLoader implements BundleC * @return An Enumeration of all resources found or null if the resource. */ protected Enumeration findResources(String name) { - if (name.startsWith(JAVA_RESOURCE)) - // if this is a java resource then we already searched the parent in getResources. - // return null because we do not want to delegate java resource requests - return null; try { return (delegate.findResources(name)); } catch (Exception e) { @@ -242,14 +215,4 @@ public abstract class AbstractClassLoader extends ClassLoader implements BundleC closed = true; } - protected ClassLoader getParentPrivileged() { - if (System.getSecurityManager() == null) - return getParent(); - - return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - return getParent(); - } - }); - } } diff --git a/bundles/org.eclipse.osgi/defaultAdaptor/src/org/eclipse/osgi/framework/internal/defaultadaptor/DefaultClassLoader.java b/bundles/org.eclipse.osgi/defaultAdaptor/src/org/eclipse/osgi/framework/internal/defaultadaptor/DefaultClassLoader.java index f48ce8412..7089cedbb 100644 --- a/bundles/org.eclipse.osgi/defaultAdaptor/src/org/eclipse/osgi/framework/internal/defaultadaptor/DefaultClassLoader.java +++ b/bundles/org.eclipse.osgi/defaultAdaptor/src/org/eclipse/osgi/framework/internal/defaultadaptor/DefaultClassLoader.java @@ -176,9 +176,8 @@ public class DefaultClassLoader extends AbstractClassLoader { for (int i = 0; i < classpathEntries.length; i++) { if (classpathEntries[i] != null) { result = findClassImpl(name, classpathEntries[i]); - if (result != null) { + if (result != null) return result; - } } } // look in fragments. @@ -188,16 +187,11 @@ public class DefaultClassLoader extends AbstractClassLoader { FragmentClasspath fragCP = (FragmentClasspath) fragClasspaths.elementAt(i); for (int j = 0; j < fragCP.classpathEntries.length; j++) { result = findClassImpl(name, fragCP.classpathEntries[j]); - if (result != null) { + if (result != null) return result; - } } } } - // Finally check the parent classloader for system classes. - ClassLoader parent = getParentPrivileged(); - if (parent != null) - return parent.loadClass(name); throw new ClassNotFoundException(name); } @@ -314,9 +308,8 @@ public class DefaultClassLoader extends AbstractClassLoader { for (int i = 0; i < classpathEntries.length; i++) { if (classpathEntries[i] != null) { result = findResourceImpl(name, classpathEntries[i].getBundleFile()); - if (result != null) { + if (result != null) return result; - } } } // look in fragments @@ -326,16 +319,11 @@ public class DefaultClassLoader extends AbstractClassLoader { FragmentClasspath fragCP = (FragmentClasspath) fragClasspaths.elementAt(i); for (int j = 0; j < fragCP.classpathEntries.length; j++) { result = findResourceImpl(name, fragCP.classpathEntries[j].getBundleFile()); - if (result != null) { + if (result != null) return result; - } } } } - // Finally check the parent classloader for system resources. - ClassLoader parent = getParentPrivileged(); - if (parent != null) - return parent.getResource(name); return null; } |