带有 ds 注释的一个目标服务的多个 PID

Multiple PIDs for one target service with ds annotations

我想知道是否可以利用声明性服务注释的 osgi 企业规范的 par.104.7.5(使用多位置)中解释的内容。引用规范:

It is also possible that Bundles are interested in multiple PIDs for one target service, for this reason they can register multiple PIDs for one service. [...]

A Bundle interested in the host configuration would register a Managed Service with the following properties:

service.pid = [ "com.acme.host", "com.acme.system" ]

The Bundle would be called back for both the com.acme.host and com.acme.system PID and must therefore discriminate between these two cases. This Managed Service therefore would have a callback like:

volatile URL url;
public void updated( Dictionary d ) {
if ( d.get("service.pid").equals("com.acme.host"))
    this.url = new URL( d.get("host"));
if ( d.get("service.pid").equals("com.acme.system"))
   ...
}

我尝试使用以下语法:

@Component(
    immediate = true,
    configurationPid = "[com.mycompany.ws.rest,com.mycompany.endpoints]",
    configurationPolicy = ConfigurationPolicy.REQUIRE
) 
public class TestImpl implements Test {
    // ...
}

但这失败了。当然,人们可以获得对配置管理员的引用并根据所需的 pid 浏览配置,但这对我来说似乎有点不雅,因为理论上可以将其委托给 ds 注释。

可能吗?正确的语法是什么?

谢谢!

我认为使用 configurationPidconfigurationPolicy 值是不可能的。我所做的是:

  • 将服务工厂 pid 定义为服务 属性。
  • 实现ManagedService接口。

示例:

@Component(property = {Constants.SERVICE_PID + "=com.acme.host", 
                       Constants.SERVICE_PID + "=com.acme.system"})
public class TestComponent implements ManagedService {

    @Override
    public void updated(Dictionary<String, ?> dict) {
    ...
    }

当然这有一个缺点,即即使没有配置你的组件也会被激活,但是你可以使用两个 PID。

即将推出的 DS 1.3 规范支持多个 PID。有关下载第 6 版规范草案的链接,请参阅 http://www.osgi.org/Specifications/Drafts

较新的版本解决了这个问题...

发件人:https://osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#org.osgi.service.component.annotations.Component

112.13.4.11 String[] configurationPid default "$" □ The configuration PIDs for the configuration of this Component.

Each value specifies a configuration PID for this Component.

If no value is specified, the name of this Component is used as the configuration PID of this Component.

A special string ("$") can be used to specify the name of the component as a configuration PID. The NAME constant holds this special string. For example:

@Component(configurationPid={"com.acme.system", Component.NAME}) Tools creating a Component Description from this annotation must replace the special string with the actual name of this Component.

See Also The configuration-pid attribute of the component element of a Component Description.

Since 1.2