Search code examples
javaosgiequinox

Lookup OSGI service which is registered with wildcard property


I know that one can lookup service from the OSGI service registry using properties wildcards. This question is about doing it the other way round.

Basically you register a service like this:

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);

and then you would like to lookup this service with a concrete value for this property, like this:

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

This won't find the service as the service registration property is not considered as wildcard i.e. you would find the service only if you ask specifically for (name=europe-*) - (not sure if you have to escape the * when you look up for * literal)

To contrast this - usually it is the other way round - you register a service with a concrete property value i.e. (name=europe-spain) and then you look it up by using (name=europe-*) - this works, but unfortunately it is not what I need.


Solution

  • It does not work that way. The properties in the service registration are values which can be searched over using a filter expression.

    You can either do this:

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

    which will look for either europe-spain or europe-*. Or you can register the service with name values it supports:

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