Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [equinox-dev] finding a BundleContext

Tom,

Not in OSGi Release 4 (i.e. Equinox 3.2). There is no public API for
getting the BundleContext of another bundle, because the BundleContext is
a "capability" - it is a private interface between a particular bundle and
the framework.

There are only very few valid reasons for Bundle-A to get the
BundleContext from Bundle-B. Implementing Declarative Services is one of
those reasons, and the stuff Spring does is another. Both at the moment
use reflection-based hacks to do it.

OSGi Release 4.1 (also known as JSR 291, which will be implemented by
Equinox 3.3) introduces a getBundleContext() method on the Bundle
interface in order to support those very few people who need to do this.
But it is still dangerous and should not be used willy-nilly. Can you
explain why you need to do it?

Regards,
Neil

> Hello,
>
> I'm trying to find the BundleContext for some random bundle I received
> through a BundleEvent.
> I see spring-osgi does it like this:
>
>       Method m = bundle.getClass().getDeclaredMethod("getContext", new
> Class[0]);
>       m.setAccessible(true);
>       return (BundleContext) m.invoke(bundle, new Object[0]);
>
> Is there a better way ?
>
> Tom
> _______________________________________________
> equinox-dev mailing list
> equinox-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/equinox-dev
>




Back to the top