Search in sources :

Example 36 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.

the class BundleWiringImpl method getRequirements.

@Override
public List<BundleRequirement> getRequirements(String namespace) {
    if (isInUse()) {
        List<BundleRequirement> searchReqs = m_resolvedReqs;
        List<BundleRequirement> wovenReqs = m_wovenReqs;
        List<BundleRequirement> result = m_resolvedReqs;
        if (wovenReqs != null) {
            searchReqs = new ArrayList<BundleRequirement>(m_resolvedReqs);
            searchReqs.addAll(wovenReqs);
            result = searchReqs;
        }
        if (namespace != null) {
            result = new ArrayList<BundleRequirement>();
            for (BundleRequirement req : searchReqs) {
                if (req.getNamespace().equals(namespace)) {
                    result.add(req);
                }
            }
        }
        return result;
    }
    return null;
}
Also used : BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 37 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.

the class BundleWiringImpl method diagnoseClassLoadError.

private static String diagnoseClassLoadError(StatefulResolver resolver, BundleRevision revision, String name) {
    // We will try to do some diagnostics here to help the developer
    // deal with this exception.
    // Get package name.
    String pkgName = Util.getClassPackage(name);
    if (pkgName.length() == 0) {
        return null;
    }
    // First, get the bundle string of the revision doing the class loader.
    String importer = revision.getBundle().toString();
    // Next, check to see if the revision imports the package.
    List<BundleWire> wires = (revision.getWiring() == null) ? null : revision.getWiring().getProvidedWires(null);
    for (int i = 0; (wires != null) && (i < wires.size()); i++) {
        if (wires.get(i).getCapability().getNamespace().equals(BundleRevision.PACKAGE_NAMESPACE) && wires.get(i).getCapability().getAttributes().get(BundleRevision.PACKAGE_NAMESPACE).equals(pkgName)) {
            String exporter = wires.get(i).getProviderWiring().getBundle().toString();
            StringBuffer sb = new StringBuffer("*** Package '");
            sb.append(pkgName);
            sb.append("' is imported by bundle ");
            sb.append(importer);
            sb.append(" from bundle ");
            sb.append(exporter);
            sb.append(", but the exported package from bundle ");
            sb.append(exporter);
            sb.append(" does not contain the requested class '");
            sb.append(name);
            sb.append("'. Please verify that the class name is correct in the importing bundle ");
            sb.append(importer);
            sb.append(" and/or that the exported package is correctly bundled in ");
            sb.append(exporter);
            sb.append(". ***");
            return sb.toString();
        }
    }
    // Next, check to see if the package was optionally imported and
    // whether or not there is an exporter available.
    List<BundleRequirement> reqs = revision.getWiring().getRequirements(null);
    // Next, check to see if the package is dynamically imported by the revision.
    if (resolver.isAllowedDynamicImport(revision, pkgName)) {
        // Try to see if there is an exporter available.
        Map<String, String> dirs = Collections.EMPTY_MAP;
        Map<String, Object> attrs = Collections.singletonMap(BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName);
        BundleRequirementImpl req = new BundleRequirementImpl(revision, BundleRevision.PACKAGE_NAMESPACE, dirs, attrs);
        List<BundleCapability> exporters = resolver.findProviders(req, false);
        BundleRevision provider = null;
        try {
            provider = resolver.resolve(revision, pkgName);
        } catch (Exception ex) {
            provider = null;
        }
        String exporter = (exporters.isEmpty()) ? null : exporters.iterator().next().toString();
        StringBuffer sb = new StringBuffer("*** Class '");
        sb.append(name);
        sb.append("' was not found, but this is likely normal since package '");
        sb.append(pkgName);
        sb.append("' is dynamically imported by bundle ");
        sb.append(importer);
        sb.append(".");
        if ((exporters.size() > 0) && (provider == null)) {
            sb.append(" However, bundle ");
            sb.append(exporter);
            sb.append(" does export this package with attributes that do not match.");
        }
        sb.append(" ***");
        return sb.toString();
    }
    // Next, check to see if there are any exporters for the package at all.
    Map<String, String> dirs = Collections.EMPTY_MAP;
    Map<String, Object> attrs = Collections.singletonMap(BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName);
    BundleRequirementImpl req = new BundleRequirementImpl(revision, BundleRevision.PACKAGE_NAMESPACE, dirs, attrs);
    List<BundleCapability> exports = resolver.findProviders(req, false);
    if (exports.size() > 0) {
        boolean classpath = false;
        try {
            BundleRevisionImpl.getSecureAction().getClassLoader(BundleClassLoader.class).loadClass(name);
            classpath = true;
        } catch (NoClassDefFoundError err) {
        // Ignore
        } catch (Exception ex) {
        // Ignore
        }
        String exporter = exports.iterator().next().toString();
        StringBuffer sb = new StringBuffer("*** Class '");
        sb.append(name);
        sb.append("' was not found because bundle ");
        sb.append(importer);
        sb.append(" does not import '");
        sb.append(pkgName);
        sb.append("' even though bundle ");
        sb.append(exporter);
        sb.append(" does export it.");
        if (classpath) {
            sb.append(" Additionally, the class is also available from the system class loader. There are two fixes: 1) Add an import for '");
            sb.append(pkgName);
            sb.append("' to bundle ");
            sb.append(importer);
            sb.append("; imports are necessary for each class directly touched by bundle code or indirectly touched, such as super classes if their methods are used. ");
            sb.append("2) Add package '");
            sb.append(pkgName);
            sb.append("' to the '");
            sb.append(Constants.FRAMEWORK_BOOTDELEGATION);
            sb.append("' property; a library or VM bug can cause classes to be loaded by the wrong class loader. The first approach is preferable for preserving modularity.");
        } else {
            sb.append(" To resolve this issue, add an import for '");
            sb.append(pkgName);
            sb.append("' to bundle ");
            sb.append(importer);
            sb.append(".");
        }
        sb.append(" ***");
        return sb.toString();
    }
    // class loader.
    try {
        BundleRevisionImpl.getSecureAction().getClassLoader(BundleClassLoader.class).loadClass(name);
        StringBuffer sb = new StringBuffer("*** Package '");
        sb.append(pkgName);
        sb.append("' is not imported by bundle ");
        sb.append(importer);
        sb.append(", nor is there any bundle that exports package '");
        sb.append(pkgName);
        sb.append("'. However, the class '");
        sb.append(name);
        sb.append("' is available from the system class loader. There are two fixes: 1) Add package '");
        sb.append(pkgName);
        sb.append("' to the '");
        sb.append(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA);
        sb.append("' property and modify bundle ");
        sb.append(importer);
        sb.append(" to import this package; this causes the system bundle to export class path packages. 2) Add package '");
        sb.append(pkgName);
        sb.append("' to the '");
        sb.append(Constants.FRAMEWORK_BOOTDELEGATION);
        sb.append("' property; a library or VM bug can cause classes to be loaded by the wrong class loader. The first approach is preferable for preserving modularity.");
        sb.append(" ***");
        return sb.toString();
    } catch (Exception ex2) {
    }
    // Finally, if there are no imports or exports for the package
    // and it is not available on the system class path, simply
    // log a message saying so.
    StringBuffer sb = new StringBuffer("*** Class '");
    sb.append(name);
    sb.append("' was not found. Bundle ");
    sb.append(importer);
    sb.append(" does not import package '");
    sb.append(pkgName);
    sb.append("', nor is the package exported by any other bundle or available from the system class loader.");
    sb.append(" ***");
    return sb.toString();
}
Also used : BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl) BundleException(org.osgi.framework.BundleException) ResolutionException(org.osgi.service.resolver.ResolutionException) ResourceNotFoundException(org.apache.felix.framework.resolver.ResourceNotFoundException) WeavingException(org.osgi.framework.hooks.weaving.WeavingException) PrivilegedActionException(java.security.PrivilegedActionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 38 with BundleRequirement

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

the class TestModuleContainer method testR3.

@Test
public void testR3() 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());
    // R3 bundle
    Map<String, String> exporterManifest = new HashMap<String, String>();
    exporterManifest = new HashMap<String, String>();
    exporterManifest.put(Constants.BUNDLE_SYMBOLICNAME, "exporter");
    exporterManifest.put(Constants.EXPORT_PACKAGE, "exporter; version=\"1.1\"");
    Module moduleExport = installDummyModule(exporterManifest, "exporter", container);
    report = container.resolve(Arrays.asList(moduleExport, moduleExport), true);
    Assert.assertNull("Failed to resolve", report.getResolutionException());
    List<BundleRequirement> reqs = moduleExport.getCurrentRevision().getWiring().getRequirements(PackageNamespace.PACKAGE_NAMESPACE);
    assertEquals("Wrong number of imports.", 0, reqs.size());
    // R3 bundle
    exporterManifest.clear();
    exporterManifest.put(Constants.BUNDLE_SYMBOLICNAME, "dynamicExporter");
    exporterManifest.put(Constants.EXPORT_PACKAGE, "exporter; version=\"1.0\"");
    exporterManifest.put(Constants.DYNAMICIMPORT_PACKAGE, "exporter");
    Module moduleWithDynExport = installDummyModule(exporterManifest, "dynamicExporter", container);
    report = container.resolve(Arrays.asList(moduleWithDynExport), true);
    Assert.assertNull("Failed to resolve", report.getResolutionException());
    reqs = moduleWithDynExport.getCurrentRevision().getWiring().getRequirements(PackageNamespace.PACKAGE_NAMESPACE);
    assertEquals("Wrong number of imports.", 2, reqs.size());
    report = container.resolve(Arrays.asList(moduleWithDynExport), true);
    Assert.assertNull("Failed to resolve", report.getResolutionException());
    reqs = moduleWithDynExport.getCurrentRevision().getWiring().getRequirements(PackageNamespace.PACKAGE_NAMESPACE);
    assertEquals("Wrong number of imports.", 2, reqs.size());
    ModuleWiring wiring = moduleWithDynExport.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) Test(org.junit.Test)

Example 39 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement 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 40 with BundleRequirement

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

Aggregations

BundleRequirement (org.osgi.framework.wiring.BundleRequirement)55 BundleCapability (org.osgi.framework.wiring.BundleCapability)23 ArrayList (java.util.ArrayList)20 BundleWire (org.osgi.framework.wiring.BundleWire)18 BundleRevision (org.osgi.framework.wiring.BundleRevision)17 HashMap (java.util.HashMap)15 Bundle (org.osgi.framework.Bundle)15 Test (org.junit.Test)14 BundleException (org.osgi.framework.BundleException)11 ResolverHook (org.osgi.framework.hooks.resolver.ResolverHook)10 BundleWiring (org.osgi.framework.wiring.BundleWiring)10 List (java.util.List)9 Collection (java.util.Collection)8 BundleRequirementImpl (org.apache.felix.framework.wiring.BundleRequirementImpl)8 ResolverHookFactory (org.osgi.framework.hooks.resolver.ResolverHookFactory)8 CompositeData (javax.management.openmbean.CompositeData)6 Module (org.eclipse.osgi.container.Module)6 ModuleContainer (org.eclipse.osgi.container.ModuleContainer)6 DummyContainerAdaptor (org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor)6 SimpleFilter (org.apache.felix.framework.capabilityset.SimpleFilter)5