开发者

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

开发者 https://www.devze.com 2023-03-03 15:11 出处:网络
What is the开发者_JAVA百科 pure OSGi equivalent to the following Eclipse platform call: org.eclipse.core.runtime.Platform.getBundle( [bundle-id] ) -> BundleThere is no direct equivalent of 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

精彩评论

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

关注公众号