Search in sources :

Example 1 with PackageSource

use of org.eclipse.osgi.internal.loader.sources.PackageSource in project rt.equinox.framework by eclipse.

the class BundleLoader method getImportedSources.

private KeyedHashSet getImportedSources(Collection<BundleLoader> visited) {
    synchronized (importedSources) {
        if (importsInitialized) {
            return importedSources;
        }
        List<ModuleWire> importWires = wiring.getRequiredModuleWires(PackageNamespace.PACKAGE_NAMESPACE);
        if (importWires != null) {
            for (ModuleWire importWire : importWires) {
                PackageSource source = createExportPackageSource(importWire, visited);
                if (source != null) {
                    importedSources.add(source);
                }
            }
        }
        importsInitialized = true;
        return importedSources;
    }
}
Also used : ModuleWire(org.eclipse.osgi.container.ModuleWire) PackageSource(org.eclipse.osgi.internal.loader.sources.PackageSource) NullPackageSource(org.eclipse.osgi.internal.loader.sources.NullPackageSource)

Example 2 with PackageSource

use of org.eclipse.osgi.internal.loader.sources.PackageSource in project rt.equinox.framework by eclipse.

the class BundleLoader method findRequiredSource.

private PackageSource findRequiredSource(String pkgName, Collection<BundleLoader> visited) {
    synchronized (requiredSources) {
        PackageSource result = (PackageSource) requiredSources.getByKey(pkgName);
        if (result != null)
            return result.isNullSource() ? null : result;
    }
    if (visited == null)
        visited = new ArrayList<>();
    if (!visited.contains(this))
        // always add ourselves so we do not recurse back to ourselves
        visited.add(this);
    List<PackageSource> result = new ArrayList<>(3);
    for (ModuleWire bundleWire : requiredBundleWires) {
        BundleLoader loader = (BundleLoader) bundleWire.getProviderWiring().getModuleLoader();
        if (loader != null) {
            loader.addExportedProvidersFor(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 = result.get(0);
    } else {
        // if there was more than one source, build a multisource and cache that.
        PackageSource[] srcs = result.toArray(new PackageSource[result.size()]);
        source = createMultiSource(pkgName, srcs);
    }
    synchronized (requiredSources) {
        requiredSources.add(source);
    }
    return source.isNullSource() ? null : source;
}
Also used : ModuleWire(org.eclipse.osgi.container.ModuleWire) PackageSource(org.eclipse.osgi.internal.loader.sources.PackageSource) NullPackageSource(org.eclipse.osgi.internal.loader.sources.NullPackageSource) ArrayList(java.util.ArrayList)

Example 3 with PackageSource

use of org.eclipse.osgi.internal.loader.sources.PackageSource in project rt.equinox.framework by eclipse.

the class BundleLoader method findClassInternal.

private Class<?> findClassInternal(String name, boolean checkParent) throws ClassNotFoundException {
    if (debug.DEBUG_LOADER)
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        Debug.println("BundleLoader[" + this + "].findClassInternal(" + name + ")");
    String pkgName = getPackageName(name);
    boolean bootDelegation = false;
    // follow the OSGi delegation model
    if (checkParent && parent != null && container.isBootDelegationPackage(pkgName)) {
        // 2) if part of the bootdelegation list then delegate to parent and continue of failure
        try {
            return parent.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) {
        if (debug.DEBUG_LOADER) {
            // $NON-NLS-1$ //$NON-NLS-2$
            Debug.println("BundleLoader[" + this + "] loading from import package: " + source);
        }
        // 3) found import source terminate search at the source
        result = source.loadClass(name);
        if (result != null)
            return result;
        // $NON-NLS-1$
        throw new ClassNotFoundException(name + " cannot be found by " + this);
    }
    // 4) search the required bundles
    source = findRequiredSource(pkgName, null);
    if (source != null) {
        if (debug.DEBUG_LOADER) {
            // $NON-NLS-1$ //$NON-NLS-2$
            Debug.println("BundleLoader[" + this + "] loading from required bundle package: " + source);
        }
        // 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;
            // $NON-NLS-1$
            throw new ClassNotFoundException(name + " cannot be found by " + this);
        }
    }
    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;
    // or last resort; do class context trick to work around VM bugs
    if (parent != null && !bootDelegation && ((checkParent && container.getConfiguration().compatibilityBootDelegation) || isRequestFromVM())) {
        // we don't need to continue if a CNFE is thrown here.
        try {
            return parent.loadClass(name);
        } catch (ClassNotFoundException e) {
        // we want to generate our own exception below
        }
    }
    // $NON-NLS-1$
    throw new ClassNotFoundException(name + " cannot be found by " + this);
}
Also used : PackageSource(org.eclipse.osgi.internal.loader.sources.PackageSource) NullPackageSource(org.eclipse.osgi.internal.loader.sources.NullPackageSource) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with PackageSource

use of org.eclipse.osgi.internal.loader.sources.PackageSource in project rt.equinox.framework by eclipse.

the class BundleLoader method listResources.

@Override
protected Collection<String> listResources(String path, String filePattern, int options) {
    // $NON-NLS-1$
    String pkgName = getResourcePackageName(path.endsWith("/") ? path : path + '/');
    if ((path.length() > 1) && (path.charAt(0) == '/'))
        /* if name has a leading slash */
        path = path.substring(1);
    /* remove leading slash before search */
    boolean subPackages = (options & BundleWiring.LISTRESOURCES_RECURSE) != 0;
    List<String> packages = new ArrayList<>();
    // search imported package names
    KeyedHashSet importSources = getImportedSources(null);
    KeyedElement[] imports;
    synchronized (importSources) {
        imports = importSources.elements();
    }
    for (KeyedElement keyedElement : imports) {
        String id = ((PackageSource) keyedElement).getId();
        if (id.equals(pkgName) || (subPackages && isSubPackage(pkgName, id)))
            packages.add(id);
    }
    // now add package names from required bundles
    Collection<BundleLoader> visited = new ArrayList<>();
    // always add ourselves so we do not recurse back to ourselves
    visited.add(this);
    for (ModuleWire bundleWire : requiredBundleWires) {
        BundleLoader loader = (BundleLoader) bundleWire.getProviderWiring().getModuleLoader();
        if (loader != null) {
            loader.addProvidedPackageNames(pkgName, packages, subPackages, visited);
        }
    }
    boolean localSearch = (options & BundleWiring.LISTRESOURCES_LOCAL) != 0;
    // Use LinkedHashSet for optimized performance of contains() plus
    // ordering guarantees.
    LinkedHashSet<String> result = new LinkedHashSet<>();
    Set<String> importedPackages = new HashSet<>(0);
    for (String name : packages) {
        // look for import source
        PackageSource externalSource = findImportedSource(name, null);
        if (externalSource != null) {
            // record this package is imported
            importedPackages.add(name);
        } else {
            // look for require bundle source
            externalSource = findRequiredSource(name, null);
        }
        // only add the content of the external source if this is not a localSearch
        if (externalSource != null && !localSearch) {
            String packagePath = name.replace('.', '/');
            Collection<String> externalResources = externalSource.listResources(packagePath, filePattern);
            for (String resource : externalResources) {
                if (// prevent duplicates; could happen if the package is split or exporter has fragments/multiple jars
                !result.contains(resource))
                    result.add(resource);
            }
        }
    }
    // now search locally
    Collection<String> localResources = getModuleClassLoader().listLocalResources(path, filePattern, options);
    for (String resource : localResources) {
        String resourcePkg = getResourcePackageName(resource);
        if (!importedPackages.contains(resourcePkg) && !result.contains(resource))
            result.add(resource);
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ModuleWire(org.eclipse.osgi.container.ModuleWire) ArrayList(java.util.ArrayList) KeyedHashSet(org.eclipse.osgi.framework.util.KeyedHashSet) KeyedElement(org.eclipse.osgi.framework.util.KeyedElement) PackageSource(org.eclipse.osgi.internal.loader.sources.PackageSource) NullPackageSource(org.eclipse.osgi.internal.loader.sources.NullPackageSource) KeyedHashSet(org.eclipse.osgi.framework.util.KeyedHashSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with PackageSource

use of org.eclipse.osgi.internal.loader.sources.PackageSource in project rt.equinox.framework by eclipse.

the class BundleLoader method findDynamicSource.

private PackageSource findDynamicSource(String pkgName) {
    if (!isExportedPackage(pkgName) && isDynamicallyImported(pkgName)) {
        if (debug.DEBUG_LOADER) {
            // $NON-NLS-1$ //$NON-NLS-2$
            Debug.println("BundleLoader[" + this + "] attempting to resolve dynamic package: " + pkgName);
        }
        ModuleRevision revision = wiring.getRevision();
        ModuleWire dynamicWire = revision.getRevisions().getModule().getContainer().resolveDynamic(pkgName, revision);
        if (dynamicWire != null) {
            PackageSource source = createExportPackageSource(dynamicWire, null);
            if (debug.DEBUG_LOADER) {
                // $NON-NLS-1$ //$NON-NLS-2$
                Debug.println("BundleLoader[" + this + "] using dynamic import source: " + source);
            }
            synchronized (importedSources) {
                importedSources.add(source);
            }
            return source;
        }
    }
    return null;
}
Also used : ModuleWire(org.eclipse.osgi.container.ModuleWire) PackageSource(org.eclipse.osgi.internal.loader.sources.PackageSource) NullPackageSource(org.eclipse.osgi.internal.loader.sources.NullPackageSource) ModuleRevision(org.eclipse.osgi.container.ModuleRevision)

Aggregations

NullPackageSource (org.eclipse.osgi.internal.loader.sources.NullPackageSource)10 PackageSource (org.eclipse.osgi.internal.loader.sources.PackageSource)10 ModuleWire (org.eclipse.osgi.container.ModuleWire)5 FileNotFoundException (java.io.FileNotFoundException)3 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 Enumeration (java.util.Enumeration)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 ModuleRevision (org.eclipse.osgi.container.ModuleRevision)1 KeyedElement (org.eclipse.osgi.framework.util.KeyedElement)1 KeyedHashSet (org.eclipse.osgi.framework.util.KeyedHashSet)1