WebReference.com - Part 1 of Extending the JXTA Shell, from Early Adopter JXTA (Wrox Press). (1/3)
Early Adopter JXTA
Custom Commands to the Rescue
Custom commands enable advanced users to:
Modify the behaviour of existing commands (since source code is available), creating versions of commands that better fit specific requirements
Create completely new commands addressing areas that the shell command designer did not think of, or decided not to implement
Adapt the JXTA shell to work with a specific P2P system that is built on top of JXTA
Test new JXTA service and application code without creating complex user interfaces
Create completely customized and embedded shells within an application
There are certainly many other possibilities. It should be obvious that the JXTA shell (and the code body that it encompasses) is much more than just a learning tool for JXTA newbies. Let's take a look at how we go about extending the shell and creating our own commands.
Basic Mechanism of Shell Extension
All of the shell's commands are part of the
This includes commands such as
search, etc. If
you examine the source code of the JXTA shell (available freely at http://www.jxta.org),
you will notice that they are all individual Java source files under their own package with the same
name. For example,
search.java is in the
During run time, the JXTA shell uses introspection to enumerate all of these commands and make them available to the user. Any time the user types in a command, these commands are re-scanned to check if an implementation is available.
To extend the JXTA shell, we simply add our own command implementation packages under
Figure 6 reveals how the JXTA Shell can be extended with our own custom commands.
In the diagram, we can see that our custom commands' classes must be "installed" into the
net.jxta.impl.shell.bin package. This can be done in at least two different ways:
Ensure that the Java VM running the JXTA shell has the JAR file(s) or directories containing our custom commands in its
CLASSPATH. This will effectively merge the classes of the
jxtashell.jarwith our custom JAR file.
instjarcommand within the shell to install the custom commands. The
instjarcommand uses Java's dynamic class loading capabilities to install the custom commands into place.
The second alternative is preferred, since it is directly supported by the shell itself,
and does not require fiddling with the external Java VM's
CLASSPATH. This makes it much
less dependent on configuration and less prone to setup errors. Furthermore, it can be done at anytime
after the JXTA shell has already started Â and commands installed with
instjar can be easily
uninstalled using the
uninstjar command. This means commands can be installed, tested,
and uninstalled as part of their development process, without repeatedly launching the shell.
All that remains now is for us to actually write some custom command implementation code.
Created: January 17, 2002
Revised: January 17, 2002