...
top Anchor
...
6.1. Extending the console
This chapter will guide you through the steps needed to extend the console and create a new shell. We will leverage Maven, Spring, Spring-DM Blueprint and OSGi, so you will need some knowledge of those products.As an example, we will create a command that will leverage the OSGi ConfigAdmin to display the existing configurations
You may also find some information about the console at RFC 147 Overview.
Create the project using maven
We first need to create the project using maven. Let's leverage maven archetypes for that.
Command line
Using the command line, we can create our project:
...
This generate the main pom.xml
and some additional packages.
Interactive shell
You can also use the interactive mode for creating the skeleton project:
...
Use the following values when prompted:
...
Manual creation
Alternatively, you can simply create the directory org.apache.servicemix.kernel.gshell.config
shell-sample-commands
and create the pom.xml
file inside it:
...
...
Dependencies
We need to tell maven which libraries our project depends on. In the dependencies
section of the pom, add the following onesone:
...
This dependency is needed to have access to the base classes that are used to define commands.
Configuring for Java 5
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.
...
These are needed respectively for OSGi, ServiceMix Kernel commands support and Spring-DM.
Additionally, we need the ConfigAdmin service, which is in the OSGi Compendium jar:
...
lang | xml |
---|
...
Loading the project in your IDE
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.
Creating a basic command class
We can now create the command class ListCommandHelloShellCommand.java
...
Creating the associated
...
blueprint configuration files
The spring blueprint configuration files 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 the ServiceMix Kernel Karaf console. This spring blueprint file must be located in the METAOSGI-INF/springblueprint/
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 METAOSGI-INF/springblueprint/
directory and put the following file inside (the name of this file has no impact at all):
...
Compiling the jar
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:
...
title | Using Java 5 |
---|
If you see something like that:
...
that's because we forgot to enable Java 5. So let's add that to the pom:
...
lang | xml |
---|
...
Turning the jar into an OSGi bundle
OSGi bundles are jars but they require some manifest headers to be correctly recognized. We will leverage Felix's manven 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
...
lang | 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.
Test in
...
Karaf
Launch a ServiceMix Kernel Karaf instance and run the following command to install are the newly created bundle:
...
Let's try running the command:
...
Command completer
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.
Command argument
We add an argument to the HelloCommand:
...
The Blueprint configuration file is the same as previously.
Completer bean
A completer is a bean which implements the Completer interface:
...
Blueprint configuration file
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:
...
Test in Karaf
Launch a Karaf instance and run the following command to install the newly created bundle:
...
If you run the help
command, you should now see our new shell config
.Let's try running the command:
Code Block |
---|
servicemix> config list
Executing List command
|
Yeah
...
...
...