Accessing services from OSGi fragment bundles

742 views Asked by At

In my project, there is an OSGi bundle which exposes an interface com.xyx.EventProvider as a declerative service.

There is another 3rd party bundle org.eclipse.equinox.http.jetty, to which I have attached a fragment bundle com.xyz.jetty.customizer.

This fragment bundle needs to access the service exposed via the interface com.xyx.EventProvider.

The problem is that fragment bundles don't have Activators, so it does not have reference to the BundleContext and therefore it cannot access any service.

Is there a way in OSGi to achieve this?

1

There are 1 answers

2
Neil Bartlett On BEST ANSWER

I always hate to suggest using FrameworkUtil.getBundle().getBundleContext() because it's such a hack, but in this case it seems to be the only solution.

You will need to pass in a class that is defined by the host bundle, because fragments cannot have a BundleContext (i.e. the getBundleContext call would return null).