java 如何获取 Eclipse RCP 应用程序的 OSGi BundleContext?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/559989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the OSGi BundleContext for an Eclipse RCP application?
提问by Thilo
I have just gotten started with an Eclipse RCP application, it is basically just one of the provided "hello world" samples.
我刚刚开始使用 Eclipse RCP 应用程序,它基本上只是提供的“hello world”示例之一。
When the application boots up, I would like to look at my command-line parameters and start some services according to them. I can get the command-line parameters in IApplication.start:
当应用程序启动时,我想查看我的命令行参数并根据它们启动一些服务。我可以在IApplication.start 中获取命令行参数:
public Object start(IApplicationContext context) {
String[] argv = (String[])
context.getArguments().get(IApplicationContext.APPLICATION_ARGS)));
}
But how do I get the BundleContext, so that I can register services? It does not seem to be in the IApplicationContext.
但是我如何获得 BundleContext,以便我可以注册服务?它似乎不在 IApplicationContext 中。
回答by Anthony Juckel
Just came across this doing a web search, and thought I'd promote the new standard OSGi R4.2 way (as provided by Equinox shipped with Eclipse 3.5). If you don't have an activator, and don't want to create one just to cache the bundle context, you can use FrameworkUtil.getBundle. Modifying the previous example:
刚刚在网上搜索时遇到了这个问题,并认为我会推广新的标准 OSGi R4.2 方式(由 Eclipse 3.5 附带的 Equinox 提供)。如果您没有激活器,并且不想创建一个只是为了缓存包上下文,您可以使用 FrameworkUtil.getBundle。修改前面的例子:
import org.osgi.framework.FrameworkUtil;
public class ExportClassDigestApplication implements IApplication {
public Object start(IApplicationContext context) throws Exception {
context.applicationRunning();
BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
.getBundleContext();
}
}
回答by VonC
Tricky internal way:
棘手的内部方式:
InternalPlatform.getDefault().getBundleContext()
could do it.
能做到这。
You will find an example in this class
您将在本课程中找到一个示例
public class ExportClassDigestApplication implements IApplication {
public Object start(IApplicationContext context) throws Exception {
context.applicationRunning();
List<ExtensionBean> extensionBeans = ImpCoreUtil.loadExtensionBeans("com.xab.core.containerlaunchers");
for (ExtensionBean bean : extensionBeans) {
ILauncher launcher = (ILauncher) bean.getInstance();
launcher.start();
}
ClassFilter classFilter = new ClassFilter() {
public boolean isClassAccepted(Class clz) {
return true;
}
};
PrintWriter writer = new PrintWriter( new File( "C:/classes.csv"));
Bundle[] bundles = InternalPlatform.getDefault().getBundleContext().getBundles();
Every plug-inhas access to its own bundle context.
Just make sure your plug-in class overrides the start(BundleContext)method. You can then save it to a place classes in your plug-in can easily access
Note the bundle context provided to a plug-in is specific to it and should never be shared with other plug-ins.
每个插件都可以访问自己的包上下文。
只需确保您的插件类覆盖了start(BundleContext)方法。然后你可以把它保存到一个地方,你的插件类可以很容易地访问
请注意,提供给插件的捆绑上下文是特定于它的,不应与其他插件共享。

