Search in sources :

Example 1 with KeyedElement

use of org.eclipse.osgi.framework.util.KeyedElement 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)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 ModuleWire (org.eclipse.osgi.container.ModuleWire)1 KeyedElement (org.eclipse.osgi.framework.util.KeyedElement)1 KeyedHashSet (org.eclipse.osgi.framework.util.KeyedHashSet)1 NullPackageSource (org.eclipse.osgi.internal.loader.sources.NullPackageSource)1 PackageSource (org.eclipse.osgi.internal.loader.sources.PackageSource)1