Search in sources :

Example 16 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project rt.equinox.framework by eclipse.

the class PackageAdminImpl method getExportedPackages.

public ExportedPackage[] getExportedPackages(String name) {
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-4$
    String filter = "(" + PackageNamespace.PACKAGE_NAMESPACE + "=" + (name == null ? "*" : name) + ")";
    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 packageReq = ModuleContainer.createRequirement(PackageNamespace.PACKAGE_NAMESPACE, directives, attributes);
    Collection<BundleCapability> packageCaps = container.getFrameworkWiring().findProviders(packageReq);
    InternalUtils.filterCapabilityPermissions(packageCaps);
    List<ExportedPackage> result = new ArrayList<>();
    for (BundleCapability capability : packageCaps) {
        ModuleWiring wiring = (ModuleWiring) capability.getRevision().getWiring();
        if (wiring != null) {
            Collection<ModuleWiring> wirings = Collections.emptyList();
            if ((capability.getRevision().getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) {
                // This is a fragment, just get all the host wirings
                List<ModuleWire> hostWires = wiring.getRequiredModuleWires(HostNamespace.HOST_NAMESPACE);
                if (hostWires != null && !hostWires.isEmpty()) {
                    wirings = new ArrayList<>(hostWires.size());
                    for (ModuleWire hostWire : hostWires) {
                        ModuleWiring hostWiring = hostWire.getProviderWiring();
                        if (hostWiring != null) {
                            wirings.add(hostWiring);
                        }
                    }
                }
            } else {
                // just a single host wiring
                wirings = Collections.singletonList(wiring);
            }
            for (ModuleWiring moduleWiring : wirings) {
                if (!moduleWiring.getSubstitutedNames().contains(capability.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE))) {
                    result.add(new ExportedPackageImpl((ModuleCapability) capability, moduleWiring));
                }
            }
        }
    }
    return (result.size() == 0 ? null : result.toArray(new ExportedPackage[result.size()]));
}
Also used : ModuleWire(org.eclipse.osgi.container.ModuleWire) ArrayList(java.util.ArrayList) ModuleCapability(org.eclipse.osgi.container.ModuleCapability) Requirement(org.osgi.resource.Requirement) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) ModuleWiring(org.eclipse.osgi.container.ModuleWiring) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 17 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project rt.equinox.framework by eclipse.

the class ClassLoadingBundleTests method testImporterExporter01.

public void testImporterExporter01() throws BundleException {
    // $NON-NLS-1$
    Bundle importerExporter1 = installer.installBundle("exporter.importer1");
    installer.resolveBundles(new Bundle[] { importerExporter1 });
    PackageAdmin pa = installer.getPackageAdmin();
    // $NON-NLS-1$
    ExportedPackage[] origExportedPackages = pa.getExportedPackages("exporter.importer.test");
    // $NON-NLS-1$
    assertNotNull("No exporter.importer.test found", origExportedPackages);
    // $NON-NLS-1$
    assertEquals("Wrong number of exports", 1, origExportedPackages.length);
    Bundle exporter = origExportedPackages[0].getExportingBundle();
    // $NON-NLS-1$
    assertEquals("Wrong exporter", importerExporter1, exporter);
    // TODO need to get clarification from OSGi on what is returned by getImportingBundles when there is no importers
    Bundle[] origImporters = origExportedPackages[0].getImportingBundles();
    // $NON-NLS-1$
    assertTrue("Should have no importers", origImporters == null || origImporters.length == 0);
    // install another importer/exporter.  This bundle should wire to the original exporter
    // $NON-NLS-1$
    Bundle importerExporter2 = installer.installBundle("exporter.importer2");
    installer.resolveBundles(new Bundle[] { importerExporter2 });
    origImporters = origExportedPackages[0].getImportingBundles();
    // $NON-NLS-1$
    assertNotNull("No importers found", origImporters);
    // $NON-NLS-1$
    assertEquals("Wrong number of importers", 1, origImporters.length);
    // $NON-NLS-1$
    assertEquals("Wrong importer", importerExporter2, origImporters[0]);
    // $NON-NLS-1$
    ExportedPackage[] newExportedPackages = pa.getExportedPackages("exporter.importer.test");
    // $NON-NLS-1$
    assertNotNull("No exporter.importer.test found", newExportedPackages);
    // $NON-NLS-1$
    assertEquals("Wrong number of exports", 1, newExportedPackages.length);
    exporter = newExportedPackages[0].getExportingBundle();
    // $NON-NLS-1$
    assertEquals("Wrong exporter", importerExporter1, exporter);
    Bundle[] newImporters = newExportedPackages[0].getImportingBundles();
    // $NON-NLS-1$
    assertNotNull("No importers found", newImporters);
    // $NON-NLS-1$
    assertEquals("Wrong number of importers", 1, newImporters.length);
    // $NON-NLS-1$
    assertEquals("Wrong importer", importerExporter2, newImporters[0]);
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) Bundle(org.osgi.framework.Bundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage)

Example 18 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project rt.equinox.framework by eclipse.

the class ClassLoadingBundleTests method testMultipleExportFragments01.

public void testMultipleExportFragments01() throws Exception {
    // $NON-NLS-1$
    Bundle host = installer.installBundle("host.multiple.exports");
    // $NON-NLS-1$
    Bundle frag = installer.installBundle("frag.multiple.exports");
    installer.resolveBundles(new Bundle[] { host, frag });
    PackageAdmin packageAdmin = installer.getPackageAdmin();
    ExportedPackage[] hostExports = packageAdmin.getExportedPackages(host);
    // $NON-NLS-1$
    assertEquals("Number host exports", 4, hostExports == null ? 0 : hostExports.length);
    BundleWiring hostWiring = host.adapt(BundleWiring.class);
    assertNotNull("No host wiring", hostWiring);
    List packageCapabilities = hostWiring.getCapabilities(PackageNamespace.PACKAGE_NAMESPACE);
    // $NON-NLS-1$
    assertEquals("Number host export capabilities", 4, packageCapabilities.size());
    // $NON-NLS-1$//$NON-NLS-2$
    assertEquals("Check export name", "host.multiple.exports", ((BundleCapability) packageCapabilities.get(0)).getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
    // $NON-NLS-1$//$NON-NLS-2$
    assertEquals("Check include directive", "Public*", ((BundleCapability) packageCapabilities.get(0)).getDirectives().get(PackageNamespace.CAPABILITY_INCLUDE_DIRECTIVE));
    // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals("Check export name", "host.multiple.exports.onlyone", ((BundleCapability) packageCapabilities.get(1)).getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
    // $NON-NLS-1$//$NON-NLS-2$
    assertEquals("Check export name", "host.multiple.exports", ((BundleCapability) packageCapabilities.get(2)).getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
    // $NON-NLS-1$//$NON-NLS-2$
    assertEquals("Check scope attribute", "private", (String) ((BundleCapability) packageCapabilities.get(2)).getAttributes().get("scope"));
    // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals("Check export name", "host.multiple.exports.onlyone", ((BundleCapability) packageCapabilities.get(3)).getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) Bundle(org.osgi.framework.Bundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) BundleWiring(org.osgi.framework.wiring.BundleWiring) List(java.util.List) ArrayList(java.util.ArrayList) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 19 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project rt.equinox.framework by eclipse.

the class PackageAdminBundleTests method testBug287636.

public void testBug287636() throws Exception {
    // $NON-NLS-1$
    Bundle bug287636a = installer.installBundle("test.bug287636.a1");
    // $NON-NLS-1$
    Bundle bug287636b = installer.installBundle("test.bug287636.b");
    try {
        bug287636a.start();
        bug287636b.start();
        // $NON-NLS-1$
        assertTrue("Bundles are not resolved", installer.resolveBundles(new Bundle[] { bug287636a, bug287636b }));
        // $NON-NLS-1$
        ExportedPackage ep = installer.getPackageAdmin().getExportedPackage("test.bug287636.a");
        // $NON-NLS-1$
        assertNotNull("Could not find exported package", ep);
        // $NON-NLS-1$
        assertEquals("Wrong version", new Version(1, 0, 0), ep.getVersion());
        // update bundle to export new 1.1.0 version of the pacakge
        // $NON-NLS-1$
        String updateLocation = installer.getBundleLocation("test.bug287636.a2");
        bug287636a.update(new URL(updateLocation).openStream());
        bug287636b.update();
        // $NON-NLS-1$
        updateLocation = installer.getBundleLocation("test.bug287636.a1");
        bug287636a.update(new URL(updateLocation).openStream());
        bug287636b.update();
        // $NON-NLS-1$
        updateLocation = installer.getBundleLocation("test.bug287636.a2");
        bug287636a.update(new URL(updateLocation).openStream());
        bug287636b.update();
        installer.refreshPackages(null);
        // $NON-NLS-1$
        ep = installer.getPackageAdmin().getExportedPackage("test.bug287636.a");
        // $NON-NLS-1$
        assertNotNull("Could not find exported package", ep);
        // $NON-NLS-1$
        assertEquals("Wrong version", new Version(1, 1, 0), ep.getVersion());
        // $NON-NLS-1$
        ExportedPackage[] eps = installer.getPackageAdmin().getExportedPackages("test.bug287636.a");
        // $NON-NLS-1$
        assertNotNull("Could not find exported package", eps);
        // $NON-NLS-1$
        assertEquals("Wrong number of exports", 1, eps.length);
        // $NON-NLS-1$
        assertEquals("Wrong version", new Version(1, 1, 0), eps[0].getVersion());
        eps = installer.getPackageAdmin().getExportedPackages(bug287636a);
        // $NON-NLS-1$
        assertNotNull("Could not find exported package", eps);
        // $NON-NLS-1$
        assertEquals("Wrong number of exports", 1, eps.length);
        // $NON-NLS-1$
        assertEquals("Wrong version", new Version(1, 1, 0), eps[0].getVersion());
    } catch (Exception e) {
        // $NON-NLS-1$
        fail("Unexpected exception", e);
    }
}
Also used : ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) URL(java.net.URL)

Example 20 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project motech by motech.

the class ImportExportResolver method resolveBundleWiring.

/**
 * Resolves the bundle wiring for the bundle represented by {@link ExtendedBundleInformation} by creating
 * {@link PackageInfo} objects. These objects represent an OSGi import containing information about both of the
 * involved parties - the importer and the exporter.
 *
 * @param bundleInfo the object representing the bundle, it will be filled with the import information. The
 *                   bundle is resolved based on the bundle ID returned by
 *                   {@link org.motechproject.admin.bundles.ExtendedBundleInformation#getBundleId()}
 * @see PackageInfo
 */
public void resolveBundleWiring(ExtendedBundleInformation bundleInfo) {
    List<PackageInfo> imports = new ArrayList<>();
    List<PackageInfo> exports = new ArrayList<>();
    Bundle bundle = bundleContext.getBundle(bundleInfo.getBundleId());
    ExportedPackage[] allExportedPackages = packageAdmin.getExportedPackages((Bundle) null);
    for (ExportedPackage exportedPackage : allExportedPackages) {
        if (isImportedByBundle(exportedPackage, bundle)) {
            PackageInfo importInfo = new PackageInfo(exportedPackage);
            imports.add(importInfo);
        }
        if (exportedPackage.getExportingBundle().equals(bundle)) {
            PackageInfo exportInfo = new PackageInfo(exportedPackage);
            exports.add(exportInfo);
        }
    }
    bundleInfo.setBundleExports(exports);
    bundleInfo.setBundleImports(imports);
}
Also used : Bundle(org.osgi.framework.Bundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) ArrayList(java.util.ArrayList)

Aggregations

ExportedPackage (org.osgi.service.packageadmin.ExportedPackage)54 Bundle (org.osgi.framework.Bundle)40 PackageAdmin (org.osgi.service.packageadmin.PackageAdmin)21 ArrayList (java.util.ArrayList)19 RequiredBundle (org.osgi.service.packageadmin.RequiredBundle)14 Test (org.junit.Test)9 HashMap (java.util.HashMap)7 List (java.util.List)7 Version (org.osgi.framework.Version)6 LinkedHashSet (java.util.LinkedHashSet)4 TreeMap (java.util.TreeMap)4 Expectations (org.jmock.Expectations)4 BundleContext (org.osgi.framework.BundleContext)4 ServiceReference (org.osgi.framework.ServiceReference)4 File (java.io.File)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 BundleCapability (org.osgi.framework.wiring.BundleCapability)3