【问题标题】:How to get a content of config file for some OSGI Bundle如何获取某些 OSGI Bundle 的配置文件内容
【发布时间】:2020-11-03 14:39:58
【问题描述】:

我有以下结构:

-- bundles
---- nicebundle
------ src
-------- ...
-------- nicebundle.config
---------- NiceConfig.java
-------- ...
----  mybundle
------ ...
----  another bundles...

-- features
----  nicebundle.config.feature
------  rootifles
--------  config.cfg
------  ...
----  another features...

在config.cfg 中有一些常量,我想在mybundle 中使用它们(在activate() 方法中)。我在NiceConfig.java 中为nicebundle 拥有的是NiceConfig @interface,它只是镜像列出了.cfg 文件中的所有内容,如String importantString();,并且在.cfg 文件中有相应的记录:importantString=Hello

对于nicebundle,您只需加载NiceConfig 对象,调用方法并接收所需的常量,例如:

@Activate
public void activate(final NiceConfig niceConfig) {
    String compositeString = niceConfig.importantString() + " World"; // receive "Hello World"
    ...

我想要的 - 做同样的事情,但在 mybundle。但是,当我这样做时,niceConfig.importantString() 返回 null,而不是“Hello”。我想我错过了一些非常简单和明显的东西,但我的问题是:如何实现 .cfg 文件的内容,位于我上面在mybundle 中指定的方式(我无法更改)?

【问题讨论】:

    标签: java osgi bundle config


    【解决方案1】:

    简而言之,对我来说正确的答案是执行以下步骤:

    1. 在方括号中的 .cfg-File 中指定新的 PID,例如[VeryNiceConfig] 然后在这个方括号行之后指定我的所有参数,例如VeryNiceInt=42

    2. 将configurationPid = "VeryNiceConfig", configurationPolicy = ConfigurationPolicy.REQUIRE 添加到@Component 注释中。

    3. 然后将我的 Config @interface 对象作为参数添加到我的 bundle 的 activate() 方法中,并将其用于荣耀。

    这在eclipse中不起作用,因为.cfg-File是通过该功能添加的,并且没有直接注入eclipse.exe,但它在(在我的情况下)maven build之后起作用。

    【讨论】:

      【解决方案2】:

      声明式服务 (DS) 会将 component properties 强制转换为注释类型,例如您的 NiceConfig 类型。但是组件属性来自component description和configurations in Configuration Admin。

      DS 对 Eclipse 功能中的某些配置文件一无所知。而且 Eclipse 功能甚至没有安装包,因此在运行时无论如何都不可见。

      您可以使用 Configurator specification 的实现将捆绑包中资源的属性配置到 DS 组件的相应配置管理配置中。但这需要一些结构更改才能将配置文件从功能移动到您的包中。

      【讨论】:

      • 非常感谢您的回答。这间接地让我找到了正确的答案!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-17
      • 2015-08-28
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      相关资源
      最近更新 更多