8

I have this scenario, I have three declarative services that provide the same interface (say a reader interface and I have readerimpl1-database- readerimpl2-flat file- readerimpl3-memory). I want to have a consumer that binds only to the database implementation. In the component definition we give it a name so I am pretty sure that the name is in the registry so if I were to add an activate method I can lookup from the component context using the name.

I want to try to it via the bind/unbind though using the service name as the parameter. I am pretty sure that the "target" parameter in the component reference element can be used to do this but I have not found how to use it.

Has anyone else done this?

This would be similar to using @Reference(mapped-name="foo")

el_eduardo
  • 3,138
  • 4
  • 21
  • 17
  • This is a really interesting question. It makes me think of the related question of if you *prefer* the database impl but will accept another. In that scenario, you could implement via @Reference(cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE,policy=ReferencePolicy.DYNAMIC) and then sift through the list in your add/remove methods. – Chris Dolan Sep 16 '11 at 18:29

1 Answers1

15

Target is simply an OSGi filter. You can use it to filter by any service property. So, if your services have property named backend with values file or database, you can bind with the following target:

<scr:reference ... target="(backend=database)"/>

And the service with database backend itself will register as:

<scr:component ...>
    ...
    <property name="backend" type="String" value="database"/>
</scr:component>
Ivan Dubrov
  • 4,778
  • 2
  • 29
  • 41