final void addExportedProvidersFor( String symbolicName, String packageName, ArrayList result, KeyedHashSet visited) { if (!visited.add(bundle)) return; // See if we locally provide the package. PackageSource local = null; if (isExportedPackage(packageName)) local = proxy.getPackageSource(packageName); else if (isSubstitutedExport(packageName)) { result.add(findImportedSource(packageName, visited)); return; // should not continue to required bundles in this case } // Must search required bundles that are exported first. if (requiredBundles != null) { int size = reexportTable == null ? 0 : reexportTable.length; int reexportIndex = 0; for (int i = 0; i < requiredBundles.length; i++) { if (local != null) { // always add required bundles first if we locally provide the package // This allows a bundle to provide a package from a required bundle without // re-exporting the whole required bundle. requiredBundles[i] .getBundleLoader() .addExportedProvidersFor(symbolicName, packageName, result, visited); } else if (reexportIndex < size && reexportTable[reexportIndex] == i) { reexportIndex++; requiredBundles[i] .getBundleLoader() .addExportedProvidersFor(symbolicName, packageName, result, visited); } } } // now add the locally provided package. if (local != null && local.isFriend(symbolicName)) result.add(local); }
private PackageSource findRequiredSource(String pkgName, KeyedHashSet visited) { if (requiredBundles == null) return null; synchronized (requiredSources) { PackageSource result = (PackageSource) requiredSources.getByKey(pkgName); if (result != null) return result.isNullSource() ? null : result; } if (visited == null) visited = new KeyedHashSet(false); visited.add(bundle); // always add ourselves so we do not recurse back to ourselves ArrayList result = new ArrayList(3); for (int i = 0; i < requiredBundles.length; i++) { BundleLoader requiredLoader = requiredBundles[i].getBundleLoader(); requiredLoader.addExportedProvidersFor(proxy.getSymbolicName(), pkgName, result, visited); } // found some so cache the result for next time and return PackageSource source; if (result.size() == 0) { // did not find it in our required bundles lets record the failure // so we do not have to do the search again for this package. source = NullPackageSource.getNullPackageSource(pkgName); } else if (result.size() == 1) { // if there is just one source, remember just the single source source = (PackageSource) result.get(0); } else { // if there was more than one source, build a multisource and cache that. PackageSource[] srcs = (PackageSource[]) result.toArray(new PackageSource[result.size()]); source = createMultiSource(pkgName, srcs); } synchronized (requiredSources) { requiredSources.add(source); } return source.isNullSource() ? null : source; }
/** * Finds the resources for a bundle. This method is used for delegation by the bundle's * classloader. */ public Enumeration findResources(String name) throws IOException { // do not delegate to parent because ClassLoader#getResources already did and it is final!! 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; try { result = (Enumeration) searchHooks(name, PRE_RESOURCES); } catch (ClassNotFoundException e) { // will not happen } catch (FileNotFoundException e) { return null; } if (result != null) return result; // start at step 3 because of the comment above about ClassLoader#getResources // 3) search the imported packages PackageSource source = findImportedSource(pkgName, null); if (source != null) // 3) found import source terminate search at the source return source.getResources(name); // 4) search the required bundles source = findRequiredSource(pkgName, null); if (source != null) // 4) attempt to load from source but continue on failure result = source.getResources(name); // 5) search the local bundle // compound the required source results with the local ones Enumeration localResults = findLocalResources(name); result = compoundEnumerations(result, localResults); // 6) attempt to find a dynamic import source; only do this if a required source was not found if (result == null && source == null) { source = findDynamicSource(pkgName); if (source != null) return source.getResources(name); } if (result == null) try { result = (Enumeration) searchHooks(name, POST_RESOURCES); } catch (ClassNotFoundException e) { // will not happen } catch (FileNotFoundException e) { return null; } if (policy != null) { Enumeration buddyResult = policy.doBuddyResourcesLoading(name); result = compoundEnumerations(result, buddyResult); } return result; }
URL findResource(String name, boolean checkParent) { 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); boolean bootDelegation = false; ClassLoader parentCL = getParentClassLoader(); // follow the OSGi delegation model // First check the parent classloader for system resources, if it is a java resource. if (checkParent && parentCL != null) { if (pkgName.startsWith(JAVA_PACKAGE)) // 1) if startsWith "java." delegate to parent and terminate search // we never delegate java resource requests past the parent return parentCL.getResource(name); else if (bundle.getFramework().isBootDelegationPackage(pkgName)) { // 2) if part of the bootdelegation list then delegate to parent and continue of failure URL result = parentCL.getResource(name); if (result != null) return result; bootDelegation = true; } } URL result = null; try { result = (URL) searchHooks(name, PRE_RESOURCE); } catch (FileNotFoundException e) { return null; } catch (ClassNotFoundException e) { // will not happen } if (result != null) return result; // 3) search the imported packages PackageSource source = findImportedSource(pkgName, null); if (source != null) // 3) found import source terminate search at the source return source.getResource(name); // 4) search the required bundles source = findRequiredSource(pkgName, null); if (source != null) // 4) attempt to load from source but continue on failure result = source.getResource(name); // 5) search the local bundle if (result == null) result = findLocalResource(name); if (result != null) return result; // 6) attempt to find a dynamic import source; only do this if a required source was not found if (source == null) { source = findDynamicSource(pkgName); if (source != null) // must return the result of the dynamic import and do not continue return source.getResource(name); } if (result == null) try { result = (URL) searchHooks(name, POST_RESOURCE); } catch (FileNotFoundException e) { return null; } catch (ClassNotFoundException e) { // will not happen } // do buddy policy loading if (result == null && policy != null) result = policy.doBuddyResourceLoading(name); if (result != null) return result; // hack to support backwards compatibiility for bootdelegation // or last resort; do class context trick to work around VM bugs if (parentCL != null && !bootDelegation && ((checkParent && bundle.getFramework().compatibiltyBootDelegation) || isRequestFromVM())) // we don't need to continue if the resource is not found here return parentCL.getResource(name); return result; }
private Class findClassInternal(String name, boolean checkParent, ClassLoader parentCL) throws ClassNotFoundException { if (Debug.DEBUG && Debug.DEBUG_LOADER) Debug.println( "BundleLoader[" + this + "].loadBundleClass(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ String pkgName = getPackageName(name); boolean bootDelegation = false; // follow the OSGi delegation model if (checkParent && parentCL != null && bundle.getFramework().isBootDelegationPackage(pkgName)) // 2) if part of the bootdelegation list then delegate to parent and continue of failure try { return parentCL.loadClass(name); } catch (ClassNotFoundException cnfe) { // we want to continue bootDelegation = true; } Class result = null; try { result = (Class) searchHooks(name, PRE_CLASS); } catch (ClassNotFoundException e) { throw e; } catch (FileNotFoundException e) { // will not happen } if (result != null) return result; // 3) search the imported packages PackageSource source = findImportedSource(pkgName, null); if (source != null) { // 3) found import source terminate search at the source result = source.loadClass(name); if (result != null) return result; throw new ClassNotFoundException(name); } // 4) search the required bundles source = findRequiredSource(pkgName, null); if (source != null) // 4) attempt to load from source but continue on failure result = source.loadClass(name); // 5) search the local bundle if (result == null) result = findLocalClass(name); if (result != null) return result; // 6) attempt to find a dynamic import source; only do this if a required source was not found if (source == null) { source = findDynamicSource(pkgName); if (source != null) { result = source.loadClass(name); if (result != null) return result; // must throw CNFE if dynamic import source does not have the class throw new ClassNotFoundException(name); } } if (result == null) try { result = (Class) searchHooks(name, POST_CLASS); } catch (ClassNotFoundException e) { throw e; } catch (FileNotFoundException e) { // will not happen } // do buddy policy loading if (result == null && policy != null) result = policy.doBuddyClassLoading(name); if (result != null) return result; // hack to support backwards compatibiility for bootdelegation // or last resort; do class context trick to work around VM bugs if (parentCL != null && !bootDelegation && ((checkParent && bundle.getFramework().compatibiltyBootDelegation) || isRequestFromVM())) // we don't need to continue if a CNFE is thrown here. try { return parentCL.loadClass(name); } catch (ClassNotFoundException e) { // we want to generate our own exception below } throw new ClassNotFoundException(name); }