Search in sources :

Example 36 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.

the class Felix method getExportedPackages.

/**
 * Returns an array of all actively exported packages from the specified
 * bundle or if the specified bundle is <tt>null</tt> an array
 * containing all actively exported packages by all bundles.
 *
 * @param b The bundle whose exported packages are to be retrieved
 *        or <tt>null</tt> if the exported packages of all bundles are
 *        to be retrieved.
 * @return An array of exported packages.
 */
ExportedPackage[] getExportedPackages(Bundle b) {
    List list = new ArrayList();
    // exported packages.
    if (b != null) {
        BundleImpl bundle = (BundleImpl) b;
        getExportedPackages(bundle, list);
    } else // Otherwise return all exported packages.
    {
        // To create a list of all exported packages, we must look
        // in the installed and uninstalled sets of bundles.
        boolean locked = acquireGlobalLock();
        if (!locked) {
            // be able to get the global lock.
            throw new IllegalStateException("Unable to acquire global lock to retrieve exported packages.");
        }
        try {
            // First get exported packages from uninstalled bundles.
            for (int bundleIdx = 0; (m_uninstalledBundles != null) && (bundleIdx < m_uninstalledBundles.size()); bundleIdx++) {
                BundleImpl bundle = m_uninstalledBundles.get(bundleIdx);
                getExportedPackages(bundle, list);
            }
            // Now get exported packages from installed bundles.
            Bundle[] bundles = getBundles();
            for (int bundleIdx = 0; bundleIdx < bundles.length; bundleIdx++) {
                BundleImpl bundle = (BundleImpl) bundles[bundleIdx];
                getExportedPackages(bundle, list);
            }
        } finally {
            releaseGlobalLock();
        }
    }
    return (list.isEmpty()) ? null : (ExportedPackage[]) list.toArray(new ExportedPackage[list.size()]);
}
Also used : Bundle(org.osgi.framework.Bundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 37 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.

the class PackageAdminImplTest method testExportedPackages.

public void testExportedPackages() throws Exception {
    String bmf = "Bundle-SymbolicName: pkg.bundle\n" + "Bundle-Version: 1\n" + "Bundle-ManifestVersion: 2\n" + "Export-Package: org.foo.bundle\n";
    File bundleFile = createBundle(bmf);
    String fmf = "Bundle-SymbolicName: pkg.frag\n" + "Bundle-Version: 1\n" + "Fragment-Host: pkg.bundle\n" + "Bundle-ManifestVersion: 2\n" + "Export-Package: org.foo.fragment;version=\"2.0.0\"\n";
    File fragFile = createBundle(fmf);
    Bundle b = felix.getBundleContext().installBundle(bundleFile.toURI().toASCIIString());
    Bundle f = felix.getBundleContext().installBundle(fragFile.toURI().toASCIIString());
    b.start();
    try {
        PackageAdminImpl pa = new PackageAdminImpl(felix);
        assertEquals(b, pa.getExportedPackage("org.foo.bundle").getExportingBundle());
        assertEquals(b, pa.getExportedPackage("org.foo.fragment").getExportingBundle());
        Set<String> expected = new HashSet<String>();
        expected.addAll(Arrays.asList("org.foo.bundle", "org.foo.fragment"));
        Set<String> actual = new HashSet<String>();
        for (ExportedPackage ep : pa.getExportedPackages(b)) {
            actual.add(ep.getName());
            assertEquals(b, ep.getExportingBundle());
        }
        assertEquals(expected, actual);
        ExportedPackage[] bundlePkgs = pa.getExportedPackages("org.foo.bundle");
        assertEquals(1, bundlePkgs.length);
        assertEquals(b, bundlePkgs[0].getExportingBundle());
        assertEquals(new Version("0"), bundlePkgs[0].getVersion());
        ExportedPackage[] fragPkgs = pa.getExportedPackages("org.foo.fragment");
        assertEquals(1, fragPkgs.length);
        assertEquals("The fragment package should be exposed through the bundle", b, fragPkgs[0].getExportingBundle());
        assertEquals(new Version("2"), fragPkgs[0].getVersion());
    } finally {
        b.stop();
        b.uninstall();
        f.uninstall();
    }
}
Also used : Version(org.osgi.framework.Version) Bundle(org.osgi.framework.Bundle) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) File(java.io.File) HashSet(java.util.HashSet)

Example 38 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.

the class InspectCommandImpl method _printImportedPackages.

private void _printImportedPackages(Bundle bundle, PrintStream out, PrintStream err) {
    // Get package admin service.
    PackageAdmin pa = getPackageAdmin();
    if (pa == null) {
        out.println("PackageAdmin service is unavailable.");
    } else {
        ExportedPackage[] exports = pa.getExportedPackages((Bundle) null);
        String title = bundle + " imports packages:";
        out.println(title);
        out.println(Util.getUnderlineString(title));
        boolean found = false;
        for (int expIdx = 0; expIdx < exports.length; expIdx++) {
            Bundle[] importers = exports[expIdx].getImportingBundles();
            for (int impIdx = 0; (importers != null) && (impIdx < importers.length); impIdx++) {
                if (importers[impIdx] == bundle) {
                    out.println(exports[expIdx] + " -> " + exports[expIdx].getExportingBundle());
                    found = true;
                }
            }
        }
        if (!found) {
            out.println("Nothing");
        }
        ungetPackageAdmin();
    }
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle)

Example 39 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.

the class WebConsolePlugin method getPackageInfo.

@SuppressWarnings("deprecation")
private static final Map<String, Object> getPackageInfo(String packageName, PackageAdmin pa, Set<Bundle> exportingBundles) {
    final Map<String, Object> ret = new HashMap<String, Object>();
    final ExportedPackage[] exports = pa.getExportedPackages(packageName);
    for (int i = 0; exports != null && i < exports.length; i++) {
        final ExportedPackage x = exports[i];
        @SuppressWarnings("unchecked") List<Object> // $NON-NLS-1$
        el = (List<Object>) ret.get("exporters");
        if (el == null) {
            el = new ArrayList<Object>();
            // $NON-NLS-1$
            ret.put("exporters", el);
        }
        el.add(toJSON(x));
        exportingBundles.add(x.getExportingBundle());
    }
    // $NON-NLS-1$
    ret.put("name", packageName);
    return ret;
}
Also used : HashMap(java.util.HashMap) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) ArrayList(java.util.ArrayList) List(java.util.List)

Example 40 with ExportedPackage

use of org.osgi.service.packageadmin.ExportedPackage in project felix by apache.

the class WebConsolePlugin method doFindDuplicates.

@SuppressWarnings("deprecation")
private static final Collection<Object> doFindDuplicates(final Map<String, Set<ExportedPackage>> exports) {
    final List<Object> ret = new ArrayList<Object>();
    for (Iterator<Entry<String, Set<ExportedPackage>>> entryIter = exports.entrySet().iterator(); entryIter.hasNext(); ) {
        Entry<String, Set<ExportedPackage>> exportEntry = entryIter.next();
        Set<ExportedPackage> exportSet = exportEntry.getValue();
        if (exportSet.size() > 1) {
            final Map<String, Object> container = new HashMap<String, Object>();
            ret.add(container);
            for (Iterator<ExportedPackage> packageIter = exportSet.iterator(); packageIter.hasNext(); ) {
                ExportedPackage exportedPackage = packageIter.next();
                final Map<String, Object> json = toJSON(exportedPackage);
                // $NON-NLS-1$
                container.put("name", exportedPackage.getName());
                @SuppressWarnings("unchecked") List<Object> // $NON-NLS-1$
                imps = (List<Object>) container.get("entries");
                if (imps == null) {
                    imps = new ArrayList<Object>();
                    // $NON-NLS-1$
                    container.put("entries", imps);
                }
                imps.add(json);
            }
        }
    }
    return ret;
}
Also used : SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Entry(java.util.Map.Entry) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) ArrayList(java.util.ArrayList) List(java.util.List)

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