jhomenet.server.console
Class Session

java.lang.Object
  extended by java.lang.Thread
      extended by jhomenet.server.console.Session
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
LocalSession

public abstract class Session
extends java.lang.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. Id: $Id: $

Author:
Dave Irwin (jhomenet at gmail dot com)

Nested Class Summary
protected  class Session.Login
          Encapsulates all of the information needed to log a user into the system.
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  AuthManager authManager
          Reference to the authentication manager.
protected  java.lang.String[] commandHistory
          A list of the last few commands issued in this session.
protected  ConsoleService consoleService
          Reference to the console service.
static java.lang.String CURRENT_COMMAND
          The key used to index the current command in system environments.
static java.lang.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  java.util.Map<java.lang.String,java.lang.Object> environment
          The environment properties associated with this session.
protected  SystemPrintStream err
          The error stream of the remote connection.
protected  boolean inCommand
          Specifies that a command is currently being processed by this session.
protected  SystemInputStream inputStream
          The input stream of the remote connection.
protected  int lastCommand
          Marks the end of the commandHistory buffer.
protected  java.util.Vector<Session.Login> loginStack
          The list of users that are currently logged into this session.
protected static java.lang.String loginSuccessMessage
          The login success message.
protected  java.lang.Object myThreadID
          The thread ID of this session.
protected  SystemPrintStream outputStream
          The output stream of the remote connection.
protected  java.lang.Object[] paramsArray
          Used as temporary storage when parsing the parameters for a command.
protected  java.util.Vector paramsVector
          Used as temporary storage when parsing the parameters for a command.
protected  java.lang.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 java.lang.String PROMPT
          The basic prompt displayed at the command line.
protected  java.lang.Object[] retArray
          Used as temporary storage when parsing the parameters for a command.
protected  boolean shutdown
          Specifies that the session should attempt to close the connection after it has finished processing the current request.
protected  AuthManager.UserStatus user
          Reference to the user object.
protected  java.lang.String userName
          The user that is currently logged in with this session.
protected static java.lang.String version
          Session version information.
static java.lang.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(java.lang.String desc, AuthManager authManager, SystemInputStream in, SystemPrintStream out, SystemPrintStream err, ConsoleService consoleService)
          Intializes the session.
 
Method Summary
 void addToHistory(java.lang.String str)
          Adds a new command to the history buffer.
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(java.lang.Exception ex)
          Notifies this session that exception was thrown when executing a command.
 void execute(java.lang.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.
 void forceEndSession(java.util.concurrent.CountDownLatch sessionEndingLatch)
          Notifies the server this session is ending and forces the session to terminate.
 java.util.Map getEnvironment()
          Gets a reference to the current properties.
 java.io.PrintStream getErrStream()
          Gets the stream this session uses for error notification and critical messages.
 java.lang.String getFromEnvironment(java.lang.String key)
          Gets the value of the key from the current properties.
 java.lang.String getHistoryNumber(int number)
          Gets the command at the given index of the command history.
 java.lang.String getNextCommand()
          Gets the next command from this session's input stream.
 java.io.PrintStream getOutputStream()
          Gets the stream this session uses for output.
static java.lang.Object[] getParams(java.lang.String str)
          Parses the command line into individual elements.
 java.lang.String getUserName()
          Returns the user location 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(java.io.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()
          Notify session implementations that the session is ending.
 java.lang.String stepDownHistory()
          Moves the current position in the history buffer down one and returns the command at that position.
 java.lang.String stepUpHistory()
          Moves the current position in the history buffer up one and returns the command at that position.
 boolean su(java.lang.String userName, java.lang.String password)
          Allows the current user to login as another user.
 void updatePrompt(java.lang.String withThis)
          Notifies this session of a directory change.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, 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

inputStream

protected SystemInputStream inputStream
The input stream of the remote connection.


outputStream

protected SystemPrintStream outputStream
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 java.util.Map<java.lang.String,java.lang.Object> environment
The environment properties 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 java.lang.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.


authManager

protected final AuthManager authManager
Reference to the authentication manager.


consoleService

protected final ConsoleService consoleService
Reference to the console service.


user

protected AuthManager.UserStatus user
Reference to the user object.


userName

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


password

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


loginStack

protected java.util.Vector<Session.Login> loginStack
The list of users that are currently logged into this session.


myThreadID

protected java.lang.Object myThreadID
The thread ID of this session.


paramsVector

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


paramsArray

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


retArray

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


CURRENT_DIRECTORY

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


CURRENT_COMMAND

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


PROMPT

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


welcomeMessage

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


version

protected static final java.lang.String version
Session version information.

See Also:
Constant Field Values

loginSuccessMessage

protected static final java.lang.String loginSuccessMessage
The login success message.

See Also:
Constant Field Values
Constructor Detail

Session

protected Session(java.lang.String desc,
                  AuthManager authManager,
                  SystemInputStream in,
                  SystemPrintStream out,
                  SystemPrintStream err,
                  ConsoleService consoleService)
Intializes the session.

Parameters:
desc - session description
loginService - Reference to the login service
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
consoleService - reference to the console service
Method Detail

getUserName

public java.lang.String getUserName()
Returns the user location associated with this session.

Returns:
the current user's location

login

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

Throws:
java.io.IOException

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.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread
See Also:
Thread.run()

getNextCommand

public java.lang.String getNextCommand()
                                throws java.io.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. This method will block until an end of line character is received by the input stream.

Returns:
the next command from the input stream
Throws:
java.io.IOException

execute

public final void execute(java.lang.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 java.io.PrintStream getErrStream()
Gets the stream this session uses for error notification and critical messages.

Returns:
the session's PrintStream used for errors

getOutputStream

public java.io.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.


forceEndSession

public final void forceEndSession(java.util.concurrent.CountDownLatch sessionEndingLatch)
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.

Parameters:
sessionEndingLatch -

endSession

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


su

public boolean su(java.lang.String userName,
                  java.lang.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 location
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()
Notify session implementations that the session is ending. This allows implementing sessions to clean up any resources associated with this session when it terminates.


exceptionThrown

protected abstract void exceptionThrown(java.lang.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 java.lang.Object[] getParams(java.lang.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

getEnvironment

public java.util.Map getEnvironment()
Gets a reference to the current properties.

Returns:
the current properties as a Hashtable

getFromEnvironment

public java.lang.String getFromEnvironment(java.lang.String key)
Gets the value of the key from the current properties.

Parameters:
key - location of desired properties variable
Returns:
the value specified by the given key, or null if that key does not exist in the properties

addToHistory

public void addToHistory(java.lang.String str)
Adds a new command to the history buffer.

Parameters:
str - command to add to the history

stepUpHistory

public java.lang.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 java.lang.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 java.lang.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(java.io.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(java.lang.String withThis)
Notifies this session of a directory change. This allows the command line prompt to reflect the change.

Parameters:
withThis - the new directory location

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.



Copyright © 2004-2007 David Irwin. All Rights Reserved.