Service.FindHook event was'nt called by adding service reference to a component
-
24-06-2021 - |
Question
Today I worked a little bit with OSGi Hooks, specially with FindHook
and EventListenerHook
for services. I wanted to try out proxying services. The EventListenerHook
works fine and my services are proxied. But I realized that the FindHook
event would'nt be called if a service will get a reference of the service implementation by annotating the related setXXX
method with @Reference
(Bnd style). Maybe it is not a problem with the annotation, but with DS in general.
This is quite bad, bc there are two services registered one without proxy and one proxied. I only want to get references from the proxied one. In general, without DS, you can get references by calling BundleContext.getServiceReference(java.lang.Class<S> clazz)
and the event FindHook.find
will be called, where you can decide which ServiceReference
you want to return.
What kind of processes will be executed when I'm using DS like approaches (for me: Bnd annotation @Reference
)? As I described in the above section: I want to decide which services should be referenced.
Solution 3
Thanks for the answers, but I found the solution by my own. After hours of debugging I found out that one if condition of my code was violated and nothing specially happened.
OTHER TIPS
This is really a question for your DS implementation. It depends upon how the DS impl manages service access on behalf of the component bundle since it is the DS implementation bundle which is locating the services. If the DS implementation uses the component bundle's BundleContext to lookup services, you should then be able to influence what services are visible to the component bundle.
When the framework has to decide between two service that would both satisfy a request, it first chooses the one with the higher service-ranking. That ranking can be set as a property when registering a service. So, when registering the proxy, jus make sure it has a higher service-ranking than competing services.