没有托管服务或工厂的OSGi捆绑包配置

没有托管服务或工厂的OSGi捆绑包配置,osgi,Osgi,Neil Bartlett的文章展示了在不使用托管服务或托管工厂的情况下为bundle设置配置的方法 搜索实际为此方法设置配置的示例,指向felix file install或使用托管服务的示例 在回答问题时,Neil Bartlett表示“请注意,DS从未为您的组件实际创建ManagedService或ManagedServiceFactory。它通过使用ConfigurationListener侦听Config Admin来工作。但是内部详细信息并不重要……只需创建PID/factoryPI

Neil Bartlett的文章展示了在不使用托管服务或托管工厂的情况下为bundle设置配置的方法

搜索实际为此方法设置配置的示例,指向felix file install或使用托管服务的示例

在回答问题时,Neil Bartlett表示“请注意,DS从未为您的组件实际创建ManagedService或ManagedServiceFactory。它通过使用ConfigurationListener侦听Config Admin来工作。但是内部详细信息并不重要……只需创建PID/factoryPID与component.name和它匹配的配置”“只是工作”

我认为这项技术需要在配置字典中放置一个pid条目,但我不知道如何在配置管理中使用它


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

我知道问题提出已经有一段时间了,但我在尝试使用声明性服务创建类似于
ManagedServiceFactory
的组件时遇到了同样的问题。因此,我想分享我的解决方案。也许其他人会找到我们我的问题是这样的:

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

首先,我尝试处理
@Component
的属性
工厂
配置PID
,但所有这些都不需要,甚至返回错误的结果(maven插件中的felix annotation processor在处理配置PID时似乎有一个bug)

我提出的解决方案是:

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) {
        // ...  
    }
}
部署时,会自动在配置文件夹中创建文件夹结构,如
com/example/my/component
,其中包含以下文件:

factory.config
内容:

factory.pid="com.example.my.component"
factory.pidList=[ \
  "com.example.my.component.525ca4fb-2d43-46f3-b912-8765f639c46f", \
  ]
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"

内容:

factory.pid="com.example.my.component"
factory.pidList=[ \
  "com.example.my.component.525ca4fb-2d43-46f3-b912-8765f639c46f", \
  ]
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).

Neil Bartlett的书清单9.15提供了实现所需结果的方法。我对配置字典中的pid条目感到困惑,因为我看到了包含pid条目的配置字典的示例。当设置新配置时,config Admin会将pid条目添加到字典本身。