gpt4 book ai didi

java - Eclipse Platform.getBundle() 的纯 OSGi 等价物是什么

转载 作者:行者123 更新时间:2023-11-30 06:35:37 24 4
gpt4 key购买 nike

什么是等同于以下 Eclipse 平台调用的纯 OSGi:

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

最佳答案

没有直接等价于 getBundle(String symbolicName) ,而普通的 OSGi 没有这样的静态帮助器,因为一个 VM 中可能有多个框架。

正如 Amir 指出的那样,您可以使用 getBundle(long id)如果你知道它的 ID,就可以得到一个包。

如果您想要具有给定符号名称的包,在最高版本中,您可以执行类似的操作(假设您有可用的 BundleContext),

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;
}

如果你因为某种原因没有可用的BundleContext(我想这种情况很少见),你可以尝试使用FrameworkUtil 找到一个。 ,

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

通过它您可以获得加载给定类的 Bundle,甚至是片段。

关于java - Eclipse Platform.getBundle() 的纯 OSGi 等价物是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5892341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com