Skip to main content

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

On 12/23/06, Neil Bartlett <njbartlett@xxxxxxxxx> wrote:
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?


I am building a client application which invokes a set of remote
services through RMI. The interfaces for these services are packaged
in a number of bundles separate from the client application bundles
(they are also used on the server side).

I would like my client to access these remote services as OSGi
services. Therefore, someone needs to register them. I have created a
separate bundle, independent from both the client and the service
interfaces, that does this. This bundle

1. Check every bundle for the existence of a RemoteServices: true header
2. Enumerate over these bundles, and find all I*Service classes (this
should be customized)
3. Register a dynamic proxy for each of these, that will connect to
the remote server on the
first use of the service.

For the registration of the services I use the BundleContext of the
bundle that declares the interface, so the lifecycle of the services
will be limited to that of their bundle.

The reason I don't register the services in their own bundle's activator are:
- I don't want to repeat this for every service bundle
- The bundle shouldn't care exactly how the connection to the remote
service is handled

I have this working now, but am open to suggestions for other ways to
handle this...

Tom

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
>


_______________________________________________
equinox-dev mailing list
equinox-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/equinox-dev



Back to the top