Tip: Custom OSGi (Equinox) Console Commands

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

picture 2 300x127 Tip: Custom OSGi (Equinox) Console Commands

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:

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();
	}

}

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

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;
	}

}

And that’s it! If you give the plug-in a run, you’ll see this output:
picture 1 300x264 Tip: Custom OSGi (Equinox) Console Commands

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

7 Responses to “Tip: Custom OSGi (Equinox) Console Commands”

  1. Heiko says:

    Chris,

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

    Heiko

  2. zx says:

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

  3. Robert Konigsberg says:

    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.

  4. zx says:

    @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.

  5. Robert Konigsberg says:

    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.

  6. [...] ¬†http://eclipsesource.com/blogs/2008/08/01/tip-custom-osgi-equinox-console-commands/ ↩ [...]

7 responses so far

Written by . Published in Categories: EclipseSource News