按属性搜索OSGI服务 [英] Search OSGI services by properties

查看:82
本文介绍了按属性搜索OSGI服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过属性区分已发布的OSGI服务实现相同的接口?

How can I distinguish between published OSGI services implementing same interface by their properties?

推荐答案

假设您要根据属性的某些值检索已注册的服务,则需要使用过滤器(基于 LDAP 语法)。

Assuming that you want to retrieve registered services based on certain values for properties, you need to use a filter (which is based on the LDAP syntax).

例如:

int myport = 5000;
String filter = "&(objectClass=" + MyInterface.class.getName() 
                + ")(port=" + myport + ")";
ServiceReference[] serviceReferences = bundleContext.getServiceReferences(null,filter);

您要查找实施 MyInterface 并且 port 属性的值等于 myport

where you want to look for services both implementing MyInterface and having a value of the port property equal to myport.

此处是获取参考资料的相关javadoc。

Here is the relevant javadoc for getting the references.

备注1:

上面的例子和javadoc引用了4.2版。如果您不限于J2SE 1.4运行时,我建议您查看版本4.3 语法,你可以使用泛型。

The above example and javadoc refer to the Release 4.2. If you are not restricted to a J2SE 1.4 runtime, I suggest you to have a look at the Release 4.3 syntax, where you can use generics.

备注2 :(由Ray提供)

您还可以通过创建 filterStr 字符串的//www.osgi.org/javadoc/r4v42/org/osgi/framework/Filter.html\">Filter 对象:

You can also pre-check the correctness of your filter by instead creating a Filter object from a filterStr string:

Filter filter = bundleContext.createFilter(filterStr);  

还允许您将过滤器与其他条件匹配。你仍然传递 filterStr 来获取引用,因为没有超载来解释 Filter 参数。但请注意,通过这种方式,您将检查两次正确性: getServiceReferences createFilter throw 解析过滤器时出现InvalidSyntaxException 。我猜,当然不是一个显示效率低下的效率,但值得一提。

which also allows you to match the filter with other criteria. You still pass filterStr to get the references, since there is no overloading that accounts for a Filter argument. Please be aware, however, that in this way you will check the correctness twice: both getServiceReferences and createFilter throw InvalidSyntaxException on parsing the filter. Certainly not a show-stopper inefficiency, I guess, but it is worth mentioning.

这篇关于按属性搜索OSGI服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆