Apache Felix Shell Service
- Overview
- How the Shell Service Works
- How Commands Work
- Creating a Command
- Security and the Shell Service
- Feedback
Overview
In order to interact with Felix it is necessary to have some sort of interactive shell that allows you to issue commands to the framework and to obtain information from it. The OSGi specification does not define how an OSGi framework should provide this interactivity. Felix defines a shell service for creating and executing arbitrary commands. The shell service does not define a user interface, only a service API.
The benefit of the Felix shell service approach is that it is possible to:
- have multiple shell user interfaces (e.g., textual and graphical),
- add custom commands to the shell (i.e., bundles can make commands available via the shell service), and
- use the shell service from other bundles/services.
The remainder of this document describes how the shell service works and how to create custom commands for it. This document does not describe how to use the command shell, nor does it describe the text-based or GUI-based user interfaces that are available for the shell.
How the Shell Service Works
The Felix shell service is intended to be a simple, but extensible shell service that can have multiple user interface implementations, all of which are independent from the Felix framework. The shell service is currently not intended to be sophisticated, rather it is just a mechanism to execute commands. The shell service maintains a list of command services, each of which have a unique command name. The shell service is defined by the following service interface:
Using the shell service interface, it is possible to access and execute available commands. The shell service methods perform the following functions:
- getCommands() - returns an array of strings that correspond to the names of the installed shell commands.
- getCommandUsage() - returns the command usage string for a particular command name
- getCommandDescription() - returns a short description for a particular command name.
- getCommandReference() - returns the service reference for a particular command name.
- executeCommand() - executes a particular command using the specified command line and print streams.
Most of the shell service methods require no explanation except for the executeCommand() method. Even though this method is the most complex, it is still fairly simplistic. The assumption of the shell service is that a command line will be typed by the user (or perhaps constructed by a GUI) and passed into it for execution. The shell service interprets the command line in a very simplistic fashion; it takes the leading string of characters terminated by a space character (not including it) and assumes that this leading token is the command name. Consider the following command line:
update 3 http://www.foo.com/bar.jar
The shell service interprets this as an update command and will search for a command service with the same name. If a corresponding command service is not found, then it will print an error message to the error print stream. If a corresponding command service is found, then it will pass the entire command line string and the print streams into the executeCommand() method of the command service (for a more detailed description of command services, see the next section).
Notice that there is no method to add commands to the shell service interface. This is because commands are implemented as OSGi services and the shell service listens for service events and when a command service registers/unregisters it automatically updates its list of commands accordingly.
How Commands Work
All commands available in the shell service are implemented as OSGi services. The advantage of this approach is two-fold: the shell service can leverage OSGi service events to maintain its list of available commands and the set available commands is dynamically extendable by installed bundles. The command service interface is defined as follows:
The semantics of the command service methods are:
- getName() - returns the name of the command; this must not contain whitespace and must be unique.
- getUsage() - returns the usage string of the command; this should be one line and as short as possible (this is used for generating the help command output).
- getShortDescription() - returns a short description of the command; this should be one line and as short as possible (this is used for generating the help command output).
- execute() - executes the command's functionality using supplied command line and print streams.
Creating a Command
The following example creates a simple version of the start command.
A bundle activator class is needed for packaging the command servce; the bundle activator registers the command service in its start() method. Note: You do not need one activator per command, a single activator can register any number of commands.
To compile these classes you will need to have the framework.jar file on your class path. Compile all of the source files using a command like:
java -d c:\classes *.java
This command compiles all of the source files and outputs the generated class files into a subdirectory of the c:\classes directory, called test, named after the package of the source files; for the above command to work, the c:\classes directory must exist. Once you have compiled all of the above classes, you need to create a bundle JAR file of the generated package directory. The bundle JAR file needs a manifest, so create a file called manifest.mf with the following contents:
To create the bundle JAR file, issue the command:
jar cfm mystart.jar manifest.mf -C c:\classes test
This command creates a JAR file using the manifest you created and includes all of the classes in the test directory inside of the c:\classes directory. Once the bundle JAR file is created, you are ready to add the command service to the shell service; simply start Felix and install and start the bundle created by the above command. By doing so, the new mystart command is made available via the shell service.
Security and the Shell Service
The shell service security handling is quite simple, all security is handled by the standard OSGi framework mechanisms. For example, if a bundle should not be able to register a shell service, then it should not be given the corresponding service permission. Security handling may change in future release after some experience is gained through usage.
Feedback
Subscribe to the Felix users mailing list by sending a message to users-subscribe@felix.apache.org; after subscribing, email questions or feedback to users@felix.apache.org.