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> originRevisions =
bundle.adapt(BundleRevisions.class).getRevisions();
for (int i = originRevisions.size() - 1; i >= 0; i--)
{
BundleRevision originBr = originRevisions.get(i);
List<BundleRevision> revisions = Collections.singletonList(originBr);
if ((originBr.getTypes() & BundleRevision.TYPE_FRAGMENT) != 0)
{
// If it's a fragment, find the revisions of the attached
// bundle(s) and work with those instead. Note that fragments
// can be attached to multiple hosts...
revisions = new ArrayList<BundleRevision>();
for (BundleWire bw : originBr.getWiring().getRequiredWires(HostNamespace.HOST_NAMESPACE))
{
revisions.add(bw.getProviderWiring().getRevision());
}
}
for (BundleRevision br : revisions)
{
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) br.getBundle(), br, cap));
}