Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the OSGi BundleContext for an Eclipse RCP application?

I have just gotten started with an Eclipse RCP application, it is basically just one of the provided "hello world" samples.

When the application boots up, I would like to look at my command-line parameters and start some services according to them. I can get the command-line parameters in IApplication.start:

public Object start(IApplicationContext context) {
   String[] argv = (String[]) 
       context.getArguments().get(IApplicationContext.APPLICATION_ARGS)));
}

But how do I get the BundleContext, so that I can register services? It does not seem to be in the IApplicationContext.

like image 558
Thilo Avatar asked Feb 18 '09 05:02

Thilo


1 Answers

Just came across this doing a web search, and thought I'd promote the new standard OSGi R4.2 way (as provided by Equinox shipped with Eclipse 3.5). If you don't have an activator, and don't want to create one just to cache the bundle context, you can use FrameworkUtil.getBundle. Modifying the previous example:

import org.osgi.framework.FrameworkUtil;

public class ExportClassDigestApplication implements IApplication {
    public Object start(IApplicationContext context) throws Exception {
        context.applicationRunning();
        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
                                                   .getBundleContext();
    }
}
like image 92
Anthony Juckel Avatar answered Sep 18 '22 19:09

Anthony Juckel