[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [equinox-dev] finding a BundleContext
- From: "Tom Huybrechts" <tom.huybrechts@xxxxxxxxx>
- Date: Sat, 23 Dec 2006 11:54:25 +0100
- Delivered-to: email@example.com
- Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=UmVDN1b1l0aqb+TopLoI4CTT/7tz79K/QUld2ym/gV0R3Dk1DdCzsRfDc0E08hMZPA5D7iLAazqMCXbkIMSXeRAw9ybeelT4+TKHx/s6FzlOEElGsKivNCAksxBpUxTvIWZ1/UpDhqdvrwZEJ1LdVF18oRbZC7pKQaYwgMQS+/M=
On 12/23/06, Neil Bartlett <njbartlett@xxxxxxxxx> wrote:
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
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
> 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
> return (BundleContext) m.invoke(bundle, new Object);
> Is there a better way ?
> equinox-dev mailing list
equinox-dev mailing list