Tip: Custom OSGi (Equinox) Console Commands

Tip: Custom OSGi (Equinox) Console Commands

Ever use the OSGi console included in Eclipse (i.e., run the eclipse executable with -console)?

Ever wanted to add your own console commands for your application?

If so, the process is pretty easy and straightforward. The first step is to implement the CommandProvider interface. Here is a simple and fun example:

[code lang=”java”]
package com.code9.examples.console;

import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;

public class MyCommandProvider implements CommandProvider {

public Object _iddqd(CommandInterpreter intp) {
intp.println(“Immortal mode enabled”);
return null;
}

public Object _idkfa(CommandInterpreter intp) {
intp.println(“Wooooo!”);
return null;
}

public Object _idclip(CommandInterpreter intp) {
intp.println(“No clipping mode on”);
return null;
}

public String getHelp() {
StringBuffer buffer = new StringBuffer();
buffer.append(“—My commands—\n\t”);
buffer.append(“iddqd – God mode\n\t”);
buffer.append(“idkfa – Armed mode\n\t”);
buffer.append(“idclip – No clipping mode\n”);
return buffer.toString();
}

}
[/code]

To extend the console, we simply need to register our implementation with the service registry. We can do this in our activator:

[code lang=”java”]
package com.code9.examples.console;

import org.eclipse.osgi.framework.console.CommandProvider;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

private CommandProvider service;

public void start(BundleContext context) throws Exception {
service = new MyCommandProvider();

// register the service
context.registerService(CommandProvider.class.getName(), service, null);
}

public void stop(BundleContext context) throws Exception {
service = null;
}

}
[/code]

And that’s it! If you give the plug-in a run, you’ll see this output:

The code for this example can be downloaded here (licensed under the EDL 1.0).

7 Comments
  • Heiko
    Posted at 11:27 am, August 1, 2008

    Chris,

    I can see that you love your Mac and you also like “alpha” shells.
    So do I 😉

    Heiko

  • zx
    Posted at 11:46 am, August 1, 2008

    @Heiko, ya… I love my MacBook Pro with 4GB RAM 🙂

  • Posted at 7:24 am, August 2, 2008

    Hello Chris,

    nice tip 😉
    I also posted a similar example here:
    http://www.tutorials.de/forum/java-technology-news/287528-equinox-osgi-konsole-um-eigene-commands-erweitern.html

    Best regards,
    Tom

  • Robert Konigsberg
    Posted at 10:49 am, August 10, 2008

    What I’d like to see are some real world examples of console extensions. I suspect these extensions, unfortunately, are not very popular just because fewer people are aware of them, and most people can extend initial functionality in to a view.

  • zx
    Posted at 8:38 am, August 20, 2008

    @Robert, if you want to see real-world examples, just look at implements of the CommandProvider interface within the Eclipse SDK. Equinox and other projects implement a bit more complex commands… but they all boil down to the same structure as given in the example.

  • Robert Konigsberg
    Posted at 8:39 pm, August 23, 2008

    Sorry, I wasn’t clear: I meant I’d like to see some examples outside the SDK — cases where end-users choose to extend the console.

  • Pingback: Having fun with the OSGi console in Helios; also random monkey-see-monkey-doisms at wwerner's blog
    Posted at 12:52 am, June 30, 2010