3.2 How to Create a Session Shell

This JNDI solution describes how to create a text-based browser that allows you to explore the Session Manager hierarchy. The SessionShell code was taken from the JNDIShell code and altered to work with the Session Manager package. A SessionShell demo utility, with accompanying source code, is provided to show the functionality of this browser solution.

3.2.1 Creating a Session Shell

The Session shell implementation consists of two major components (SessionShell class and shell class) with other supporting interfaces and classes.

SessionShell Class

The SessionShell class contains the Session Manager specific shell implementations. Sample source code is provided in the SessionShell.java file. The primary purpose of the SessionShell class is to control the shell state. The specific tasks included in this class are as follows:

  1. Defines the input stream from which commands are read and the output stream to which results are written.
  2. Defines the continueWStdIn flag and the verbose flag as described in the sample source code, and initializes the current session.
  3. Sets up a constructor for a child shell with the parent shell, initial session, input stream, and output stream as parameters.
  4. Sets up constructors for several SessionShell objects with different parameters, such as initial session, input stream, output stream, defined flags, and so forth.
  5. Sets up a loop in the main run method for reading in the command line from the input stream, checking and processing the command line, registering and de-registering commands, and writing the results to the output stream.
  6. Provides the following methods for streams operations, resolving names, registering and de-registering commands, and performing relative context operations.
    • addCommand(String, Command) - Binds a command to a string command name in the shell.
    • removeCommand(String) - Unbinds a command from a string command name in the shell.
    • getCommand(String) - Returns the command bound to the specified command name.
    • getCommandNames - Returns all command names bound to the shell.
    • clearCommands - Clears all bound commands in the shell.
    • getCurrSession - Returns the current session.
    • setCurrSession(Session) - Sets the current session to the specified Session.
    • setPrompt(String) - Sets the prompt to that specified.
    • getVerbose - Returns the verbose state.
    • setVerbose(boolean) - Sets the verbose state to TRUE or FALSE.
    • getInputStream - Returns the input stream attached to the shell.
    • getOutputStream - Returns the output stream attached to the shell.

shell Class

The shell class is a generic shell that does general initialization, parses the command line, and controls input and output. Its primary purpose is to set up the environment in which the SessionShell class can operate. This generic shell contains two principal methods (main and runShell) and 22 internal classes, which define the different default commands that are implemented.

Sample source code is provided in the shell.java file.

main Method

The primary purpose of the main method is to interpret any strings passed to it as command line parameters. It performs the following specific functions:

  • Takes as input a string array of arguments and sets up a command line parser for interpreting the strings passed in.
  • Initializes optional variables and flags (continueWStdIn and verbose), the shell.ini environment configuration file, and the input and output streams.
  • Looks for the initial session factory property, and sets the default to be "com.novell.service.session.nds.NDSInitialSessionFactory:" + "com.novell.service.session.bindery.BinderyInitialSessionFactory".
  • Adds several other properties as defaults, including any environment properties set in the shell.ini configuration startup file.
  • Loads and runs the shell with the default command bindings by calling the runShell method.

runShell Method

The primary purpose of the runShell method is to run the shell given a set of minimal options and the necessary input and output streams. It performs the following specific functions:

  • Sets up the environment for and spawns a SessionShell object according to the options specified.
  • Defines the default commands and binds the command-name pairs to the shell. These commands may be loaded and unloaded during the course of the shell execution. You can implement your own commands by creating a class that implements either the Command interface or the CommandWithHelp interface.
  • Runs the shell given a set of minimal options and the necessary input and output streams.

runShell Command Classes

A command class is included for each of the 22 default shell commands. Because each command class implements the CommandWithHelp interface, each has a getHelpStrs method for printing the command help information. These commands are bound to the shell, and because of the load and unload commands, all of them can be changed during the course of shell execution.

  • Load Command (load) adds a command class to the shell under a command name.
  • Unload Command (unload) removes a command from the shell.
  • Dir Command (dir) lists bindings of the current context or relative context.
  • Cd Command (cd) changes the current context to a relative context.
  • Find Command (find) changes the current session to a relative session via the findSession method.
  • Attr Command (attr) dumps all or specified attributes of a DirContext.
  • Debug Command (debug) turns debugging on and off.
  • Finalizer Command (final) runs the finalizer.
  • Trace Command (trace) turns instruction and/or method tracing on and off.
  • Close Command (close) closes the current session.
  • Keep Command (keep) keeps the current session.
  • Update Command (update) updates session links.
  • Loging Command (loging) attempts to log in to the domain using the GUI authenticator based only on user name.
  • Login Command (login) attempts to log in to the domain based on the username and password identity.
  • Logoutg) Command (logoutg) attempts to log out of the domain using the GUI authenticator based only on the user name.
  • Logout Command (logout) attempts to log out of the domain based on the user name and password identity.
  • Pause Command (pause) pauses the shell.
  • Shell Command (shell) executes a shell script.
  • Exec Command (exec) executes an OS-specific command.
  • VerboseToggle Command (v) toggles the verbose flag in the shell.
  • Exit Command (exit, quit, q) exits the shell.
  • Help Command (help, h, ?) prints help for all of the default bound commands.

Other Supporting Interfaces and Classes

The SessionShell class and the shell class uses the following additional interfaces and classes.

  • Command interface - Provides a generic interface for a command. A class that implements this interface must be registered with the shell. When the command is entered, all arguments are passed to the command through the execute method. This interface is used by both the SessionShell and shell classes.
  • CommandWithHelp interface - Provides a generic interface for a command that supports help through extending Help by adding the getHelpStrs method. A class that implements this interface must be registered with the shell. When the command is entered, all arguments are passed to the command through the execute method. This interface is used only by the shell class.
  • LineReader class - Provides support for reading text input lines from an arbitrary input stream or reader. This class is used only in the SessionShell class.
  • NullOutputStream class - Extends OutputStream and acts as a sink for bytes that should disappear. This class is used only by the SessionShell class.
  • ShellException class - Used to pass error messages from a command to the shell. If an error occurs in a command, a ShellException can be thrown with a message. The name of the command along with the message are then printed, and execution continues. If the exitShell flag is set, then the shell will exit. This class is used by both the SessionShell and shell classes.
  • shell.ini file - Contains the initial environment settings. This configuration file can be edited to use session providers other than those supplied by Novell by changing the com.novell.service.session.spi.InitialSessionFactory environment key to be different from the default.

3.2.2 Executing the SessionShell Demo

The SessionShell demo is designed for users who have a basic knowledge of the Session Manager. Basic session manager methods can be called. Some possible uses for SessionShell are:

  • As a tool to explore and discover how Session Manager works.
  • As a debugging tool, SessionShell allows you to save debugging time by providing calls to Session Manager methods. To test specific situations, run SessionShell and execute the desired command.

To run the SessionShell demo utility, follow the procedures given below.

Setting up SessionShell Locally

  1. Go to the c:\novell\Ndk\samples\njcl_sample\SessionShell directory.
  2. Compile SessionShell by entering the following at the command line:
       javac shell.java
       
  3. Run SessionShell by entering the following at the command line:
       java shell
       
  4. Get command line help by adding the '-?' line option. For example, at the command line, enter
       java shell -?
       

Implementing Your Own Commands

To implement your own commands, create a class that implements either the Command or the CommandWithHelp interfaces. Then load the command into the shell by typing

   load <cmdclass> <cmdname>
   

where <cmdclass> is the name of the class you created and <cmdname> is the command name to which you want to bind the class.

Changing the initial environment

To change the initial environment, edit the shell.ini file. To use different providers, change com.novell.service.session.spi.InitialSessionFactory to be different from the default.

Getting Help on Commands

For all commands in the shell, at the command line, enter

   ?
   

For one particular command, at the command line, enter

   ? <cmd>
   

3.2.3 SessionShell Source Code

This source code demonstrates how to create the functionality of a session shell that accepts commands from an input stream and executes these commands.