Search in sources :

Example 66 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project rt.equinox.framework by eclipse.

the class TestModuleContainer method testBug483849.

@Test
public void testBug483849() throws BundleException, IOException {
    DummyContainerAdaptor adaptor = createDummyAdaptor();
    ModuleContainer container = adaptor.getContainer();
    // install and resolve host bundle
    Module host = installDummyModule("bug483849.host.MF", "host", container);
    ResolutionReport report = container.resolve(Arrays.asList(host), true);
    Assert.assertNull("Failed to resolve host.", report.getResolutionException());
    // install and dynamically attach a fragment that exports a package and resolve an importer
    Module frag = installDummyModule("bug483849.frag.MF", "frag", container);
    Module importer = installDummyModule("bug483849.importer.MF", "importer", container);
    report = container.resolve(Arrays.asList(frag, importer), true);
    Assert.assertNull("Failed to resolve test fragment and importer.", report.getResolutionException());
    // get the count of package exports
    ModuleWiring wiring = host.getCurrentRevision().getWiring();
    int originalPackageCnt = wiring.getCapabilities(PackageNamespace.PACKAGE_NAMESPACE).size();
    // update the host to generate a new revision
    Map<String, String> updateManifest = getManifest("bug483849.host.MF");
    ModuleRevisionBuilder updateBuilder = OSGiManifestBuilderFactory.createBuilder(updateManifest);
    container.update(host, updateBuilder, null);
    // refresh host which should force the importer to re-resolve to the new revision
    report = container.refresh(Collections.singleton(host));
    ModuleWiring importerWiring = importer.getCurrentRevision().getWiring();
    Assert.assertNotNull("No wiring for importer.", importerWiring);
    List<ModuleWire> importerPackageWires = importerWiring.getRequiredModuleWires(PackageNamespace.PACKAGE_NAMESPACE);
    Assert.assertEquals("Wrong number of importer package Wires.", 1, importerPackageWires.size());
    Assert.assertEquals("Wrong provider wiring.", host.getCurrentRevision().getWiring(), importerPackageWires.iterator().next().getProviderWiring());
    Assert.assertEquals("Wrong provider revision.", host.getCurrentRevision(), importerPackageWires.iterator().next().getProviderWiring().getRevision());
    wiring = host.getCurrentRevision().getWiring();
    List<BundleCapability> packages = wiring.getCapabilities(PackageNamespace.PACKAGE_NAMESPACE);
    Assert.assertEquals("Wrong number of host packages.", originalPackageCnt, packages.size());
}
Also used : ModuleWire(org.eclipse.osgi.container.ModuleWire) ModuleContainer(org.eclipse.osgi.container.ModuleContainer) ResolutionReport(org.eclipse.osgi.report.resolution.ResolutionReport) DummyContainerAdaptor(org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor) ModuleWiring(org.eclipse.osgi.container.ModuleWiring) ModuleRevisionBuilder(org.eclipse.osgi.container.ModuleRevisionBuilder) Module(org.eclipse.osgi.container.Module) BundleCapability(org.osgi.framework.wiring.BundleCapability) Test(org.junit.Test)

Example 67 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project rt.equinox.framework by eclipse.

the class TestModuleContainer method testSubstitutableExports01.

@Test
public void testSubstitutableExports01() throws BundleException, IOException {
    DummyContainerAdaptor adaptor = createDummyAdaptor();
    ModuleContainer container = adaptor.getContainer();
    Module systemBundle = installDummyModule("system.bundle.MF", Constants.SYSTEM_BUNDLE_LOCATION, container);
    container.resolve(Arrays.asList(systemBundle), true);
    Module sub1 = installDummyModule("sub1_v1.MF", "sub1", container);
    Module sub2 = installDummyModule("sub2_v1.MF", "sub2", container);
    ModuleRevision sub1Revision0 = sub1.getCurrentRevision();
    container.resolve(Arrays.asList(sub2), true);
    container.update(sub1, OSGiManifestBuilderFactory.createBuilder(getManifest("sub1_v2.MF")), null);
    container.resolve(Arrays.asList(sub1), true);
    ModuleWiring sub1Wiring = sub1.getCurrentRevision().getWiring();
    List<BundleCapability> exportedPackages = sub1Wiring.getCapabilities(PackageNamespace.PACKAGE_NAMESPACE);
    Assert.assertEquals("Wrong number of exported packages: " + exportedPackages, 0, exportedPackages.size());
    List<BundleWire> requiredWires = sub1Wiring.getRequiredWires(PackageNamespace.PACKAGE_NAMESPACE);
    Assert.assertEquals("Wrong number of imported packages: ", 2, requiredWires.size());
    Assert.assertEquals("Wrong provider for package: " + requiredWires.get(1).getProvider(), sub1Revision0, requiredWires.get(1).getProvider());
    container.refresh(Arrays.asList(sub1));
    sub1Wiring = sub1.getCurrentRevision().getWiring();
    exportedPackages = sub1Wiring.getCapabilities(PackageNamespace.PACKAGE_NAMESPACE);
    Assert.assertEquals("Wrong number of exported packages: " + exportedPackages, 1, exportedPackages.size());
    requiredWires = sub1Wiring.getRequiredWires(PackageNamespace.PACKAGE_NAMESPACE);
    Assert.assertEquals("Wrong number of imported packages: ", 1, requiredWires.size());
}
Also used : DummyContainerAdaptor(org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor) ModuleContainer(org.eclipse.osgi.container.ModuleContainer) ModuleWiring(org.eclipse.osgi.container.ModuleWiring) Module(org.eclipse.osgi.container.Module) BundleCapability(org.osgi.framework.wiring.BundleCapability) ModuleRevision(org.eclipse.osgi.container.ModuleRevision) BundleWire(org.osgi.framework.wiring.BundleWire) Test(org.junit.Test)

Example 68 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project rt.equinox.framework by eclipse.

the class TestModuleContainer method testSingleton02.

@Test
public void testSingleton02() throws BundleException, IOException {
    ResolverHookFactory resolverHookFactory = new ResolverHookFactory() {

        @Override
        public ResolverHook begin(Collection<BundleRevision> triggers) {
            return new ResolverHook() {

                @Override
                public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) {
                    collisionCandidates.clear();
                }

                @Override
                public void filterResolvable(Collection<BundleRevision> candidates) {
                // nothing
                }

                @Override
                public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) {
                // nothing
                }

                @Override
                public void end() {
                // nothing
                }
            };
        }
    };
    DummyContainerAdaptor adaptor = new DummyContainerAdaptor(new DummyCollisionHook(false), Collections.<String, String>emptyMap(), resolverHookFactory);
    ModuleContainer container = adaptor.getContainer();
    Module s1 = installDummyModule("singleton1_v1.MF", "s1_v1", container);
    Module s2 = installDummyModule("singleton1_v2.MF", "s1_v2", container);
    Module s3 = installDummyModule("singleton1_v3.MF", "s1_v3", container);
    container.resolve(null, false);
    Assert.assertTrue("Singleton v1 is not resolved.", Module.RESOLVED_SET.contains(s1.getState()));
    Assert.assertTrue("Singleton v2 is not resolved.", Module.RESOLVED_SET.contains(s2.getState()));
    Assert.assertTrue("Singleton v3 is not resolved.", Module.RESOLVED_SET.contains(s3.getState()));
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) DummyResolverHookFactory(org.eclipse.osgi.tests.container.dummys.DummyResolverHookFactory) DummyContainerAdaptor(org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor) DummyResolverHook(org.eclipse.osgi.tests.container.dummys.DummyResolverHook) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) DummyCollisionHook(org.eclipse.osgi.tests.container.dummys.DummyCollisionHook) ModuleContainer(org.eclipse.osgi.container.ModuleContainer) Collection(java.util.Collection) BundleCapability(org.osgi.framework.wiring.BundleCapability) Module(org.eclipse.osgi.container.Module) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) Test(org.junit.Test)

Example 69 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability 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)

Example 70 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project rt.equinox.framework by eclipse.

the class PackageAdminImpl method getBundles.

public Bundle[] getBundles(String symbolicName, String versionRange) {
    if (symbolicName == null) {
        throw new IllegalArgumentException();
    }
    if (Constants.SYSTEM_BUNDLE_SYMBOLICNAME.equals(symbolicName)) {
        // need to alias system.bundle to the implementation BSN
        symbolicName = EquinoxContainer.NAME;
    }
    VersionRange range = versionRange == null ? null : new VersionRange(versionRange);
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
    String filter = (range != null ? "(&" : "") + "(" + IdentityNamespace.IDENTITY_NAMESPACE + "=" + symbolicName + ")" + (range != null ? range.toFilterString(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE) + ")" : "");
    Requirement identityReq = ModuleContainer.createRequirement(IdentityNamespace.IDENTITY_NAMESPACE, Collections.<String, String>singletonMap(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter), Collections.<String, Object>emptyMap());
    Collection<BundleCapability> identityCaps = container.getFrameworkWiring().findProviders(identityReq);
    if (identityCaps.isEmpty()) {
        return null;
    }
    List<Bundle> sorted = new ArrayList<>(identityCaps.size());
    for (BundleCapability capability : identityCaps) {
        Bundle b = capability.getRevision().getBundle();
        // a sanity check incase this is an old revision
        if (symbolicName.equals(b.getSymbolicName()) && !sorted.contains(b)) {
            sorted.add(b);
        }
    }
    Collections.sort(sorted, new Comparator<Bundle>() {

        @Override
        public int compare(Bundle b1, Bundle b2) {
            return b2.getVersion().compareTo(b1.getVersion());
        }
    });
    if (sorted.isEmpty()) {
        return null;
    }
    return sorted.toArray(new Bundle[sorted.size()]);
}
Also used : Requirement(org.osgi.resource.Requirement) Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) ArrayList(java.util.ArrayList) VersionRange(org.osgi.framework.VersionRange) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Aggregations

BundleCapability (org.osgi.framework.wiring.BundleCapability)83 ArrayList (java.util.ArrayList)30 BundleRevision (org.osgi.framework.wiring.BundleRevision)26 HashMap (java.util.HashMap)24 Bundle (org.osgi.framework.Bundle)24 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)21 BundleWiring (org.osgi.framework.wiring.BundleWiring)19 BundleWire (org.osgi.framework.wiring.BundleWire)18 List (java.util.List)15 Version (org.osgi.framework.Version)12 BundleException (org.osgi.framework.BundleException)11 ResolverHook (org.osgi.framework.hooks.resolver.ResolverHook)11 Collection (java.util.Collection)10 Test (org.junit.Test)10 HashSet (java.util.HashSet)9 Set (java.util.Set)8 ResolverHookFactory (org.osgi.framework.hooks.resolver.ResolverHookFactory)8 LinkedHashMap (java.util.LinkedHashMap)6 Module (org.eclipse.osgi.container.Module)6 CompositeData (javax.management.openmbean.CompositeData)5