WebReference.com - Part 1 of Extending the JXTA Shell, from Early Adopter JXTA (Wrox Press). (1/3)
[next] |
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 net.jxta.impl.shell.bin
package.
This includes commands such as peers
, groups
, search
, etc. If
you examine the source code of the JXTA shell (available freely at https://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 net.jxta.impl.shell.bin.search
package.
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
net.jxta.impl.shell.bin
.
Figure 6 reveals how the JXTA Shell can be extended with our own custom commands.
Figure 6
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 thejxtashell.jar
with our custom JAR file.Use the
instjar
command within the shell to install the custom commands. Theinstjar
command 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.
[next] |
Created: January 17, 2002
Revised: January 17, 2002
URL: https://webreference.com/programming/jxta/chap3/1/