How can I reference all components that implement a given interface?

1k views Asked by At

I am using the Apache Felix Service Component Runtime (SCR) on an Eclipse Equinox OSGi environment.

There are several components declared that implement the interface org.example.Producer like:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ProducerA">
    <implementation class="org.example.ProducerA"/>
    <service>
        <provide interface="org.example.Producer"/>
    </service>
</scr:component>

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ProducerB">
    <implementation class="org.example.ProducerB"/>
    <service>
        <provide interface="org.example.Producer"/>
    </service>
</scr:component>

Now in an other component I like to reference all those components that implement the interface org.example.Producer dynamically:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ConsumerA">
    <implementation class="org.example.ConsumerA"/>
    <reference bind="bindProducer" cardinality="0..n" interface="org.example.Producer" policy="dynamic" unbind="unbindProducer"/>
    <service>
        <provide interface="org.example.Consumer"/>
    </service>
</scr:component>

But this gives an error at runtime. It seems that the SCR includes a component name into its search filter:

!ENTRY org.eclipse.equinox.ds 1 0 2015-06-22 11:31:31.781
!MESSAGE Could not bind a reference of component org.example.ConsumerA. The reference is: Reference[name = org.example.Producer, interface = org.example.Producer, policy = dynamic, cardinality = 0..n, target = null, bind = bindProducer, unbind = unbindProducer]

As you see in the error message it is searching explicitly for components with the name org.example.Producer. But none of the above listed components has that name (org.example.ProducerA, org.example.ProducerB).

So the question is how can I reference components dynamically that provide implementations for a given interface by ignoring their names?

1

There are 1 answers

0
Henrik Sachse On BEST ANSWER

I was mislead by the mentioned log message as Neil Bartlett pointed out. The corresponding services just took very long to start up but at the end they were bound correctly.