我知道可以使用属性通配符从 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/