Search code examples
javaosgiapache-felix

Define OSGi DS Reference Filter with multiple properties


My question is simple, is it possible to create a @Reference filter based on more than a single property? If so, what is the syntax?

For background, my service looks like this:

@Component (property = {"myProp1=foo", "myProp2=bar"})
public class MyService implements IMyService
...

The thing that uses IMyService only wants one where myProp1=foo and myProp2=bar. I have tried various syntax forms and have not found one that works yet.

private @Reference (target = "(myProp1=foo)(myProp2=bar)") IMyService svc;

The above actually does resolve and I get a reference, however an error is logged, "Invalid syntax in target property for dependency...". So even though it works, it appears to be a fluke because there is some issue with the syntax.

private @Reference (target = "((myProp1=foo)(myProp2=bar))") IMyService svc;
private @Reference (target = "((myProp1=foo) (myProp2=bar))") IMyService svc;
private @Reference (target = "((myProp1=foo), (myProp2=bar))") IMyService svc;
private @Reference (target = "{(myProp1=foo), (myProp2=bar)}") IMyService svc;
private @Reference (target = "[(myProp1=foo), (myProp2=bar)]") IMyService svc;

None of the above even compile which is nice to have validation.

private @Reference (target = "([myProp1=foo, myProp2=bar])") IMyService svc;
private @Reference (target = "(myProp1=foo, myProp2=bar)") IMyService svc;

That above compile but simply don't return a service.

private @Reference (target = "(myProp1=foo)") IMyService svc;

Filtering on a single field as shown above works just fine but it is not the complete solution I am looking for.

The javadoc for the @Reference annotation does not provide any guidance on how to use the "target" parameter.


Solution

  • Sure you can, the filter format is 'ldap style'. The and operator is the '&', and you use it before your sub expressions.

    In your case it would be something like:

    (&(myProp1=foo)(myProp2=bar))

    For reference: https://osgi.org/javadoc/r2/org/osgi/framework/Filter.html