Hi SMX users, I wrote a command for the console, which prints out a list of services and their consumers (def: a consumer is a bundle which uses a particular service).
I used ServiceReference.getUsingBundles() to get hold of the consumers and I expected, if there are several instances of the same service, that the consumers for each service differ, e.g.: [38, org.ops4j.pax.web.pax-web-runtime][org.osgi.service.cm.ManagedService][5] [2, org.ops4j.pax.url.wrap][org.osgi.service.cm.ManagedService][ 32] [43, org.ops4j.pax.url.war][org.osgi.service.cm.ManagedService][36, 42] [1, org.ops4j.pax.url.mvn][org.osgi.service.cm.ManagedService][] (The first id at the left is the bundle id of the bundle which exposes the service (ServiceReference.getBundle), The last column contains bundle ids of consumers.) However, I get a result like this one: [38, org.ops4j.pax.web.pax-web-runtime][org.osgi.service.cm.ManagedService][5, 32] [2, org.ops4j.pax.url.wrap][org.osgi.service.cm.ManagedService][5, 32] [43, org.ops4j.pax.url.war][org.osgi.service.cm.ManagedService][5, 32] [1, org.ops4j.pax.url.mvn][org.osgi.service.cm.ManagedService][5, 32] The output above indicates that the bundles 5 and 32 use all of these service instances. I thought to myself, that could be possible - maybe only one bundle creates the service, but several bundles register them (no idea if that is possible at all). So I printed more information about this particular service: karaf@root>servicelist ____________________________ The service: [org.osgi.service.cm.ManagedService] This bundle registered the following service: 38, org.ops4j.pax.web.pax-web-runtime Service reference hash code 19719540 Service hash code 26924663 Service Properties: objectClass = [Ljava.lang.String;@764254 service.id = 17 service.pid = org.ops4j.pax.web ************************** ____________________________ The service: [org.osgi.service.cm.ManagedService] This bundle registered the following service: 2, org.ops4j.pax.url.wrap Service reference hash code 30776636 Service hash code 33338817 Service Properties: objectClass = [Ljava.lang.String;@c115 service.id = 5 service.pid = org.ops4j.pax.url.wrap ************************** ____________________________ The service: [org.osgi.service.cm.ManagedService] This bundle registered the following service: 43, org.ops4j.pax.url.war Service reference hash code 5013008 Service hash code 25452612 Service Properties: objectClass = [Ljava.lang.String;@1cc446d service.id = 18 service.pid = org.ops4j.pax.url.war ************************** ____________________________ The service: [org.osgi.service.cm.ManagedService] This bundle registered the following service: 1, org.ops4j.pax.url.mvn Service reference hash code 3534544 Service hash code 27294748 Service Properties: objectClass = [Ljava.lang.String;@1343e2f service.id = 3 service.pid = org.ops4j.pax.url.mvn ************************** As you can see, the hashcodes for the services are all different, the same goes for the service ID and the servicereference instances. I conclude therefore, that all these services are actually different instances. So my question is: how is this possible? Is this a bug in the ServiceReference implementation, did I get the Javadocs wrong or is this the expected behaviour? If this is the expected behaviour, is there any other way to find the real consumer of a particular service instance? Any hints highly appreciated! Cheers, Andy Sources: http://www.osgi.org/javadoc/r4v42/index.html?org/osgi/framework/ServiceReference.html
