3

I am newbie in OSGi. I have googled about a few hours but couldn't find the answer. Maybe my understanding is wrong. So the problem. Lets suppose I have a component.

<component name="sample.component" immediate="true">
  <implementation class="sample.SampleComparator" />
  <service>
    <provide interface="java.util.Comparator" />
  </service>
</component>

and in code:

ServiceReference[] serviceReferences = 
    bundleContext.getServiceReferences(
        java.util.Comparator.class.getName(), "(name=sample.component)");

But I get null. Where is the mistake? Is filter (name=sample.component) right? Or how can I set id of the service and lookup by it?

palacsint
  • 28,416
  • 10
  • 82
  • 109

1 Answers1

5

The name of the service property is "component.name", not "name".

If you do this, it will work:

getServiceReferences(java.util.Comparator.class.getName(),"(component.name=sample.component)");
Balazs Zsoldos
  • 6,036
  • 2
  • 23
  • 31
  • Thank you very much! If you know is this the only way to set id (name) of service? I mean we can use only component.name? –  Apr 03 '14 at 09:26
  • 2
    It is better to use service.pid as it is unique even if you create your component in the way that it is instantiated multiple times based on configuration (see configurationFactory=true). service.pid is a persistent id that is will be the same after restart. Filtering on service.pid should come also from a persistent configuration (see referenceName.target inside a component configuration) – Balazs Zsoldos Apr 03 '14 at 17:42