Konstantin Komissarchik
Konstantin Komissarchik

Reputation: 29139

What is pure OSGi equvalent for Eclipse Platform.getBundle()

What is the pure OSGi equivalent to the following Eclipse platform call:

org.eclipse.core.runtime.Platform.getBundle( [bundle-id] ) -> Bundle

Upvotes: 2

Views: 3742

Answers (3)

Raimar
Raimar

Reputation: 62

Since OSGi 1.6 there exists also the method BundleContext.getBundle(String), if you have already a BundleContext.

Upvotes: -2

Angelo van der Sijpt
Angelo van der Sijpt

Reputation: 3641

There is no direct equivalent of getBundle(String symbolicName), and plain OSGi does not have static helpers like this, because there may be more than one framework in a VM.

You can, as Amir points out, use getBundle(long id) to get a bundle if you know it's ID.

If you want the bundle with a given symbolic name, in the highest version, you can do something like (assuming you have a BundleContext available),

Bundle getBundle(BundleContext bundleContext, String symbolicName) {
    Bundle result = null;
    for (Bundle candidate : bundleContext.getBundles()) {
        if (candidate.getSymbolicName().equals(symbolicName)) {
            if (result == null || result.getVersion().compareTo(candidate.getVersion()) < 0) {
                result = candidate;
            }
        }
    }
    return result;
}

If you don't have a BundleContext available for some reason (and I guess those will be rare cases), you can try to find one by using FrameworkUtil,

FrameworkUtil.getBundle(getClass()).getBundleContext()

by which you can get the Bundle that loaded a given class, even for fragments.

Upvotes: 7

Related Questions