【问题标题】:OSGi bundle config without managed service or factory没有托管服务或工厂的 OSGi 捆绑配置
【发布时间】:2013-02-23 21:36:48
【问题描述】:

Neil Bartlett 的文章 http://njbartlett.name/2010/07/19/factory-components-in-ds.html 展示了在不使用托管服务或托管工厂的情况下为捆绑软件设置配置的方法。

搜索实际设置此方法配置的示例,指向 felix 文件安装或使用托管服务的示例。

在回答OSGi Declarative Services vs. ManagedService for configuring service? Neil Bartlett 的问题时说:“请注意,DS 从未真正为您的组件创建 ManagedService 或 ManagedServiceFactory。它通过使用 ConfigurationListener 侦听 Config Admin 来工作。但是内部细节并不重要……只是使用与 component.name 匹配的 PID/factoryPID 创建配置,并且它“正常工作”

我认为该技术涉及在配置字典中放置一个 pid 条目,但我不知道这将如何与配置管理员一起使用。

有关如何使用此方法设置配置的指南或简单示例将非常有帮助。

【问题讨论】:

  • Neil Bartlett 的书 njbartlett.name/files/osgibook_preview_20091217.pdf 清单 9.15 提供了实现所需结果的方法。
  • 我对配置字典中的 pid 条目感到困惑是因为我看到了包含 pid 条目的配置字典的示例。设置新配置时,配置管理员将 pid 条目添加到字典本身。

标签: osgi


【解决方案1】:

我知道这个问题已经有一段时间了,但是在尝试使用声明式服务创建类似 ManagedServiceFactory 的组件时遇到了同样的问题。所以我想分享我的解决方案。也许其他人觉得它有用。我的问题是这样的:

我已经定义了一个组件(用@Component 注释)。在我使用 felix 文件安装添加的每个配置中,我想要使用给定配置创建并立即激活该组件的实例。

首先我尝试弄乱@Component的属性factory和configurationPid,但这一切都不需要,甚至返回错误的结果(maven插件中的felix注释处理器在处理configurationPid时似乎有一个错误) .

我想出的解决方案:

package com.example.my;

@Component(
    name = MyExampleComponent.FACTORY_PID,
    configurationPolicy = ConfigurationPolicy.REQUIRE,
    property = {"abc=", "exampleProp="}
)
public class MyExampleComponent {

    public static final String FACTORY_PID = "com.example.my.component";

    @Activate
    protected void activate(BundleContext context, Map<String,Object> map) {
        // ...  
    }
}

然后我为 felix 文件安装创建了一个名为 com.example.my.component-test1.cfg 的配置文件:

abc = Hello World
exampleProp = 123

部署后,它会自动在配置文件夹中创建一个文件夹结构,例如 com/example/my/component,其中包含文件:

factory.config

内容:

factory.pid="com.example.my.component"
factory.pidList=[ \
  "com.example.my.component.525ca4fb-2d43-46f3-b912-8765f639c46f", \
  ]

.

525ca4fb-2d43-46f3-b912-8765f639c46f.config

内容:

abc="Hello World"
exampleProp="123"
felix.fileinstall.filename="file:/..._.cfg"
service.factoryPid="com.example.my.component"
service.pid="com.example.my.component.525ca4fb-2d43-46f3-b912-8765f639c46f"

525ca4fb-2d43-46f3-b912-8765f639c46f 似乎是一些随机生成的 ID(可能是 UUID)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    • 2015-08-11
    • 2019-11-18
    • 1970-01-01
    相关资源
    最近更新 更多