Skip to content

Given a class, how to retrieve its bundle?

2009 July 16
by Benjamin Cabé

OSGI R4.2 (thus Equinox 3.5) comes (well, will come, since the spec. is not final yet) with a new simple and handy utility: the org.osgi.framework.FrameworkUtil.getBundle(Class) static method, which allows to retrieve the bundle a given class belongs to.

More specifically, this method will give you the bundle who loaded the given class, or null if the class has not been loaded by the OSGi framework (e.g. if it is a class belonging to the boot classpath…).

Thus, whenever you want to access information such as the version of a bundle, its headers,  etc. without having to query its Activator (perhaps you don’t even have an Activator for this bundle…), you know what you’ve got to use! Of course, whoever says Bundle, means BundleContext, and this handy helper is also very convenient to easily publish/consume OSGi services on behalf of the bundle… :roll:

10 Responses leave one →
  1. July 16, 2009

    You can use the PackageAdmin too! :)

  2. July 16, 2009

    Yup of course but in 3.5 I find it more direct to use this new thing ;-)

  3. July 16, 2009

    Very nice tip.

    Do you know how to get the information which packages are exported by the bundle? Is there also a method to get the corresponding bundle for a selected project?

    By the way the WP-Cumulus plug-in is cool!

  4. Philipp Kursawe permalink
    July 17, 2009

    I have used ((BundleReference)getClass().getClassLoader()) directly until now. Thanks to inform us, that there is a framework method to do that now.

    I have also adjusted my code assist templates like:
    bundle
    ${imp:import(org.osgi.framework.FrameworkUtil)}
    FrameworkUtil.getBundle(getClass())${cursor}

    For bundle and bc (bundleContext) import the templates from here:
    http://pastebin.com/m3b4a1ef1

  5. Philipp Kursawe permalink
    July 17, 2009

    Yeah the 3d-cloud plugin here is very cool. Is the source for it available? Would be nice to have it in my Eclipse app :)

  6. July 17, 2009

    @Lars

    > Do you know how to get the information which packages are exported by the bundle?

    You need to use the PackageAdmin service. Get a PackageAdmin service reference the way you like most (using a ServiceTracker, Declarative Services, or whatever) and you will then be able to call PackageAdmin#getExportedPackages(Bundle)!

    > Is there also a method to get the corresponding bundle for a selected project?
    You need to use PDE core API, and call o.e.pde.core.plugin.PluginRegistry#findModel(IProject).

  7. July 17, 2009

    @Philip

    > Yeah the 3d-cloud plugin here is very cool. Is the source for it available?

    If you download the “Developer version” there, http://wordpress.org/extend/plugins/wp-cumulus/download/ you’ll get the sources! :-)

  8. July 17, 2009

    Thank you Benjamin.

  9. July 19, 2009

    Wow – finally, we can retrieve the bundle a class belongs to, after years of “you can’t touch this” (see http://wiki.eclipse.org/FAQ_How_do_I_locate_the_owner_plug-in_from_a_given_class%3F). Is hell freezing over?

    At any rate, this is great news and I assume many people will be glad to exploit this feature. Let’s hope they use it wisely.

  10. September 2, 2010

    Per Record,encourage collect walk bag lead amount relative revolution silence stop force single treatment science exercise remember many my gather risk occur promise okay as fall involve client represent both economy close freedom there church name both protect job damage procedure impression debate hair popular instruction trend typical elsewhere cos attack fairly total whole entire they replace behaviour today too wine leaf vote concern date potential carefully labour wrong hide pension display original experience speaker asset neck real walk rise down job hang impact generation prevent

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS