Hello,
Thankyou for that example, it got me on the right track. It wasn't exactly
what I required, because the "provided wires" seems to be what's used on an
active bundle; in my case, I just scanned inactive bundles.
Here's what works for me, FWIW and hoping it might help others (I'm
obviously open to any comments on this):
private Version getClassVersion(final Class c, final Bundle b)
{
final Package p = c.getPackage();
final String pkgName = p.getName();
// query package directly first
final String sv = p.getSpecificationVersion();
if (sv != null && !sv.isEmpty())
{
try
{
return new Version(sv);
}
catch (RuntimeException e)
{
_log.warn(format("Cannot parse \"Specification-Version: %s\" as OSGi
version for package \"%s\".", sv, pkgName));
}
}
// query package admin next
if (b != null)
{
final BundleWiring bw = b.adapt(BundleWiring.class);
final List<Capability> capabilities =
bw.getResourceCapabilities(PackageNamespace.PACKAGE_NAMESPACE);
for (final Capability capability : capabilities)
{
final Map<String, Object> attributes = capability.getAttributes();
final String exportPkgName =
(String)attributes.get(PackageNamespace.PACKAGE_NAMESPACE);
if (pkgName.equals(exportPkgName))
{
final Version exportPkgVersion =
(Version)attributes.get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
if (exportPkgVersion != null) return exportPkgVersion;
}
}
}
// give up and return default
_log.warn(format("Cannot determine OSGi version for package \"%s\", using
default.", pkgName));
return Version.emptyVersion;
}
Of note, I used constants from PackageNamespace instead of BundleRevision.
Seemed a better fit.
Incidentally, I also scan JARs not packaged with bundle manifest headers
(that's the bit with use of Java's standard "package" API).
--
Christopher
On 11 August 2014 16:57, Chetan Mehrotra <[email protected]> wrote:
> Following code has worked [1] for me so far. Might be useful in your
> usecase
>
> BundleWiring bw = b.adapt(BundleWiring.class)
> List<BundleWire> wires =
> bw.getProvidedWires(BundleRevision.PACKAGE_NAMESPACE)
>
> for(BundleWire w : wires){
> String pkgName =
>
> (String)w.getCapability().getAttributes().get(BundleRevision.PACKAGE_NAMESPACE)
> }
>
> Chetan Mehrotra
> [1] https://gist.github.com/chetanmeh/10971530
>
>
> On Mon, Aug 11, 2014 at 8:02 PM, Christopher BROWN <[email protected]>
> wrote:
> > Hello,
> >
> > I'm upgrading some code that used PackageAdmin with Felix 3.2.2 to Felix
> > 4.4.1 and am trying to find the equivalent code that can (for a given
> > class) determine a version number, if a bundle exports that class'
> package.
> > The code, which works with PackageAdmin, is like this (I've simplified):
> >
> > // b is a bundle, in a loop:
> > // for (Bundle b : bundleContext.getBundles())
> > //
> > // c is the class of a service interface, in a loop:
> > //
> > Package p = c.getPackage();
> > String packageName = p.getName();
> > ExportedPackage[] exports = packageAdmin.getExportedPackages(b);
> > if (exports != null)
> > {
> >
> > for (ExportedPackage export : exports)
> > {
> >
> > if (packageName.equals(export.getName())) return export.getVersion();
> >
> > }
> >
> > }
> >
> > I'm a bit stuck finding the equivalent code with the "bundlewiring"
> > package, which is apparently the equivalent in OSGi R5. I can't find
> > anything as obvious as "packages", it's all wires and requirements and
> > capabilities, which seem promising but I can't see how to just get at the
> > packages.
> >
> > Thanks in advance for advice,
> > Christopher
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>