ExportedPackage[] getExportedPackages(String pkgName)
{
// First, get all exporters of the package.
Map<String, Object> attrs = Collections.singletonMap(
BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName);
BundleRequirementImpl req = new BundleRequirementImpl(
null,
BundleRevision.PACKAGE_NAMESPACE,
Collections.EMPTY_MAP,
attrs);
List<BundleCapability> exports = m_resolver.findProviders(req, false);
// We only want resolved capabilities.
for (Iterator<BundleCapability> it = exports.iterator(); it.hasNext(); )
{
if (it.next().getRevision().getWiring() == null)
{
it.remove();
}
}
if (exports != null)
{
List pkgs = new ArrayList();
for (Iterator<BundleCapability> it = exports.iterator(); it.hasNext(); )
{
// Get the bundle associated with the current exporting revision.
Bundle bundle = it.next().getRevision().getBundle();
// We need to find the version of the exported package, but this
// is tricky since there may be multiple versions of the package
// offered by a given bundle, since multiple revisions of the
// bundle JAR file may exist if the bundle was updated without
// refreshing the framework. In this case, each revision of the
// bundle JAR file is represented as a revision ordered from
// newest to oldest. We assume that the first revision found to
// be exporting the package is the provider of the package,
// which makes sense since it must have been resolved first.
List<BundleRevision> revisions =
bundle.adapt(BundleRevisions.class).getRevisions();
for (int i = revisions.size() - 1; i >= 0; i--)
{
BundleRevision br = revisions.get(i);
List<BundleCapability> caps = (br.getWiring() == null)
? br.getDeclaredCapabilities(null)
: br.getWiring().getCapabilities(null);
for (BundleCapability cap : caps)
{
if (cap.getNamespace().equals(req.getNamespace())
&& CapabilitySet.matches(cap, req.getFilter()))
{
pkgs.add(
new ExportedPackageImpl(
this, (BundleImpl) bundle, br, cap));
}