Search code examples
javaosgi

Best technique for getting the OSGi bundle context?


Each bundle in my OSGi project has its own BundleActivator, which I think is normal. This gets passed the current BundleContext, which is useful to have around for getting service references and whatnot.

However, from classes in my bundle, how can I get the BundleContext? Assigning it to a public static field in the BundleActivator sucks and passing it around as an argument also sucks. Is there a more intelligent way?


Solution

  • You can use FrameworkUtil.getBundle(ClassFromBundle).getBundleContext().

    See FrameworkUtil JavaDoc.