Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Osgi Declarative service conditional binding

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

like image 411
el_eduardo Avatar asked Jul 27 '11 19:07

el_eduardo


1 Answers

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>
like image 60
Ivan Dubrov Avatar answered Oct 12 '22 23:10

Ivan Dubrov