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()]);
}
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();
}
}
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();
}
}
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;
}
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;
}
Aggregations