You can do FrameworkUtil.getBundle(someClass) to get the Bundle for a 
class. This avoids the need for permission to get the class loader.
-- 

BJ Hargrave
Senior Technical Staff Member, IBM
OSGi Fellow and CTO of the OSGi Alliance
[email protected]

office: +1 386 848 1781
mobile: +1 386 848 3788





From:   Ivan Dubrov <[email protected]>
To:     OSGi Developer Mail List <[email protected]>
Date:   2010/08/17 22:30
Subject:        [osgi-dev] Getting BundleContext from classloader
Sent by:        [email protected]



Hi,

Just wondering, is it a good idea for some class to get its bundle
BundleContext from its own classloader, like this:

BundleContext ctx = ((BundleReference)
ThisClass.class.getClassLoader()).getBundle().getBundleContext();

It seems to me that the only limitation is that getClassLoader() may
require additional permissions.

The reason I want to do it this way is that I have a lot of bundles
without activator which want to get BundleContext from time to time. Of
course, adding an activator that will remember BundleContext is a simple
thing, but that's a pretty boilerplate code which I need to add to a
number of bundles (about 20 or so).

Probably, Declarative Services or Blueprint Container can inject those,
but my project currently does not use them and seems like adding them
immediately is not very possible.

What do you think? Is there a better way to get Bundle/BundleContext
without writing explicit activators?

-- 
WBR,
Ivan S. Dubrov



_______________________________________________
OSGi Developer Mail List
[email protected]
https://mail.osgi.org/mailman/listinfo/osgi-dev

_______________________________________________
OSGi Developer Mail List
[email protected]
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to