Search in sources :

Example 6 with ModuleWire

use of org.eclipse.osgi.container.ModuleWire 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 7 with ModuleWire

use of org.eclipse.osgi.container.ModuleWire 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)

Example 8 with ModuleWire

use of org.eclipse.osgi.container.ModuleWire in project rt.equinox.framework by eclipse.

the class BundleLoader method addExportedProvidersFor.

final void addExportedProvidersFor(String packageName, List<PackageSource> result, Collection<BundleLoader> visited) {
    if (visited.contains(this))
        return;
    visited.add(this);
    // See if we locally provide the package.
    PackageSource local = null;
    if (isExportedPackage(packageName))
        local = exportSources.getPackageSource(packageName);
    else if (isSubstitutedExport(packageName)) {
        result.add(findImportedSource(packageName, visited));
        // should not continue to required bundles in this case
        return;
    }
    // Must search required bundles that are exported first.
    for (ModuleWire bundleWire : requiredBundleWires) {
        if (local != null || BundleNamespace.VISIBILITY_REEXPORT.equals(bundleWire.getRequirement().getDirectives().get(BundleNamespace.REQUIREMENT_VISIBILITY_DIRECTIVE))) {
            // 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.
            BundleLoader loader = (BundleLoader) bundleWire.getProviderWiring().getModuleLoader();
            if (loader != null) {
                loader.addExportedProvidersFor(packageName, result, visited);
            }
        }
    }
    // now add the locally provided package.
    if (local != null)
        result.add(local);
}
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 9 with ModuleWire

use of org.eclipse.osgi.container.ModuleWire in project rt.equinox.framework by eclipse.

the class TestModuleContainer method testSubstitutableExport.

@Test
public void testSubstitutableExport() throws BundleException, IOException {
    DummyContainerAdaptor adaptor = createDummyAdaptor();
    ModuleContainer container = adaptor.getContainer();
    // install the system.bundle
    Module systemBundle = installDummyModule("system.bundle.MF", Constants.SYSTEM_BUNDLE_LOCATION, Constants.SYSTEM_BUNDLE_SYMBOLICNAME, null, null, container);
    ResolutionReport report = container.resolve(Arrays.asList(systemBundle), true);
    Assert.assertNull("Failed to resolve system.bundle.", report.getResolutionException());
    // install an exporter with substitutable export.
    Map<String, String> exporterManifest = new HashMap<String, String>();
    exporterManifest.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    exporterManifest.put(Constants.BUNDLE_SYMBOLICNAME, "exporter");
    exporterManifest.put(Constants.EXPORT_PACKAGE, "exporter");
    exporterManifest.put(Constants.IMPORT_PACKAGE, "exporter");
    Module moduleSubsExport = installDummyModule(exporterManifest, "exporter", container);
    report = container.resolve(Arrays.asList(moduleSubsExport), true);
    Assert.assertNull("Failed to resolve", report.getResolutionException());
    List<BundleRequirement> reqs = moduleSubsExport.getCurrentRevision().getWiring().getRequirements(PackageNamespace.PACKAGE_NAMESPACE);
    assertEquals("Wrong number of imports.", 0, reqs.size());
    container.uninstall(moduleSubsExport);
    exporterManifest = new HashMap<String, String>();
    exporterManifest.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    exporterManifest.put(Constants.BUNDLE_SYMBOLICNAME, "substitutableExporter");
    exporterManifest.put(Constants.EXPORT_PACKAGE, "exporter");
    exporterManifest.put(Constants.IMPORT_PACKAGE, "exporter; pickme=true");
    moduleSubsExport = installDummyModule(exporterManifest, "substitutableExporter", container);
    exporterManifest = new HashMap<String, String>();
    exporterManifest.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    exporterManifest.put(Constants.BUNDLE_SYMBOLICNAME, "exporter");
    exporterManifest.put(Constants.EXPORT_PACKAGE, "exporter; pickme=true");
    Module moduleExport = installDummyModule(exporterManifest, "exporter", container);
    report = container.resolve(Arrays.asList(moduleSubsExport), true);
    Assert.assertNull("Failed to resolve", report.getResolutionException());
    List<BundleCapability> caps = moduleSubsExport.getCurrentRevision().getWiring().getCapabilities(PackageNamespace.PACKAGE_NAMESPACE);
    assertEquals("Wrong number of capabilities.", 0, caps.size());
    reqs = moduleSubsExport.getCurrentRevision().getWiring().getRequirements(PackageNamespace.PACKAGE_NAMESPACE);
    assertEquals("Wrong number of imports.", 1, reqs.size());
    ModuleWiring wiring = moduleSubsExport.getCurrentRevision().getWiring();
    List<ModuleWire> packageWires = wiring.getRequiredModuleWires(PackageNamespace.PACKAGE_NAMESPACE);
    Assert.assertEquals("Unexpected number of wires", 1, packageWires.size());
    Assert.assertEquals("Wrong exporter", packageWires.get(0).getProviderWiring().getRevision(), moduleExport.getCurrentRevision());
}
Also used : ModuleWire(org.eclipse.osgi.container.ModuleWire) ModuleContainer(org.eclipse.osgi.container.ModuleContainer) HashMap(java.util.HashMap) ResolutionReport(org.eclipse.osgi.report.resolution.ResolutionReport) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) DummyContainerAdaptor(org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor) ModuleWiring(org.eclipse.osgi.container.ModuleWiring) Module(org.eclipse.osgi.container.Module) BundleCapability(org.osgi.framework.wiring.BundleCapability) Test(org.junit.Test)

Example 10 with ModuleWire

use of org.eclipse.osgi.container.ModuleWire in project rt.equinox.framework by eclipse.

the class TestModuleContainer method testDynamicImport05.

@Test
public void testDynamicImport05() throws BundleException, IOException {
    DummyContainerAdaptor adaptor = createDummyAdaptor();
    ModuleContainer container = adaptor.getContainer();
    Module systemBundle = installDummyModule("system.bundle.MF", Constants.SYSTEM_BUNDLE_LOCATION, null, null, "osgi.ee; osgi.ee=JavaSE; version:Version=\"1.5.0\"", container);
    container.resolve(Arrays.asList(systemBundle), true);
    Module c1 = installDummyModule("c1_v1.MF", "c1_v1", container);
    Module c4 = installDummyModule("c4_v1.MF", "c4_v1", container);
    Module dynamic3 = installDummyModule("dynamic3_v1.MF", "dynamic3_v1", container);
    Module dynamic3Frag = installDummyModule("dynamic3.frag_v1.MF", "dynamic3.frag_v1", container);
    container.resolve(Arrays.asList(c1, c4, dynamic3, dynamic3Frag), true);
    ModuleWire dynamicWire = container.resolveDynamic("c4.a", dynamic3.getCurrentRevision());
    Assert.assertNotNull("No dynamic wire found.", dynamicWire);
    Assert.assertEquals("Wrong package found.", "c4.a", dynamicWire.getCapability().getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
    Assert.assertEquals("Wrong provider for the wire found.", c4.getCurrentRevision(), dynamicWire.getProvider());
    dynamicWire = container.resolveDynamic("c4.b", dynamic3.getCurrentRevision());
    Assert.assertNotNull("No dynamic wire found.", dynamicWire);
    Assert.assertEquals("Wrong package found.", "c4.b", dynamicWire.getCapability().getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
    Assert.assertEquals("Wrong provider for the wire found.", c4.getCurrentRevision(), dynamicWire.getProvider());
}
Also used : DummyContainerAdaptor(org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor) ModuleWire(org.eclipse.osgi.container.ModuleWire) ModuleContainer(org.eclipse.osgi.container.ModuleContainer) Module(org.eclipse.osgi.container.Module) Test(org.junit.Test)

Aggregations

ModuleWire (org.eclipse.osgi.container.ModuleWire)45 Module (org.eclipse.osgi.container.Module)32 ModuleContainer (org.eclipse.osgi.container.ModuleContainer)31 Test (org.junit.Test)31 DummyContainerAdaptor (org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor)25 ModuleWiring (org.eclipse.osgi.container.ModuleWiring)21 ResolutionReport (org.eclipse.osgi.report.resolution.ResolutionReport)13 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)9 ModuleRevision (org.eclipse.osgi.container.ModuleRevision)9 NullPackageSource (org.eclipse.osgi.internal.loader.sources.NullPackageSource)5 PackageSource (org.eclipse.osgi.internal.loader.sources.PackageSource)5 HashSet (java.util.HashSet)4 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)4 ModuleCapability (org.eclipse.osgi.container.ModuleCapability)3 Generation (org.eclipse.osgi.storage.BundleInfo.Generation)3 DummyModuleDatabase (org.eclipse.osgi.tests.container.dummys.DummyModuleDatabase)3 BundleCapability (org.osgi.framework.wiring.BundleCapability)3 Bundle (org.osgi.framework.Bundle)2 BundleException (org.osgi.framework.BundleException)2