WebReference.com - Part 1 of Extending the JXTA Shell, from Early Adopter JXTA (Wrox Press). (2/3) | 2 | WebReference

WebReference.com - Part 1 of Extending the JXTA Shell, from Early Adopter JXTA (Wrox Press). (2/3) | 2

To page 1current pageTo page 3
[previous] [next]

Early Adopter JXTA

Creating a Trivial Shell Extension

First, we will write a very simple shell extension in order to get to know the support system for writing new shell commands. Our simple shell extension is located in the package in the chapter code download called net.jxta.impl.shell.bin.date.date.java.

This extension provides a new JXTA shell command called date, and all it will do is print the current date and time. We use the java.text.DateFormat class from JDK 1.3 to pretty-print the date according to our default locale. The default instance of java.util.Calendar class is used to obtain the current time. Combining the functions of these two classes, we can print the current date using the line:

println(DateFormat.getDateTimeInstance().format(
  Calendar.getInstance().getTime()));

We'll see that line in context in a moment, but naturally, we have to build some infrastructure in order to actually incorporate it into a shell command. First, notice that as expected, it resides in the net.jxta.impl.shell.bin.date package.

package net.jxta.impl.shell.bin.date;

We need to import the classes we need for date operations, as well as the package containing shell support classes.

import net.jxta.impl.shell.*;
import java.util.Calendar;
import java.text.DateFormat;

Every shell command is a subclass of ShellApp; this is required. net.jxta.impl.shell.ShellApp is an abstract class that provides a lot of environment information and support to the shell command implementation. Here are some commonly used methods of ShellApp that can save the custom command implementer a lot of work:

Method NameDescription
getEnv()Get the shell environment, including read and write access to any environment variable
getGroup()Get the current group of the peer represented by the shell
getReturnVariable()Provides access to the environment variable on the left hand side of an assignment

Typically, the logic of the command is implemented in the startApp() method of the class. The startApp() and stopApp() methods are actually part of the net.jxta.impl.Module interface that is implemented by every JXTA service or application.

public class date extends ShellApp {
  public date() {
  }

Here, we simply print the current day and time when the command is called. println() calls an inherited method that writes to the application's default output stream, as provided by the shell.

  public int startApp (String[] args) {
    println(DateFormat.getDateTimeInstance()
      .format(Calendar.getInstance().getTime()));
    return ShellApp.appNoError; 
  }
  public void stopApp () {
  }

One other method that is frequently overridden in a shell command is getDescription(). This command is used by the man command, to obtain a short description for the command.

  public String getDescription() {
    return "Returns the current date and time.";
  }

The help() method provides detailed usage help to a user when they use the command man date.

  public void help() {
    println("NAME");
    println("   date - get the current date and time");
    println(" ");
    println("SYNOPSIS");
    println("   date");
    println(" ");
    println("DESCRIPTION");
    println(" ");
    println("   'date' prints the current date and 
            time to stdout.");
    println(" ");
    println("EXAMPLE");
    println("   JXTA>date");
    println(" ");
   }
}

As we can observe, writing a custom shell command is quite easy, we need only:


To page 1current pageTo page 3
[previous] [next]

Created: January 17, 2002
Revised: January 17, 2002


URL: https://webreference.com/programming/jxta/chap3/1/2.html