views:

2000

answers:

3

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.

+4  A: 

Tricky internal way:

InternalPlatform.getDefault().getBundleContext()

could do it.

You will find an example in this class

public class ExportClassDigestApplication implements IApplication {

    public Object start(IApplicationContext context) throws Exception {
     context.applicationRunning();

     List<ExtensionBean> extensionBeans = ImpCoreUtil.loadExtensionBeans(&quot;com.xab.core.containerlaunchers&quot;);
     for (ExtensionBean bean : extensionBeans) {
      ILauncher launcher = (ILauncher) bean.getInstance();
      launcher.start();
     }
     ClassFilter classFilter = new ClassFilter() {
      public boolean isClassAccepted(Class clz) {
       return true;
      }
     };

     PrintWriter writer = new PrintWriter( new File( "C:/classes.csv"));

     Bundle[] bundles = InternalPlatform.getDefault().getBundleContext().getBundles();


Proper way:

Every plug-in has access to its own bundle context.

Just make sure your plug-in class overrides the start(BundleContext) method. You can then save it to a place classes in your plug-in can easily access

Note the bundle context provided to a plug-in is specific to it and should never be shared with other plug-ins.

VonC
+3  A: 
Anthony Juckel
A: 

Trying to use the Internal Platform trick and running my plugin as an Eclipse Application, I get the following exception:

"java.lang.ClassNotFoundException: org.eclipse.core.internal.runtime.InternalPlatform"

When invoking InternalPlaform in the code I get a warning: "Discouraged access: The type InternalPlatform is not accessible due to restriction on required library org.eclipse.core.runtime_3.5.0.v20090525.jar"

I have the package "org.eclipse.core.internal.runtime" imported in the manifest file. What else should I do to avoid this exception?

Thanks in advance.

orionym
Probably better to try the "proper way" described in the answers.
Thilo