The creation of a custom command can be boiled down to drafting a boiler plate pom file, extending OsgiCommandSupport, writing a command completer, and finally wiring it all together via blueprint. Don't worry about directory structure, or other boiler plate bits of code for now, a full source code demo will be included in Karaf starting on version 2.2.5.

The Pom File:

The critical bits you'll encounter in the pom file for making your own command will be the initial setup declarations:

The actual custom command logic is implemented in a class you write that extends OsgiCommandSupport. The annotations for 'command' and 'argument' is where you'll immediately need to pay attention. In the command annotation you setup your command scope and name (each scope can have several names associated via separate commands). In the argument annotation you describe the arguments that the command will expect. The doExecute() method is where you'll perform the action for the command, add any custom code you want to have performed here.

Now to bring all these components together we use Blueprint. Blueprint is a dependency injection framework that we like to use with OSGi in Karaf. In short the below xml handles the instantiation of objects and wiring them together.

3 comments:

Hi,A very nice blog. I followed yours and was able to come up with one of my own CLIs.How can we have a variable list of arguments for the CLI that we add.Say, for my CLI, it can have 3/4/5 arguements (varaible length of arguments).Any help will be greatly appreciated.