开发者

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

What is the开发者_JAVA百科 pure OSGi equivalent to the following Eclipse platform call:

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


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.


In plain OSGi you can use PackageAdmin service org.osgi.service.packageadmin.PackageAdmin.getBundles(String, String)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜