osgiinjecte4declarative-services

How to deal with 2 OSGi Declarative Services Bundle component in the same Plugin?


I use OSGi services via manually component definition. Service components consist of an XML description and an object. My project was working just fine until I try to instantiate another service in the same plugin. Now it seems to me, as if I'm not supposed to declare two component.xml file in the same plugin.

component.xml

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="ICustomerOSGiService">
   <implementation class="de.checkpoint.rinteln.service.customer.service.CustomerOSGiService"/>
   <service>
    <provide interface="de.checkpoint.rinteln.carlofon.common.service.ICustomerOSGiService"/>
   </service>
</scr:component>

By injecting the interface I can access to the implementation.

Now I want a second component.xml with a different implementation so I can call just like the first. But Eclipse wouldn't let me to it. So I figured, my be i need to separate them. I mean in 2 differents plugins, which has been working fine so far. Still, my plugins look pretty empty now. So I want to combine all the services in the same plugin. Is there any way to concentrate the components as XML? Something like the code below (which I already tried by the way but unfortunately, doesn't work)

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="IOSGiService">
   <implementation class="de.checkpoint.rinteln.service.customer.service.CustomerOSGiService"/>
   <service>
    <provide interface="de.checkpoint.rinteln.carlofon.common.service.ICustomerOSGiService"/>
   </service>

   <implementation class="de.checkpoint.rinteln.service.customer.service.ReminderOSGiService"/>
   <service>
    <provide interface="de.checkpoint.rinteln.carlofon.common.service.IReminderOSGiService"/>
   </service>
</scr:component>

Solution

  • You can have any number of components in a plug-in (I have 8 in one plug-in).

    You put each component in a separate XML file (the name can be whatever you want) and list them in the Service-Component entry in the MANIFEST.MF.

    So in the MANIFEST.MF I have:

    Service-Component: OSGI-INF/playerStateService.xml,
     OSGI-INF/editorManager.xml,
     OSGI-INF/viewManager.xml,
     OSGI-INF/dateUtil.xml,
     OSGI-INF/preferenceSettings.xml,
     OSGI-INF/dialogSettings.xml,
     OSGI-INF/extensionFactory.xml,
     OSGI-INF/imperativeExpressionManager.xml
    

    And my XML file looks like:

    <?xml version="1.0" encoding="UTF-8"?>
    <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" enabled="true" name="greg.music.playerStateService">
       <implementation class="greg.music.core.services.PlayerStateContextFunction"/>
       <property 
           name="service.context.key" 
           type="String" 
           value="greg.music.core.services.IPlayerStateService"/>
       <service>
          <provide interface="org.eclipse.e4.core.contexts.IContextFunction"/>
       </service>
    </scr:component>
    

    (ignore the property value, that is just for this specific service).