Search in sources :

Example 6 with ModuleCapability

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

the class TestModuleContainer method testCompatProvidePackage.

@Test
public void testCompatProvidePackage() throws BundleException, IOException {
    DummyContainerAdaptor adaptor = createDummyAdaptor();
    ModuleContainer container = adaptor.getContainer();
    Module b1 = installDummyModule("compatProvidePackage1.MF", "b1", container);
    List<ModuleCapability> packageCaps = b1.getCurrentRevision().getModuleCapabilities(PackageNamespace.PACKAGE_NAMESPACE);
    Assert.assertEquals("Wrong number of exports", 5, packageCaps.size());
    Assert.assertEquals("Wrong package name.", "foo", packageCaps.get(0).getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
    Assert.assertEquals("Wrong package name.", "faa", packageCaps.get(1).getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
    Assert.assertEquals("Wrong package name.", "bar", packageCaps.get(2).getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
    Assert.assertEquals("Wrong package name.", "baz", packageCaps.get(3).getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
    Assert.assertEquals("Wrong package name.", "biz", packageCaps.get(4).getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
}
Also used : DummyContainerAdaptor(org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor) ModuleContainer(org.eclipse.osgi.container.ModuleContainer) ModuleCapability(org.eclipse.osgi.container.ModuleCapability) Module(org.eclipse.osgi.container.Module) Test(org.junit.Test)

Example 7 with ModuleCapability

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

the class TestModuleContainer method testFragments01.

@Test
public void testFragments01() throws BundleException, IOException {
    DummyContainerAdaptor adaptor = createDummyAdaptor();
    ModuleContainer container = adaptor.getContainer();
    Module systemModule = installDummyModule("system.bundle.MF", Constants.SYSTEM_BUNDLE_LOCATION, container);
    Module c1 = installDummyModule("c1_v1.MF", "c1_v1", container);
    Module h2 = installDummyModule("h2_v1.MF", "h2_v1", container);
    Module f2 = installDummyModule("f2_v1.MF", "f2_v1", container);
    container.resolve(Arrays.asList(systemModule, c1, h2, f2), true);
    ModuleWiring wiring = h2.getCurrentRevision().getWiring();
    List<ModuleWire> requiredWires = wiring.getRequiredModuleWires(null);
    Assert.assertEquals("Wrong number of required wires.", 3, requiredWires.size());
    for (ModuleWire wire : requiredWires) {
        ModuleCapability capability = wire.getCapability();
        Assert.assertEquals("Wrong namespace.", PackageNamespace.PACKAGE_NAMESPACE, capability.getNamespace());
        Assert.assertEquals("Wrong requirer.", h2.getCurrentRevision(), wire.getRequirer());
        String pkgName = (String) capability.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE);
        Assert.assertNotNull("No package name.", pkgName);
        ModuleRevision expectedReqRevision;
        if (pkgName.equals("org.osgi.framework")) {
            expectedReqRevision = h2.getCurrentRevision();
        } else {
            expectedReqRevision = f2.getCurrentRevision();
        }
        Assert.assertEquals("Wrong requirement revision.", expectedReqRevision, wire.getRequirement().getRevision());
    }
}
Also used : DummyContainerAdaptor(org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor) ModuleWire(org.eclipse.osgi.container.ModuleWire) ModuleContainer(org.eclipse.osgi.container.ModuleContainer) ModuleWiring(org.eclipse.osgi.container.ModuleWiring) ModuleCapability(org.eclipse.osgi.container.ModuleCapability) Module(org.eclipse.osgi.container.Module) ModuleRevision(org.eclipse.osgi.container.ModuleRevision) Test(org.junit.Test)

Example 8 with ModuleCapability

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

the class TestModuleContainer method testSystemBundleFragmentRequiresOtherFragmentFailResolution.

@Test
public void testSystemBundleFragmentRequiresOtherFragmentFailResolution() throws BundleException, IOException {
    // install the system.bundle
    Module systemBundle = createContainerWithSystemBundle(true);
    ModuleContainer container = systemBundle.getContainer();
    // install an system.bundle fragment that provides a capability
    Map<String, String> systemFragManifest1 = new HashMap<String, String>();
    systemFragManifest1.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    systemFragManifest1.put(Constants.BUNDLE_SYMBOLICNAME, "systemFrag1");
    systemFragManifest1.put(Constants.FRAGMENT_HOST, Constants.SYSTEM_BUNDLE_SYMBOLICNAME);
    systemFragManifest1.put(Constants.PROVIDE_CAPABILITY, "fragment.capability; fragment.capability=test1");
    Module systemFrag1 = installDummyModule(systemFragManifest1, "systemFrag1", container);
    // install an system.bundle fragment that requires a fragment capability, but fails to match
    Map<String, String> systemFragManifest2 = new HashMap<String, String>();
    systemFragManifest2.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    systemFragManifest2.put(Constants.BUNDLE_SYMBOLICNAME, "systemFrag2");
    systemFragManifest2.put(Constants.FRAGMENT_HOST, Constants.SYSTEM_BUNDLE_SYMBOLICNAME);
    systemFragManifest2.put(Constants.REQUIRE_CAPABILITY, "fragment.capability; filter:=\"(fragment.capability=test4)\"");
    systemFragManifest2.put(Constants.PROVIDE_CAPABILITY, "fragment.capability; fragment.capability=test2");
    Module systemFrag2 = installDummyModule(systemFragManifest2, "systemFrag2", container);
    // install an system.bundle fragment that requires a fragment capability from a fragment that fails to resolve
    Map<String, String> systemFragManifest3 = new HashMap<String, String>();
    systemFragManifest3.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    systemFragManifest3.put(Constants.BUNDLE_SYMBOLICNAME, "systemFrag3");
    systemFragManifest3.put(Constants.FRAGMENT_HOST, Constants.SYSTEM_BUNDLE_SYMBOLICNAME);
    systemFragManifest3.put(Constants.REQUIRE_CAPABILITY, "fragment.capability; filter:=\"(fragment.capability=test2)\"");
    systemFragManifest3.put(Constants.PROVIDE_CAPABILITY, "fragment.capability; fragment.capability=test3");
    Module systemFrag3 = installDummyModule(systemFragManifest3, "systemFrag3", container);
    ResolutionReport report = container.resolve(Arrays.asList(systemFrag3), true);
    Assert.assertNotNull("Expected failure message", report.getResolutionException());
    List<ModuleWire> hostWires = systemBundle.getCurrentRevision().getWiring().getProvidedModuleWires(HostNamespace.HOST_NAMESPACE);
    assertEquals("Wrong number of fragments.", 1, hostWires.size());
    List<ModuleWire> systemFrag1HostWires = systemFrag1.getCurrentRevision().getWiring().getRequiredModuleWires(HostNamespace.HOST_NAMESPACE);
    assertWires(systemFrag1HostWires, hostWires);
    // install a bundle that can satisfy the failed requirement, but it should not be allowed since it is not a fragment
    Map<String, String> provideCapabilityManifest1 = new HashMap<String, String>();
    provideCapabilityManifest1.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    provideCapabilityManifest1.put(Constants.BUNDLE_SYMBOLICNAME, "provideCapabilityBundle1");
    provideCapabilityManifest1.put(Constants.PROVIDE_CAPABILITY, "fragment.capability; fragment.capability=test4");
    installDummyModule(provideCapabilityManifest1, "provideCapabilityBundle1", container);
    hostWires = systemBundle.getCurrentRevision().getWiring().getProvidedModuleWires(HostNamespace.HOST_NAMESPACE);
    assertEquals("Wrong number of fragments.", 1, hostWires.size());
    systemFrag1HostWires = systemFrag1.getCurrentRevision().getWiring().getRequiredModuleWires(HostNamespace.HOST_NAMESPACE);
    assertWires(systemFrag1HostWires, hostWires);
    // install a fragment that satisfies the failed requirement
    Map<String, String> systemFragManifest4 = new HashMap<String, String>();
    systemFragManifest4.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    systemFragManifest4.put(Constants.BUNDLE_SYMBOLICNAME, "systemFrag4");
    systemFragManifest4.put(Constants.FRAGMENT_HOST, Constants.SYSTEM_BUNDLE_SYMBOLICNAME);
    systemFragManifest4.put(Constants.PROVIDE_CAPABILITY, "fragment.capability; fragment.capability=test4");
    Module systemFrag4 = installDummyModule(systemFragManifest4, "systemFrag4", container);
    report = container.resolve(Arrays.asList(systemFrag3), true);
    Assert.assertNull("Failed to resolve.", report.getResolutionException());
    hostWires = systemBundle.getCurrentRevision().getWiring().getProvidedModuleWires(HostNamespace.HOST_NAMESPACE);
    assertEquals("Wrong number of fragments.", 4, hostWires.size());
    systemFrag1HostWires = systemFrag1.getCurrentRevision().getWiring().getRequiredModuleWires(HostNamespace.HOST_NAMESPACE);
    List<ModuleWire> systemFrag2HostWires = systemFrag2.getCurrentRevision().getWiring().getRequiredModuleWires(HostNamespace.HOST_NAMESPACE);
    List<ModuleWire> systemFrag3HostWires = systemFrag3.getCurrentRevision().getWiring().getRequiredModuleWires(HostNamespace.HOST_NAMESPACE);
    List<ModuleWire> systemFrag4HostWires = systemFrag4.getCurrentRevision().getWiring().getRequiredModuleWires(HostNamespace.HOST_NAMESPACE);
    assertWires(systemFrag1HostWires, hostWires);
    assertWires(systemFrag2HostWires, hostWires);
    assertWires(systemFrag3HostWires, hostWires);
    assertWires(systemFrag4HostWires, hostWires);
    List<ModuleCapability> fragmentCapabilities = systemBundle.getCurrentRevision().getWiring().getModuleCapabilities("fragment.capability");
    assertEquals("Wrong number of fragment capabilities.", 4, fragmentCapabilities.size());
    // Use set since the order of required and provided wires will be different
    Set<ModuleWire> hostRequiredFragmentCapWires = new HashSet<ModuleWire>(systemBundle.getCurrentRevision().getWiring().getRequiredModuleWires("fragment.capability"));
    Set<ModuleWire> hostProvidedFragmentCapWires = new HashSet<ModuleWire>(systemBundle.getCurrentRevision().getWiring().getProvidedModuleWires("fragment.capability"));
    assertEquals("Wrong number of wires.", 2, hostProvidedFragmentCapWires.size());
    assertEquals("Wrong wires found from host.", hostRequiredFragmentCapWires, hostProvidedFragmentCapWires);
}
Also used : ModuleWire(org.eclipse.osgi.container.ModuleWire) ModuleContainer(org.eclipse.osgi.container.ModuleContainer) HashMap(java.util.HashMap) ModuleCapability(org.eclipse.osgi.container.ModuleCapability) Module(org.eclipse.osgi.container.Module) ResolutionReport(org.eclipse.osgi.report.resolution.ResolutionReport) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with ModuleCapability

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

the class Capabilities method addCapabilities.

/**
 * Adds the {@link ModuleRevision#getModuleCapabilities(String) capabilities}
 * provided by the specified revision to this database.  These capabilities must
 * become available for lookup with the {@link #findCapabilities(Requirement)}
 * method.
 * @param revision the revision which has capabilities to add
 * @return a collection of package names added for the osgi.wiring.package namespace
 */
public Collection<String> addCapabilities(ModuleRevision revision) {
    Collection<String> packageNames = null;
    for (ModuleCapability capability : revision.getModuleCapabilities(null)) {
        NamespaceSet namespaceSet = namespaceSets.get(capability.getNamespace());
        if (namespaceSet == null) {
            namespaceSet = new NamespaceSet(capability.getNamespace());
            namespaceSets.put(capability.getNamespace(), namespaceSet);
        }
        namespaceSet.addCapability(capability);
        // This is used to clear the dynamic package miss caches.
        if (PackageNamespace.PACKAGE_NAMESPACE.equals(capability.getNamespace())) {
            Object packageName = capability.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE);
            if (packageName instanceof String) {
                if (packageNames == null) {
                    packageNames = new ArrayList<>();
                }
                packageNames.add((String) packageName);
            }
        }
    }
    return packageNames == null ? Collections.<String>emptyList() : packageNames;
}
Also used : ModuleCapability(org.eclipse.osgi.container.ModuleCapability)

Example 10 with ModuleCapability

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

the class Storage method checkSystemBundle.

private void checkSystemBundle() {
    Module systemModule = moduleContainer.getModule(0);
    Generation newGeneration = null;
    try {
        if (systemModule == null) {
            BundleInfo info = new BundleInfo(this, 0, Constants.SYSTEM_BUNDLE_LOCATION, 0);
            newGeneration = info.createGeneration();
            File contentFile = getSystemContent();
            newGeneration.setContent(contentFile, false);
            ModuleRevisionBuilder builder = getBuilder(newGeneration);
            systemModule = moduleContainer.install(null, Constants.SYSTEM_BUNDLE_LOCATION, builder, newGeneration);
            moduleContainer.resolve(Arrays.asList(systemModule), false);
        } else {
            ModuleRevision currentRevision = systemModule.getCurrentRevision();
            Generation currentGeneration = currentRevision == null ? null : (Generation) currentRevision.getRevisionInfo();
            if (currentGeneration == null) {
                // $NON-NLS-1$
                throw new IllegalStateException("No current revision for system bundle.");
            }
            try {
                ModuleRevisionBuilder newBuilder = getBuilder(currentGeneration);
                if (needUpdate(currentRevision, newBuilder)) {
                    newGeneration = currentGeneration.getBundleInfo().createGeneration();
                    File contentFile = getSystemContent();
                    newGeneration.setContent(contentFile, false);
                    moduleContainer.update(systemModule, newBuilder, newGeneration);
                    moduleContainer.refresh(Collections.singleton(systemModule));
                } else {
                    if (currentRevision.getWiring() == null) {
                        // must resolve before continuing to ensure extensions get attached
                        moduleContainer.resolve(Collections.singleton(systemModule), true);
                    }
                }
            } catch (BundleException e) {
                // $NON-NLS-1$
                throw new IllegalStateException("Could not create a builder for the system bundle.", e);
            }
        }
        ModuleRevision currentRevision = systemModule.getCurrentRevision();
        List<ModuleCapability> nativeEnvironments = currentRevision.getModuleCapabilities(NativeNamespace.NATIVE_NAMESPACE);
        Map<String, Object> configMap = equinoxContainer.getConfiguration().getInitialConfig();
        for (ModuleCapability nativeEnvironment : nativeEnvironments) {
            nativeEnvironment.setTransientAttrs(configMap);
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        Requirement osgiPackageReq = ModuleContainer.createRequirement(PackageNamespace.PACKAGE_NAMESPACE, Collections.singletonMap(Namespace.REQUIREMENT_FILTER_DIRECTIVE, "(" + PackageNamespace.PACKAGE_NAMESPACE + "=org.osgi.framework)"), Collections.<String, String>emptyMap());
        Collection<BundleCapability> osgiPackages = moduleContainer.getFrameworkWiring().findProviders(osgiPackageReq);
        for (BundleCapability packageCapability : osgiPackages) {
            if (packageCapability.getRevision().getBundle().getBundleId() == 0) {
                Version v = (Version) packageCapability.getAttributes().get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
                if (v != null) {
                    this.equinoxContainer.getConfiguration().setConfiguration(Constants.FRAMEWORK_VERSION, v.toString());
                    break;
                }
            }
        }
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        // $NON-NLS-1$
        throw new RuntimeException("Error occurred while checking the system module.", e);
    } finally {
        if (newGeneration != null) {
            newGeneration.getBundleInfo().unlockGeneration(newGeneration);
        }
    }
}
Also used : ModuleCapability(org.eclipse.osgi.container.ModuleCapability) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) NoSuchElementException(java.util.NoSuchElementException) MalformedURLException(java.net.MalformedURLException) Requirement(org.osgi.resource.Requirement) Generation(org.eclipse.osgi.storage.BundleInfo.Generation) Version(org.osgi.framework.Version) ModuleRevisionBuilder(org.eclipse.osgi.container.ModuleRevisionBuilder) BundleException(org.osgi.framework.BundleException) Module(org.eclipse.osgi.container.Module) BundleCapability(org.osgi.framework.wiring.BundleCapability) NestedDirBundleFile(org.eclipse.osgi.storage.bundlefile.NestedDirBundleFile) File(java.io.File) ZipBundleFile(org.eclipse.osgi.storage.bundlefile.ZipBundleFile) DirBundleFile(org.eclipse.osgi.storage.bundlefile.DirBundleFile) BundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile) ModuleRevision(org.eclipse.osgi.container.ModuleRevision)

Aggregations

ModuleCapability (org.eclipse.osgi.container.ModuleCapability)11 Module (org.eclipse.osgi.container.Module)6 ModuleContainer (org.eclipse.osgi.container.ModuleContainer)4 ModuleRevision (org.eclipse.osgi.container.ModuleRevision)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 ModuleWire (org.eclipse.osgi.container.ModuleWire)3 ModuleWiring (org.eclipse.osgi.container.ModuleWiring)3 DummyContainerAdaptor (org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor)3 File (java.io.File)2 HashMap (java.util.HashMap)2 Generation (org.eclipse.osgi.storage.BundleInfo.Generation)2 BundleCapability (org.osgi.framework.wiring.BundleCapability)2 Requirement (org.osgi.resource.Requirement)2 ExportedPackage (org.osgi.service.packageadmin.ExportedPackage)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1