javaspringosgispring-dm

How to get osgi service by id with Spring?


Spring noob here.

I have an osgi service defined as follows in one of my bundles:

<osgi:service id="myModelInterpreter" ref="myModelInterpreterService" interface="*.IModelInterpreter" />

I know I can access it from another bundle via the BundleContext doing getServiceReference (passing down IModelInterpreter.class.getName) and then getService.

Is there a way to get the service via the id (myModelInterpreter) instead of using the Interface (passed down as IModelInterpreter.class.getName to getServiceReference)?

Any help appreciated.


Solution

  • I dont think you can get the service by the id you specified in the spring config as it is an internal setting of the spring context. What you can do is add some service proprties to the service and filter for them.

    Like this:

    <service ref="myModelInterpreterService" interface="*.IModelInterpreter">
      <service-properties>
        <beans:entry key="myId" value="myModelInterpreter"/>
      </service-properties>
    </service>
    

    Then in the other bundle you can filter for the properties:

    <reference id="myModelInterpreterService" interface="com.xyz.IModelInterpreter"
      filter="(myId=myModelInterpreter)"/>