Search in sources :

Example 71 with BundleCapability

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

the class PackageAdminImpl method getRequiredBundles.

public RequiredBundle[] getRequiredBundles(String symbolicName) {
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-4$
    String filter = "(" + BundleNamespace.BUNDLE_NAMESPACE + "=" + (symbolicName == null ? "*" : symbolicName) + ")";
    Map<String, String> directives = Collections.<String, String>singletonMap(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter);
    Map<String, Boolean> attributes = Collections.singletonMap(Capabilities.SYNTHETIC_REQUIREMENT, Boolean.TRUE);
    Requirement bundleReq = ModuleContainer.createRequirement(BundleNamespace.BUNDLE_NAMESPACE, directives, attributes);
    Collection<BundleCapability> bundleCaps = container.getFrameworkWiring().findProviders(bundleReq);
    InternalUtils.filterCapabilityPermissions(bundleCaps);
    Collection<RequiredBundle> result = new ArrayList<>();
    for (BundleCapability capability : bundleCaps) {
        BundleWiring wiring = capability.getRevision().getWiring();
        if (wiring != null) {
            result.add(new RequiredBundleImpl(capability, wiring));
        }
    }
    return result.isEmpty() ? null : result.toArray(new RequiredBundle[result.size()]);
}
Also used : Requirement(org.osgi.resource.Requirement) BundleWiring(org.osgi.framework.wiring.BundleWiring) ArrayList(java.util.ArrayList) BundleCapability(org.osgi.framework.wiring.BundleCapability) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle)

Example 72 with BundleCapability

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

the class SystemBundleTests method doTestBug351519Refresh.

private void doTestBug351519Refresh(Boolean refreshDuplicates) {
    // Create a framework with equinox.refresh.duplicate.bsn=false configuration
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile(getName());
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    if (refreshDuplicates != null) {
        configuration.put("equinox.refresh.duplicate.bsn", refreshDuplicates.toString());
    } else {
        // we default to false now
        refreshDuplicates = Boolean.FALSE;
    }
    Equinox equinox = new Equinox(configuration);
    try {
        equinox.start();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected exception in init()", e);
    }
    BundleContext systemContext = equinox.getBundleContext();
    systemContext.registerService(ResolverHookFactory.class, new ResolverHookFactory() {

        public ResolverHook begin(Collection triggers) {
            return new ResolverHook() {

                public void filterResolvable(Collection candidates) {
                // nothing
                }

                public void filterSingletonCollisions(BundleCapability singleton, Collection collisionCandidates) {
                    // resolve all singletons
                    collisionCandidates.clear();
                }

                public void filterMatches(BundleRequirement requirement, Collection candidates) {
                // nothing
                }

                public void end() {
                // nothing
                }
            };
        }
    }, null);
    BundleInstaller testBundleInstaller = null;
    BundleInstaller testBundleResolver = null;
    try {
        testBundleResolver = new BundleInstaller(OSGiTestsActivator.TEST_FILES_ROOT + "wiringTests/bundles", systemContext);
        testBundleInstaller = new BundleInstaller(OSGiTestsActivator.TEST_FILES_ROOT + "wiringTests/bundles", getContext());
    } catch (InvalidSyntaxException e) {
        fail("Failed to create installers.", e);
    }
    // $NON-NLS-1$
    assertNotNull("System context is null", systemContext);
    // try installing a bundle before starting
    Bundle tb1v1 = null, tb1v2 = null;
    try {
        // $NON-NLS-1$
        tb1v1 = systemContext.installBundle(testBundleInstaller.getBundleLocation("singleton.tb1v1"));
        // $NON-NLS-1$
        tb1v2 = systemContext.installBundle(testBundleInstaller.getBundleLocation("singleton.tb1v2"));
    } catch (BundleException e1) {
        // $NON-NLS-1$
        fail("failed to install a bundle", e1);
    }
    assertTrue("Could not resolve test bundles", testBundleResolver.resolveBundles(new Bundle[] { tb1v1, tb1v2 }));
    Bundle[] refreshed = testBundleResolver.refreshPackages(new Bundle[] { tb1v1 });
    if (refreshDuplicates) {
        List refreshedList = Arrays.asList(refreshed);
        assertEquals("Wrong number of refreshed bundles", 2, refreshed.length);
        assertTrue("Refreshed bundles does not include v1", refreshedList.contains(tb1v1));
        assertTrue("Refreshed bundles does not include v2", refreshedList.contains(tb1v2));
    } else {
        assertEquals("Wrong number of refreshed bundles", 1, refreshed.length);
        assertEquals("Refreshed bundles does not include v1", refreshed[0], tb1v1);
    }
    try {
        equinox.stop();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected erorr stopping framework", e);
    }
    try {
        equinox.waitForStop(10000);
    } catch (InterruptedException e) {
        // $NON-NLS-1$
        fail("Unexpected interrupted exception", e);
    }
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox.getState());
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) Bundle(org.osgi.framework.Bundle) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) Equinox(org.eclipse.osgi.launch.Equinox) Collection(java.util.Collection) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BundleException(org.osgi.framework.BundleException) BundleCapability(org.osgi.framework.wiring.BundleCapability) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 73 with BundleCapability

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

the class ClassLoadingBundleTests method testBug370258_endException.

public void testBug370258_endException() {
    final boolean[] endCalled = { false };
    ResolverHookFactory endHook = new ResolverHookFactory() {

        public ResolverHook begin(Collection triggers) {
            return new ResolverHook() {

                public void filterSingletonCollisions(BundleCapability singleton, Collection collisionCandidates) {
                // Nothing
                }

                public void filterResolvable(Collection candidates) {
                    throw new RuntimeException("Error");
                }

                public void filterMatches(BundleRequirement requirement, Collection candidates) {
                // Nothing
                }

                public void end() {
                    endCalled[0] = true;
                }
            };
        }
    };
    ResolverHookFactory error = new ResolverHookFactory() {

        public ResolverHook begin(Collection triggers) {
            return new ResolverHook() {

                public void filterSingletonCollisions(BundleCapability singleton, Collection collisionCandidates) {
                // Nothing
                }

                public void filterResolvable(Collection candidates) {
                // Nothing
                }

                public void filterMatches(BundleRequirement requirement, Collection candidates) {
                // Nothing
                }

                public void end() {
                    throw new RuntimeException("Error");
                }
            };
        }
    };
    ServiceRegistration errorReg = OSGiTestsActivator.getContext().registerService(ResolverHookFactory.class, error, null);
    ServiceRegistration endReg = OSGiTestsActivator.getContext().registerService(ResolverHookFactory.class, endHook, null);
    try {
        // $NON-NLS-1$
        Bundle test = installer.installBundle("test");
        try {
            test.start();
            fail("Should not be able to start this bundle");
        } catch (BundleException e) {
            // expected
            assertEquals("Wrong exception type.", BundleException.REJECTED_BY_HOOK, e.getType());
        }
    } catch (BundleException e) {
        fail("Unexpected install fail", e);
    } finally {
        errorReg.unregister();
        endReg.unregister();
    }
    assertTrue("end is not called", endCalled[0]);
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) Bundle(org.osgi.framework.Bundle) Collection(java.util.Collection) BundleException(org.osgi.framework.BundleException) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 74 with BundleCapability

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

the class ClassLoadingBundleTests method testBug348806.

public void testBug348806() {
    ResolverHookFactory error = new ResolverHookFactory() {

        public ResolverHook begin(Collection triggers) {
            return new ResolverHook() {

                public void filterSingletonCollisions(BundleCapability singleton, Collection collisionCandidates) {
                // Nothing
                }

                public void filterResolvable(Collection candidates) {
                // Nothing
                }

                public void filterMatches(BundleRequirement requirement, Collection candidates) {
                // Nothing
                }

                public void end() {
                    throw new RuntimeException("Error");
                }
            };
        }
    };
    ServiceRegistration reg = OSGiTestsActivator.getContext().registerService(ResolverHookFactory.class, error, null);
    try {
        // $NON-NLS-1$
        Bundle test = installer.installBundle("test");
        try {
            test.start();
            fail("Should not be able to start this bundle");
        } catch (BundleException e) {
            // expected
            assertEquals("Wrong exception type.", BundleException.REJECTED_BY_HOOK, e.getType());
        }
    } catch (BundleException e) {
        fail("Unexpected install fail", e);
    } finally {
        reg.unregister();
    }
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) Bundle(org.osgi.framework.Bundle) Collection(java.util.Collection) BundleException(org.osgi.framework.BundleException) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 75 with BundleCapability

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

the class ClassLoadingBundleTests method testBug370258_beginException.

public void testBug370258_beginException() {
    final boolean[] endCalled = { false };
    ResolverHookFactory endHook = new ResolverHookFactory() {

        public ResolverHook begin(Collection triggers) {
            return new ResolverHook() {

                public void filterSingletonCollisions(BundleCapability singleton, Collection collisionCandidates) {
                // Nothing
                }

                public void filterResolvable(Collection candidates) {
                    throw new RuntimeException("Error");
                }

                public void filterMatches(BundleRequirement requirement, Collection candidates) {
                // Nothing
                }

                public void end() {
                    endCalled[0] = true;
                }
            };
        }
    };
    ResolverHookFactory error = new ResolverHookFactory() {

        public ResolverHook begin(Collection triggers) {
            throw new RuntimeException("Error");
        }
    };
    ServiceRegistration endReg = OSGiTestsActivator.getContext().registerService(ResolverHookFactory.class, endHook, null);
    ServiceRegistration errorReg = OSGiTestsActivator.getContext().registerService(ResolverHookFactory.class, error, null);
    try {
        // $NON-NLS-1$
        Bundle test = installer.installBundle("test");
        try {
            test.start();
            fail("Should not be able to start this bundle");
        } catch (BundleException e) {
            // expected
            assertEquals("Wrong exception type.", BundleException.REJECTED_BY_HOOK, e.getType());
        }
    } catch (BundleException e) {
        fail("Unexpected install fail", e);
    } finally {
        errorReg.unregister();
        endReg.unregister();
    }
    assertTrue("end is not called", endCalled[0]);
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) Bundle(org.osgi.framework.Bundle) Collection(java.util.Collection) BundleException(org.osgi.framework.BundleException) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) ServiceRegistration(org.osgi.framework.ServiceRegistration)

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