查找使用通配符属性的OSGI服务

时间:2013-04-19 14:59:05

标签: 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-*)时才会找到该服务 - (不确定您是否必须逃避*查找* literal)

要对此进行对比 - 通常是相反的方式 - 您注册具有特定属性值的服务,即(name=europe-spain),然后使用(name=europe-*)进行查找 - 这可行,但不幸的是不是我需要的。

1 个答案:

答案 0 :(得分:2)

它不起作用。服务注册中的属性是可以使用过滤器表达式搜索的值。

你可以这样做:

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

将寻找欧洲 - 西班牙或欧洲 - *。或者您可以使用它支持的名称值注册服务:

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