如何获取Eclipse RCP应用程序的OSGi BundleContext? [英] How do I get the OSGi BundleContext for an Eclipse RCP application?
问题描述
我刚刚开始使用Eclipse RCP应用程序,它基本上只是提供的hello world示例之一。
I have just gotten started with an Eclipse RCP application, it is basically just one of the provided "hello world" samples.
当应用程序启动时,我想查看我的命令行参数并根据它们启动一些服务。我可以在 IApplication.start 中获取命令行参数:
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:
public Object start(IApplicationContext context) {
String[] argv = (String[])
context.getArguments().get(IApplicationContext.APPLICATION_ARGS)));
}
但是如何获得BundleContext,以便我可以注册服务?它似乎不在IApplicationContext中。
But how do I get the BundleContext, so that I can register services? It does not seem to be in the IApplicationContext.
推荐答案
棘手的内部方式:
InternalPlatform.getDefault().getBundleContext()
可以做到。
你会在此类
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();
每个插件-in 可以访问自己的bundle上下文。
Every plug-in has access to its own bundle context.
只需确保你的插件类覆盖 start(BundleContext)方法。然后,您可以将其保存到插件中可以轻松访问的地方类
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.
这篇关于如何获取Eclipse RCP应用程序的OSGi BundleContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!