I'm having a problem with service resolution of the equinox service registry implementation and I'm not sure whether this is a bug.
Here is a short description of my bundles and runtime:
This it the component.xml of FooServiceImpl:
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="com.foo.impl.FooServiceImpl"> <implementation class="com.foo.impl.FooServiceImpl"/> <service> <provide interface="com.foo.api.IFooService"/> <provide interface="org.osgi.service.cm.ManagedService"/> </service> <property name="service.pid" value="fooservice"/> </scr:component>
The component.xml
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="com.foo.user.BarComponentImpl" enabled="true" immediate="true"> <implementation class="com.foo.user.BarComponentImpl" /> <reference interface="com.foo.api.IFooService" name="fooService" policy="static" cardinality="1..1" bind="bindFooService" unbind="unbindFooService"/> </scr:component>
I'm using equinox, in config.ini the bundles described above, org.eclipse.osgi.service and org.apache.felix.configadmin are loaded. (Beside others of course, but they are not of interest right now)
org.eclipse.osgi.service and org.apache.felix.configadmin both provide the package org.osgi.service.cm
Depending on the order of the bundles in config.ini it can happen that com.foo.user does not get a refence to FooServiceImpl.
Debugging into the equinox runtime I found that this happens:
comp
command at the osgi console.Is this the desired behaviour in this case? Shouldn't the framework return a reference to FooServiceImpl when requesting a IFooService even if the service implements another interface that is not compatible with the using bundle?
I didn't find any statement in the OSGi specification, but before opening a bug at eclipse I wanted to hear what the experts think.
Thanks to BJ Hargrave the main problem is solved (see below), but I'm still wondering why the framework handles these two cases differently
Bundle A gets a reference to the service, Bundle B doesn't.
Whats the difference between
when this interface isn't acutally used when requesting service?
Shouldn't the framework return a reference to FooServiceImpl when requesting a IFooService even if the service implements another interface that is not compatible with the using bundle?
It is specified only to return services which are type compatible with the requester. You don't provide the specifics of how you register and find the service in question to provide a more definitive answer.
The solution here is to not install the org.eclipse.osgi.service
bundle. A bundle such as that (a collection of unrelated, exported packages), causes these sorts of problems. org.eclipse.osgi.service
is useful at compile time since you have access to a large palette of services. But at runtime, it creates the sort of problems you see. At runtime, it is better to have either the service implementation export the package, as Felix CM does, or have a bundle export just the service package to support the implementation and its clients.
OSGi provides a osgi.cmpn.jar
, which is like org.eclipse.osgi.service
, for use at compile-time. The latest versions of this jar include an unresolvable requirement to prevent people from using the jar as a bundle at runtime.