java 按属性搜索 OSGI 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11151376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Search OSGI services by properties
提问by John Smith
How can I distinguish between published OSGI services implementing same interface by their properties?
如何通过属性区分实现相同接口的已发布 OSGI 服务?
回答by Luca Geretti
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 LDAPsyntax).
假设您想根据属性的某些值检索已注册的服务,您需要使用过滤器(基于LDAP语法)。
For example:
例如:
int myport = 5000;
String filter = "&(objectClass=" + MyInterface.class.getName()
+ ")(port=" + myport + ")";
ServiceReference[] serviceReferences = bundleContext.getServiceReferences(null,filter);
where you want to look for services both implementing MyInterface
and having a value of the port
property equal to myport
.
您想在其中查找既实现MyInterface
又具有port
等于的属性值的服务myport
。
Hereis the relevant javadoc for getting the references.
这是用于获取引用的相关 javadoc。
Remark 1:
备注一:
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.
上面的示例和 javadoc 是指版本 4.2。如果您不限于 J2SE 1.4 运行时,我建议您查看 4.3 版语法,您可以在其中使用泛型。
Remark 2: (courtesy of Ray)
备注 2:(Ray 提供)
You can also pre-check the correctness of your filter by instead creating a Filterobject from a filterStr
string:
您还可以通过从字符串创建Filter对象来预先检查过滤器的正确性filterStr
:
Filter filter = bundleContext.createFilter(filterStr);
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.
这还允许您将过滤器与其他条件相匹配。您仍然可以通过filterStr
获取引用,因为没有考虑Filter
参数的重载。请注意,不过,这样你将检查正确性两次都getServiceReferences
和createFilter
扔InvalidSyntaxException
在解析过滤器。当然,我猜这不是一个令人窒息的低效率,但值得一提。
回答by Neil Bartlett
Luca's answer above is correct, however it assumes you are using the low level API for accessing services.
Luca 上面的回答是正确的,但是它假设您使用的是低级 API 来访问服务。
If you are using Declarative Services (which I would generally recommend) then the filter can be added to the target
attribute of the service reference. For example (using the bnd annotations for DS):
如果您使用的是声明式服务(我通常会推荐),那么可以将过滤器添加到target
服务引用的属性中。例如(使用 DS 的 bnd 注释):
@Reference(target = "(port=8080)")
public void setHttpService(HttpService http) {
// ...
}
回答by sky4
In Blueprint you can specify the filter attribute on the reference or reference-list element. For example:
在蓝图中,您可以在引用或引用列表元素上指定过滤器属性。例如:
<reference id="sampleRef"
interface="org.sample.MyInterface"
filter="(port=5000)"/>