com.dalsemi.shell.server
Class Session

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--com.dalsemi.shell.server.Session
Direct Known Subclasses:
FTPSession, SerialSession, TelnetSession

public abstract class Session
extends Thread

A session represents a single connection to the TINI system shell. When someone attempts to login, a server object spins off a session (an implementation of this class) to handle the communication. The session allows you to log into the system, then processes all system requests. Sessions are designed to be multi-threaded, allowing multiple simultaneous connections.


Inner Class Summary
protected  class Session.Login
          Encapsulates all of the information needed to log a user into the system.
 
Field Summary
protected  String[] commandHistory
          A list of the last few commands issued in this session.
static String CURRENT_COMMAND
          The key used to index the current command in system environments.
static String CURRENT_DIRECTORY
          The key used to index the current directory in system environments.
protected  int currentCommand
          Marks the beginning of the commandHistory buffer.
protected  Hashtable environment
          The environment associated with this session.
protected  SystemPrintStream err
          The error stream of the remote connection.
protected  SystemInputStream in
          The input stream of the remote connection.
protected  boolean inCommand
          Specifies that a command is currently being processed by this session.
protected  int lastCommand
          Marks the end of the commandHistory buffer.
protected  Vector loginStack
          The list of users that are currently logged into this session.
protected  Object myThreadID
          The thread ID of this session.
protected  SystemPrintStream out
          The output stream of the remote connection.
protected  Object[] paramsArray
          Used as temporary storage when parsing the parameters for a command.
protected  Vector paramsVector
          Used as temporary storage when parsing the parameters for a command.
protected  String password
          The password for the user that is currently logged in.
protected  byte[] prompt
          The text used as the command line prompt for this session.
static String PROMPT
          The basic prompt displayed at the command line.
protected  Object[] retArray
          Used as temporary storage when parsing the parameters for a command.
protected  Server server
          The server that created this session.
protected  boolean shutdown
          Specifies that the session should attempt to close the connection after it has finished processing the current request.
protected  String userName
          The user that is currently logged in with this session.
static String welcomeMessage
          The message shown to all users when they login to this session.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
protected Session(SystemInputStream in, SystemPrintStream out, SystemPrintStream err, Server server)
          Intializes the session.
 
Method Summary
 void addToHistory(String str)
          Adds a new command to the history buffer.
 void broadcast(String sendThis)
          Displays a message in this session.
protected abstract  void currentCommandFinished()
          Notifies this session that the current command has completed.
 void endSession()
          Cleans up the resources used by this session.
protected abstract  void exceptionThrown(Exception ex)
          Notifies this session that exception was thrown when executing a command.
protected  void execute(String commandStr)
          Executes the given command in the current shell.
 void forceEndSession()
          Notifies the server this session is ending and forces the session to terminate.
 Hashtable getEnvironment()
          Gets a reference to the current environment.
 PrintStream getErrStream()
          Gets the stream this session uses for error notification and critical messages.
 String getFromEnvironment(String key)
          Gets the value of the key from the current environment.
 String getHistoryNumber(int number)
          Gets the command at the given index of the command history.
 String getNextCommand()
          Gets the next command from this session's input stream.
 PrintStream getOutputStream()
          Gets the stream this session uses for output.
static Object[] getParams(String str)
          Parses the command line into individual elements.
 String getUserName()
          Returns the user name associated with this session.
 boolean inCommand()
          Indicates whether this session is executing a shell command.
protected abstract  void login()
          Logs a user into the system.
protected  void parseLogin()
          Parses and executes the ".login" file for the current user.
 void printHistory(PrintStream out)
          Prints the list of commands stored in the history buffer of this session.
 void run()
          Starts the communication loop of the session.
protected abstract  void sessionEnding()
          Cleans up any resources associated with this session when it terminates.
 String stepDownHistory()
          Moves the current position in the history buffer down one and returns the command at that position.
 String stepUpHistory()
          Moves the current position in the history buffer up one and returns the command at that position.
 boolean su(String userName, String password)
          Allows the current user to login as another user.
 void updatePrompt(String withThis)
          Notifies this session of a directory change.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getName, getPriority, getThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

in

protected SystemInputStream in
The input stream of the remote connection.

out

protected SystemPrintStream out
The output stream of the remote connection.

err

protected SystemPrintStream err
The error stream of the remote connection.

inCommand

protected boolean inCommand
Specifies that a command is currently being processed by this session.

environment

protected Hashtable environment
The environment associated with this session.

shutdown

protected boolean shutdown
Specifies that the session should attempt to close the connection after it has finished processing the current request.

commandHistory

protected String[] commandHistory
A list of the last few commands issued in this session. This list is implemented as a circular buffer.

lastCommand

protected int lastCommand
Marks the end of the commandHistory buffer.

currentCommand

protected int currentCommand
Marks the beginning of the commandHistory buffer.

prompt

protected byte[] prompt
The text used as the command line prompt for this session.

server

protected Server server
The server that created this session.

userName

protected String userName
The user that is currently logged in with this session.

password

protected String password
The password for the user that is currently logged in.

loginStack

protected Vector loginStack
The list of users that are currently logged into this session.

myThreadID

protected Object myThreadID
The thread ID of this session.

paramsVector

protected Vector paramsVector
Used as temporary storage when parsing the parameters for a command.

paramsArray

protected Object[] paramsArray
Used as temporary storage when parsing the parameters for a command.

retArray

protected Object[] retArray
Used as temporary storage when parsing the parameters for a command.

CURRENT_DIRECTORY

public static String CURRENT_DIRECTORY
The key used to index the current directory in system environments.

CURRENT_COMMAND

public static String CURRENT_COMMAND
The key used to index the current command in system environments.

PROMPT

public static String PROMPT
The basic prompt displayed at the command line.

welcomeMessage

public static String welcomeMessage
The message shown to all users when they login to this session.
Constructor Detail

Session

protected Session(SystemInputStream in,
                  SystemPrintStream out,
                  SystemPrintStream err,
                  Server server)
Intializes the session.
Parameters:
in - stream this session should use to get data from user
out - stream this session should use to output to user
err - stream this session should use to output errors to user
server - the server in charge of this session
Method Detail

getUserName

public String getUserName()
Returns the user name associated with this session.
Returns:
the current user's name

login

protected abstract void login()
                       throws IOException
Logs a user into the system. This method is responsible for identifying and verifying the user. Typically this is done with a user name and password.

run

public final void run()
Starts the communication loop of the session. First the login() method is called. If the user is sucessfully logged into the system, commands are accepted and processed until the session is terminated.
Overrides:
run in class Thread

execute

protected void execute(String commandStr)
Executes the given command in the current shell. The commandStr parameter can contain any number of elements. These elements will be separated into String[] for the command interpreter.
Parameters:
commandStr - full command line to execute

getErrStream

public PrintStream getErrStream()
Gets the stream this session uses for error notification and critical messages.
Returns:
the session's PrintStream used for errors

getOutputStream

public PrintStream getOutputStream()
Gets the stream this session uses for output.
Returns:
the session's PrintStream

forceEndSession

public final void forceEndSession()
Notifies the server this session is ending and forces the session to terminate. This is used when some error has occurred that the session and server do not know how to handle.

endSession

public final void endSession()
Cleans up the resources used by this session.

su

public boolean su(String userName,
                  String password)
Allows the current user to login as another user. The old user's identity is added to a login stack. Once the new user terminates their session, the old user idenity is resumed.
Parameters:
userName - new user's name
password - new user's password
Returns:
true if login was successful, false otherwise

currentCommandFinished

protected abstract void currentCommandFinished()
Notifies this session that the current command has completed. For example, the user types "ls". The command is received and parsed, then the appropriate command is called in the shell. Finally, this session may need to again display the system prompt or perform other session-specific functions. Only call this from a synchronized block!!!

sessionEnding

protected abstract void sessionEnding()
Cleans up any resources associated with this session when it terminates.

exceptionThrown

protected abstract void exceptionThrown(Exception ex)
Notifies this session that exception was thrown when executing a command. This method will attempt to notify the user. Any exceptions raised specifically by shell commands should try to give as descriptive a message to the exception as possible.
Parameters:
ex - the exception thrown

getParams

public static Object[] getParams(String str)
Parses the command line into individual elements. The parser will use whitespace characters and quotes as delimiters.
Parameters:
str - complete command line
Returns:
an object array that contains the String command in the first element, and a String[] of the arguments to that command in the second element

getNextCommand

public String getNextCommand()
                      throws IOException
Gets the next command from this session's input stream. This effectively performs a readLine(), but gives this session a chance to parse the incoming data for special characters and commands.
Returns:
the next command from the input stream

getEnvironment

public Hashtable getEnvironment()
Gets a reference to the current environment.
Returns:
the current environment as a Hashtable

getFromEnvironment

public String getFromEnvironment(String key)
Gets the value of the key from the current environment.
Parameters:
key - name of desired environment variable
Returns:
the value specified by the given key, or null if that key does not exist in the environment

addToHistory

public void addToHistory(String str)
Adds a new command to the history buffer.
Parameters:
str - command to add to the history

stepUpHistory

public String stepUpHistory()
Moves the current position in the history buffer up one and returns the command at that position.
Returns:
the command one up from the current position in the history buffer

stepDownHistory

public String stepDownHistory()
Moves the current position in the history buffer down one and returns the command at that position.
Returns:
the command one down from the current position in the history buffer

getHistoryNumber

public String getHistoryNumber(int number)
Gets the command at the given index of the command history.
Parameters:
number - index into the history cache.
Returns:
the command specified by the given number

printHistory

public void printHistory(PrintStream out)
Prints the list of commands stored in the history buffer of this session.
Parameters:
out - stream used to print the history

inCommand

public boolean inCommand()
Indicates whether this session is executing a shell command.
Returns:
true if the session is executing a shell command

updatePrompt

public void updatePrompt(String withThis)
Notifies this session of a directory change. This allows the command line prompt to reflect the change.
Parameters:
withThis - the new directory name

broadcast

public void broadcast(String sendThis)
Displays a message in this session. This functionality may be disabled by setting the environment variable "BROADCASTS" to "false".
Parameters:
sendThis - message to display

parseLogin

protected void parseLogin()
Parses and executes the ".login" file for the current user. First, the current directory is searched for a directory that matches the user's name. If one is found, it becomes the current directory. The user's home directory is also set to this location. Next, that directory is checked for the existance of a ".login" file. If one is found, the file is executed using the "source" command.