使用 OSGi 声明性服务获取服务实现列表

发布于 2024-12-08 08:25:11 字数 2116 浏览 1 评论 0原文

我有一个非常简单的声明性服务示例。我正在关注本教程 http://www.eclipsezone.com/eclipse /forums/t97690.html?start=0。一切都按预期进行。但是,我无法弄清楚如何使“SampleImporter”(这是预期使用其他捆绑包服务的捆绑包)了解“SampleExporter”(提供服务的捆绑包)列表。换句话说,我希望“SamlpeImporter”查看它最终使用的包的 ID。这些信息对我的申请非常有用。

这是 SampleExporter 的 XML 文件:

<?xml version="1.0"?>
<component name="samplerunnable">
<implementation class="org.example.ds.SampleRunnable"/>
<property name="ID" value="expoter" />
<service>
<provide interface="java.lang.Runnable"/>
</service>

而对于 SampleImporter:

<?xml version="1.0"?>
<component name="commandprovider1">
<implementation class="org.example.ds.SampleCommandProvider1"/>
<service>
<provide interface="org.eclipse.osgi.framework.console.CommandProvider"/>
</service>
<reference name="RUNNABLE"
    interface="java.lang.Runnable"
    bind="setRunnable"
    unbind="unsetRunnable"
    cardinality="0..1"
    policy="dynamic"/>
</component>

在导入器方面,我有以下函数:

public class SampleCommandProvider1 implements CommandProvider {
    private Runnable runnable;
public synchronized void setRunnable(Runnable r) {
    runnable = r;
}
public synchronized void unsetRunnable(Runnable r) {
    runnable = null;
}
public synchronized void _run(CommandInterpreter ci) {
    if(runnable != null) {
            runnable.run();
    } else {
        ci.println("Error, no Runnable available");
    }
}
public String getHelp() {
    return "\trun - execute a Runnable service";
}

}

这工作正常,但是如果我想获取属性的值,则永远不会调用导出器的 using

public synchronized void setRunnable(Runnable r, Map properties)

public synchronized void setRunnable(Runnable r, ServiceReference reference)

方法运行,这意味着绑定函数(setRunnable 未被调用)。但是,使用控制台命令“services”,我看到导出器包被导入器使用。另外,使用 ss 和 ls 我可以看到组件 eporter 是“满意的”。 我的实现有什么问题吗?

预先感谢

干杯

玛丽

I have a very simple example of declarative services. I'm following this tutorial http://www.eclipsezone.com/eclipse/forums/t97690.html?start=0. Every thing is working as expected. However, I cannot figure out how I can make the "SampleImporter" (which is the bundle that is expected to use other bundles' services) aware of the list of "SampleExporter" (bundle providing a service). In other words, I want the "SamlpeImporter" to see the ID of the bundle(s) that it is eventually using. This information is very useful for my application.

here is the XML file for SampleExporter:

<?xml version="1.0"?>
<component name="samplerunnable">
<implementation class="org.example.ds.SampleRunnable"/>
<property name="ID" value="expoter" />
<service>
<provide interface="java.lang.Runnable"/>
</service>

while for the SampleImporter:

<?xml version="1.0"?>
<component name="commandprovider1">
<implementation class="org.example.ds.SampleCommandProvider1"/>
<service>
<provide interface="org.eclipse.osgi.framework.console.CommandProvider"/>
</service>
<reference name="RUNNABLE"
    interface="java.lang.Runnable"
    bind="setRunnable"
    unbind="unsetRunnable"
    cardinality="0..1"
    policy="dynamic"/>
</component>

In the Importer side, I have the following function:

public class SampleCommandProvider1 implements CommandProvider {
    private Runnable runnable;
public synchronized void setRunnable(Runnable r) {
    runnable = r;
}
public synchronized void unsetRunnable(Runnable r) {
    runnable = null;
}
public synchronized void _run(CommandInterpreter ci) {
    if(runnable != null) {
            runnable.run();
    } else {
        ci.println("Error, no Runnable available");
    }
}
public String getHelp() {
    return "\trun - execute a Runnable service";
}

}

This works fine but then if I want to get the value of the property, using

public synchronized void setRunnable(Runnable r, Map properties)

or

public synchronized void setRunnable(Runnable r, ServiceReference reference)

the method run of the exporter is never called which means that the bind function (setRunnable is not called).Hwever, using the console command "services" I see that the exporter bundle is used by the imporeter one. Also, using ss and ls I can see that the component eporter is "satisfied".
What is wrong with my implementetion?

Thanks in advance

Cheers

Marie

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水水月牙 2024-12-15 08:25:11

任何版本的 DS 都不支持以下绑定签名:

public void setRunnable(Runnable r, ServiceReference ref)

相反,您必须使用 ServiceReference 并使用 ComponentContext 或 < code>BundleContext 来访问服务实例对象。

或者,如果您想要以更加 POJO 风格的方式访问服务属性,则 DS 1.1 中允许使用以下绑定签名(但在 DS 1.0 中不允许):

public void setRunnable(Runnable r, Map properties)

要访问 DS 1.1 功能,您需要添加将命名空间正确地添加到您的 XML 中,如下所示:

<component xmlns='http://www.osgi.org/xmlns/scr/v1.1.0' name='...'>

顺便说一句,我很久以前就写了这篇原创文章!这些天我会使用 bnd 注释 来避免手动编写 XML 文档。

The following bind signature is not supported by any version of DS:

public void setRunnable(Runnable r, ServiceReference ref)

Instead you will have to take only the ServiceReference and use either the ComponentContext or BundleContext to access the service instance object.

Alternatively if you want a more POJO-style way of accessing service properties, the following bind signature is allowed in DS 1.1 (but not in DS 1.0):

public void setRunnable(Runnable r, Map properties)

To access DS 1.1 features, you need to add the correct namespace to your XML as follows:

<component xmlns='http://www.osgi.org/xmlns/scr/v1.1.0' name='...'>

By the way, I wrote this original article a very long time ago! These days I would use bnd annotations to avoid having to write the XML document by hand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文