Twitter github

Tour de OSGi Console

After feeling like one of those people in the Lost TV series that disappears from the island… (was tracing some file handle leak problem from hell) I finished up an article regarding using and extending the OSGi console in Eclipse. For those who don’t know, launching Eclipse with the -console parameter can be a ton of fun (you can start and stop things and take down Eclipse in no time!). If you type help in the console, you’ll notice a ton of commands available. Ever wonder how to have your own command (can be useful in debugging situations regarding your specific service)? Well, wonder no more, here’s a simple example from the upcoming article (the important parts are implementing CommandProvider and prefixing commands as methods that start with the ‘_’ character):


public class Activator
implements BundleActivator, CommandProvider {

private BundleContext context;

public void start(BundleContext context) throws Exception {
this.context = context;
Hashtable properties = new Hashtable();
context.registerService(CommandProvider.class.getName(),
this, properties);

}

public String getHelp() {
StringBuffer buffer = new StringBuffer();
buffer.append("\tuname - framework information\n");
return buffer.toString();
}

public void stop(BundleContext context) throws Exception {}

public void _uname(CommandInterpreter ci) throws Exception {
String vendor =
context.getProperty(Constants.FRAMEWORK_VENDOR);
String version =
context.getProperty(Constants.FRAMEWORK_VERSION);
String osName =
context.getProperty(Constants.FRAMEWORK_OS_NAME);
String osVersion =
context.getProperty(Constants.FRAMEWORK_OS_VERSION);
System.out.println("\n " + vendor + " "
+ version + " (" + osName + " "
+ osVersion + ")");
}

The end result should look something like this: