This chapter will guide you through the steps needed to extend the console and create a new shell. We will leverage Maven, Blueprint and OSGi, so you will need some knowledge of those products.
You may also find some information about the console at RFC 147 Overview.
We first need to create the project using maven. Let's leverage maven archetypes for that.
Using the command line, we can create our project:
This generate the main pom.xml and some additional packages.
You can also use the interactive mode for creating the skeleton project:
Use the following values when prompted:
Alternatively, you can simply create the directory shell-sample-commands and create the pom.xml file inside it:
We need to tell maven which libraries our project depends on. In the dependencies section of the pom, add the following one:
This dependency is needed to have access to the base classes that are used to define commands.
We are using annotations to define commands, so we need to ensure maven will actually use JDK 1.5 to compile the jar.
Just add the following snippet after the dependencies section.
We can use maven to generate the needed files for your IDE:
Inside the project, run the following command
or
The project files for your IDE should now be created. Just open the IDE and load the project.
We can now create the command class HelloShellCommand.java
The blueprint configuration file will be used to create the command and register it in the OSGi registry, which is the way to make the command available to Karaf console. This blueprint file must be located in the OSGI-INF/blueprint/ directory inside the bundle.
If you don't have the src/main/resources directory yet, create it.
Then, re-generate the IDE project files and reload it so that this folder is now recognized as a source folder.
Inside this directory, create the OSGI-INF/blueprint/ directory and put the following file inside (the name of this file has no impact at all):
Let's try to build the jar. Remove the test classes and sample classes if you used the artifact, then from the command line, run:
The end of the maven output should look like:
OSGi bundles are jars but they require some manifest headers to be correctly recognized. We will leverage Felix's maven plugin to easily generate those.
Lets turn it into a bundle: modify the line in the pom.xml to adjust the packaging:
Add the following section at the bottom of the pom.xml, in the existing build/plugins section:
The Import-Package is required to make sure our bundle will import the org.osgi.service.command package so that the service will be correctly seen in Felix.
Let's compiled it again using the mvn install command.
Launch a Karaf instance and run the following command to install the newly created bundle:
Let's try running the command:
A completer allow you to automatically complete a command argument using <tab>. A completer is simply a bean which is injected to a command.
Of course to be able to complete it, the command should require an argument.
We add an argument to the HelloCommand:
The Blueprint configuration file is the same as previously.
A completer is a bean which implements the Completer interface:
Using Blueprint, you can "inject" the completer linked to your command. The same completer could be used for several commands and a command can have several completers:
Launch a Karaf instance and run the following command to install the newly created bundle:
Let's try running the command: