【问题标题】:How do I get the OSGi BundleContext for an Eclipse RCP application?如何获取 Eclipse RCP 应用程序的 OSGi BundleContext?
【发布时间】:2010-10-08 06:45:52
【问题描述】:

我刚刚开始使用 Eclipse RCP 应用程序,它基本上只是提供的“hello world”示例之一。

当应用程序启动时,我想查看我的命令行参数并根据它们启动一些服务。我可以在IApplication.start中获取命令行参数:

public Object start(IApplicationContext context) {
   String[] argv = (String[]) 
       context.getArguments().get(IApplicationContext.APPLICATION_ARGS)));
}

但是如何获取BundleContext,以便注册服务呢?它似乎不在 IApplicationContext 中。

【问题讨论】:

    标签: java osgi eclipse-rcp


    【解决方案1】:

    棘手的内部方式:

    InternalPlatform.getDefault().getBundleContext()
    

    可以做到。

    你会在this class找到一个例子

    public class ExportClassDigestApplication implements IApplication {
    
        public Object start(IApplicationContext context) throws Exception {
            context.applicationRunning();
    
            List<ExtensionBean> extensionBeans = ImpCoreUtil.loadExtensionBeans(&quot;com.xab.core.containerlaunchers&quot;);
            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();
    

    Proper way:

    每个plug-in 都可以访问自己的包上下文。

    只要确保您的插件类覆盖start(BundleContext) 方法即可。然后您可以将其保存到您的插件中可以轻松访问的地方类

    请注意,提供给插件的包上下文是特定于它的,不应与其他插件共享。

    【讨论】:

    • 但是 start 方法本身需要一个包上下文:你会在哪里喂你的 BundleActivator ?我可以从FrameworkUtil 获取它,但是(在我的情况下)是null 所以......否则你在清单中声明你的激活器,所以我得到一个捆绑上下文......但是它是如何给它的? :)
    • @Campa 不确定:那是 6 多年前的事了,我再也无法访问那种项目了。不过,您可以提出一个新问题,并提供指向此问题的链接。
    • InternalPlatform.getDefault().getCommandLineArgs();帮助我获得了 eclipse 无头构建命令行参数。
    【解决方案2】:

    刚刚在进行网络搜索时发现了这一点,并认为我会推广新的标准 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();
        }
    }
    

    【讨论】:

    • 在此注意,在某些 OSGi 实现中,捆绑包的捆绑包上下文可能不存在。它不需要在那里。
    • @Francis:这是一个幸运儿!我的包没有提供上下文 (null)。其他选择? (内部棘手的 hack 不好)
    • 似乎声明Bundle-Activator 的插件将设置其上下文,否则FrameworkUtil-way 将返回null
    • 即使有这个内部黑客,如果你想在你的类中注入一个 Eclipse IEventBroker 也会有麻烦:@see Unable to process "EventBroker.logger" 错误。
    猜你喜欢
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多