java - 查找使用通配符属性注册的 OSGI 服务

标签 java osgi equinox

我知道可以使用属性通配符从 OSGI 服务注册表查找服务。这个问题是反过来做的。

基本上你注册一个服务是这样的:

Map<String, String. prop = new HashMap<String, String>();
prop.put("name", "europe-*"); // this is the point - the service is registered with property containing wildcard 
context.registerService(IService.class.getName(), new ContinentServiceEuropeImpl(), prop);

然后您希望使用该属性的具体值来查找该服务,如下所示:

ServiceTracker primaryTracker = new ServiceTracker(bundleContext, "(&(objectClass=<IService class name here>)(name=europe-spain))", null);

这不会找到服务,因为服务注册属性不被视为通配符,即只有当您专门询问 (name=europe-*) 时,您才会找到该服务 - (不确定如果您在查找 * 文字时必须转义 *)

相比之下 - 通常是相反的 - 您使用具体的属性值注册一个服务,即 (name=europe-spain),然后使用 查找它(name=europe-*) - 这可行,但不幸的是这不是我需要的。

最佳答案

事实并非如此。服务注册中的属性是可以使用过滤表达式搜索的值。

您可以执行以下操作:

ServiceTracker primaryTracker = new ServiceTracker(bundleContext, "(&(objectClass=<IService class name here>)(|(name=europe-spain)(name=europe-\\*))", null);

它将查找 europe-spain 或 europe-*。或者您可以使用它支持的名称值注册服务:

prop.put("name", new String[]{"europe-spain","europe-france",...});

关于java - 查找使用通配符属性注册的 OSGI 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16107575/

相关文章:

java - Weka 打包和扩展

java - 找不到合适的驱动程序发现 odbc、db2 错误

haskell - Haskell 可以实现 OSGi 吗?

eclipse - 如何在 Windows 上为 headless Eclipse 应用程序设置代理配置

java - 根据 boolean 条件使用星号强制必填字段

java - 从 Android 手机发送字符串到 PC

OSGI Bundle 的 start() 方法未被调用

java - 运行 OSGI 框架时出现 bundle 异常

java - Logback 在 OSGi 容器中包含片段

java - 如何在 Eclipse RCP 应用程序中使用单独的数据库进行生产和测试