Search in sources :

Example 1 with ResourceNotFoundException

use of org.apache.felix.framework.resolver.ResourceNotFoundException in project felix by apache.

the class BundleWiringImpl method findClassOrResourceByDelegation.

private Object findClassOrResourceByDelegation(String name, boolean isClass) throws ClassNotFoundException, ResourceNotFoundException {
    Object result = null;
    Set requestSet = (Set) m_cycleCheck.get();
    if (requestSet == null) {
        requestSet = new HashSet();
        m_cycleCheck.set(requestSet);
    }
    if (requestSet.add(name)) {
        try {
            // Get the package of the target class/resource.
            String pkgName = (isClass) ? Util.getClassPackage(name) : Util.getResourcePackage(name);
            boolean accessor = name.startsWith("sun.reflect.Generated") || name.startsWith("jdk.internal.reflect.");
            if (accessor) {
                if (m_accessorLookupCache == null) {
                    m_accessorLookupCache = new ConcurrentHashMap<String, ClassLoader>();
                }
                ClassLoader loader = m_accessorLookupCache.get(name);
                if (loader != null) {
                    return loader.loadClass(name);
                }
            }
            // property to the parent class loader.
            if (shouldBootDelegate(pkgName)) {
                try {
                    // Get the appropriate class loader for delegation.
                    ClassLoader bdcl = getBootDelegationClassLoader();
                    result = (isClass) ? (Object) bdcl.loadClass(name) : (Object) bdcl.getResource(name);
                    // search; otherwise, continue to look locally if not found.
                    if (pkgName.startsWith("java.") || (result != null)) {
                        if (accessor) {
                            m_accessorLookupCache.put(name, bdcl);
                        }
                        return result;
                    }
                } catch (ClassNotFoundException ex) {
                    // search; otherwise, continue to look locally if not found.
                    if (pkgName.startsWith("java.")) {
                        throw ex;
                    }
                }
            }
            if (accessor) {
                List<Collection<BundleRevision>> allRevisions = new ArrayList<Collection<BundleRevision>>(1 + m_requiredPkgs.size());
                allRevisions.add(m_importedPkgs.values());
                allRevisions.addAll(m_requiredPkgs.values());
                for (Collection<BundleRevision> revisions : allRevisions) {
                    for (BundleRevision revision : revisions) {
                        ClassLoader loader = revision.getWiring().getClassLoader();
                        if (loader != null && loader instanceof BundleClassLoader) {
                            BundleClassLoader bundleClassLoader = (BundleClassLoader) loader;
                            result = bundleClassLoader.findLoadedClassInternal(name);
                            if (result != null) {
                                m_accessorLookupCache.put(name, bundleClassLoader);
                                return result;
                            }
                        }
                    }
                }
                try {
                    result = tryImplicitBootDelegation(name, isClass);
                } catch (Exception ex) {
                // Ignore, will throw using CNFE_CLASS_LOADER
                }
                if (result != null) {
                    m_accessorLookupCache.put(name, BundleRevisionImpl.getSecureAction().getClassLoader(this.getClass()));
                    return result;
                } else {
                    m_accessorLookupCache.put(name, CNFE_CLASS_LOADER);
                    CNFE_CLASS_LOADER.loadClass(name);
                }
            }
            // Look in the revision's imports. Note that the search may
            // be aborted if this method throws an exception, otherwise
            // it continues if a null is returned.
            result = searchImports(pkgName, name, isClass);
            // If not found, try the revision's own class path.
            if (result == null) {
                if (isClass) {
                    ClassLoader cl = getClassLoaderInternal();
                    if (cl == null) {
                        throw new ClassNotFoundException("Unable to load class '" + name + "' because the bundle wiring for " + m_revision.getSymbolicName() + " is no longer valid.");
                    }
                    result = ((BundleClassLoader) cl).findClass(name);
                } else {
                    result = m_revision.getResourceLocal(name);
                }
                // If still not found, then try the revision's dynamic imports.
                if (result == null) {
                    result = searchDynamicImports(pkgName, name, isClass);
                }
            }
        } finally {
            requestSet.remove(name);
        }
    } else {
        // loading code and not to the actual instigator of the class load.
        return null;
    }
    if (result == null) {
        if (isClass) {
            throw new ClassNotFoundException(name + " not found by " + this.getBundle());
        } else {
            throw new ResourceNotFoundException(name + " not found by " + this.getBundle());
        }
    }
    return result;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) BundleException(org.osgi.framework.BundleException) ResolutionException(org.osgi.service.resolver.ResolutionException) ResourceNotFoundException(org.apache.felix.framework.resolver.ResourceNotFoundException) WeavingException(org.osgi.framework.hooks.weaving.WeavingException) PrivilegedActionException(java.security.PrivilegedActionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BundleRevision(org.osgi.framework.wiring.BundleRevision) SecureClassLoader(java.security.SecureClassLoader) Collection(java.util.Collection) ResourceNotFoundException(org.apache.felix.framework.resolver.ResourceNotFoundException) HashSet(java.util.HashSet)

Example 2 with ResourceNotFoundException

use of org.apache.felix.framework.resolver.ResourceNotFoundException in project felix by apache.

the class BundleWiringImpl method searchImports.

private Object searchImports(String pkgName, String name, boolean isClass) throws ClassNotFoundException, ResourceNotFoundException {
    // Check if the package is imported.
    BundleRevision provider = m_importedPkgs.get(pkgName);
    if (provider != null) {
        // If we find the class or resource, then return it.
        Object result = (isClass) ? (Object) ((BundleWiringImpl) provider.getWiring()).getClassByDelegation(name) : (Object) ((BundleWiringImpl) provider.getWiring()).getResourceByDelegation(name);
        if (result != null) {
            return result;
        }
        // requested class and imported packages are atomic.
        if (isClass) {
            throw new ClassNotFoundException(name);
        }
        throw new ResourceNotFoundException(name);
    }
    // Check if the package is required.
    List<BundleRevision> providers = m_requiredPkgs.get(pkgName);
    if (providers != null) {
        for (BundleRevision p : providers) {
            // If we find the class or resource, then return it.
            try {
                Object result = (isClass) ? (Object) ((BundleWiringImpl) p.getWiring()).getClassByDelegation(name) : (Object) ((BundleWiringImpl) p.getWiring()).getResourceByDelegation(name);
                if (result != null) {
                    return result;
                }
            } catch (ClassNotFoundException ex) {
            // Since required packages can be split, don't throw an
            // exception here if it is not found. Instead, we'll just
            // continue searching other required bundles and the
            // revision's local content.
            }
        }
    }
    return null;
}
Also used : BundleRevision(org.osgi.framework.wiring.BundleRevision) ResourceNotFoundException(org.apache.felix.framework.resolver.ResourceNotFoundException)

Aggregations

ResourceNotFoundException (org.apache.felix.framework.resolver.ResourceNotFoundException)2 BundleRevision (org.osgi.framework.wiring.BundleRevision)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 SecureClassLoader (java.security.SecureClassLoader)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 BundleException (org.osgi.framework.BundleException)1 WeavingException (org.osgi.framework.hooks.weaving.WeavingException)1 ResolutionException (org.osgi.service.resolver.ResolutionException)1