SK.gnome.dwarf.main
Class Console

java.lang.Object
  extended bySK.gnome.dwarf.GenericService
      extended bySK.gnome.dwarf.GenericServer
          extended bySK.gnome.dwarf.thread.ThreadServer
              extended bySK.gnome.dwarf.main.Console
All Implemented Interfaces:
Reportable, java.lang.Runnable, Server, Service

public class Console
extends ThreadServer

This class provides management console.

The console is a human interface through which the end-user can invoke arbitrary commands to manage the application services. Custom commands can be created by the developer and plugged-in to the application. All such commands must extend the abstract Command class and may be added to any Server implementation.

Commands contained by the Console service itself are globally accessible, while those plugged-in to the other servers are accessible only by choosing the particular server as the current working service. See the CSCmd class for more information.

The user commands are passed in to the execute(Subject, String, Writer) method, which maps them to the corresponding command services, found in the application.

This class implements one concrete way how to obtain a command string from the end-user - an interactive command prompt, which can be enabled or disabled via the setPrompt(boolean) method. Other ways can be freely developed and used, too.


Field Summary
protected  boolean prompt
          Whether to enable the interactive command prompt.
protected  int timeout
          The command prompt inactivity timeout in seconds.
 
Fields inherited from class SK.gnome.dwarf.thread.ThreadServer
daemon, thread
 
Fields inherited from class SK.gnome.dwarf.GenericService
initParameters, parent
 
Fields inherited from interface SK.gnome.dwarf.Service
INITIALIZED, LOG_DEBUG, LOG_ERROR, LOG_FATAL, LOG_INFO, LOG_TRACE, LOG_WARN, LOG_XFER, SHUTDOWN, STARTED, STOPPED
 
Constructor Summary
Console(java.lang.String name)
          Creates a new Console.
 
Method Summary
protected  boolean enableThread()
          Enables or disables the service thread.
 void execute(javax.security.auth.Subject subject, java.lang.String command, java.io.Writer out)
          Executes the particular console command.
 Service getServiceByPath(javax.security.auth.Subject subject, java.lang.String path)
          Returns the target service according to the given path and subject.
 Service getWorkingService(javax.security.auth.Subject subject)
          Returns the current working service of the given subject.
 void changeWorkingService(javax.security.auth.Subject subject, java.lang.String path)
          Changes the current working service of the given subject.
 void init(Server parent)
          Initializes the service.
protected  void loop()
          Handles the command prompt input.
static java.lang.String[] parseCommandString(java.lang.String cmd)
          Parses the command string.
 void setPrompt(boolean enable)
          Enables or disables the interactive command prompt.
 void setTimeout(int timeout)
          Sets the command prompt inactivity timeout.
 void start()
          Starts the service.
 
Methods inherited from class SK.gnome.dwarf.thread.ThreadServer
finish, run, setDaemon, shutdown, stop
 
Methods inherited from class SK.gnome.dwarf.GenericServer
addService, addService, getAuthenticator, getLogger, getParameters, getService, getServices, getServices, removeService, report
 
Methods inherited from class SK.gnome.dwarf.GenericService
getAuthFacility, getFullName, getInitParameter, getInitParameterNames, getLogFacility, getName, getPrincipal, getShutdownTimeout, getState, log, log, login, logout, setAuthFacility, setInitParameters, setLogFacility, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface SK.gnome.dwarf.Service
getFullName, getName, getState, log, log, login, logout
 

Field Detail

prompt

protected boolean prompt
Whether to enable the interactive command prompt.

Default value: true


timeout

protected int timeout
The command prompt inactivity timeout in seconds.

Zero means that the command prompt will wait for the user input indefinitelly.

Default value: Constants.DEFAULT_CONSOLE_TIMEOUT

Constructor Detail

Console

public Console(java.lang.String name)
Creates a new Console.

Method Detail

setPrompt

public void setPrompt(boolean enable)
Enables or disables the interactive command prompt.

Parameters:
enable - true if the command prompt should be enabled

setTimeout

public void setTimeout(int timeout)
Sets the command prompt inactivity timeout.

The command prompt will be deactivated and the console logging will be resumed automatically after the specified time of user inactivity. The command prompt may be activated again by pressing the Enter key.

Zero value means that the command prompt will wait for the user input indefinitelly.

Parameters:
timeout - the timeout in seconds

init

public void init(Server parent)
          throws ServiceException
Initializes the service.

It throws an IllegalServiceClassException if the parent server is not an instance of the MainServer class. If the command prompt is enabled, it initializes and starts a new thread for reading user commands from the standard System.in stream.

Specified by:
init in interface Service
Overrides:
init in class GenericServer
Throws:
ServiceException

start

public void start()
           throws ServiceException
Description copied from class: ThreadServer
Starts the service.

If the ThreadServer.enableThread() method returns true, it creates and starts a new thread. The thread's name corresponds to the service name and the thread's Runnable target is the service itself since the this class implements the Runnable interface. The reference to the thread object is stored in the ThreadServer.thread field.

It throws an IllegalStateException if the ThreadServer.thread field is not null and its isAlive() method returns true.

Specified by:
start in interface Service
Overrides:
start in class ThreadServer
Throws:
ServiceException

execute

public void execute(javax.security.auth.Subject subject,
                    java.lang.String command,
                    java.io.Writer out)
Executes the particular console command.

It parses the command string provided in the command argument. The actual parsing is performed in the parseCommandString(String) static method. The first array element returned by the parsing method is the name of the command. The command name must be the same as the name of the Command service, plugged-in to the application. The rest of the array elements are the command arguments. The particular command instance is obtained either from the Console itself, or from the current working service, associated with the given subject. Then the command's Command.execute(Subject, Service, String[], Writer) method is invoked and the command output is redirected to the out stream.

If a security manager is installed, the ServicePermission "executeCommand.cmd_name" is checked first in the context of the subject argument. The subject should represent an indentity of the user executing the command, therefore particular commands can be enabled or disabled for particular users by granting the appropriate subject-based permissions to them. In the case of invoking this method from a HTTP servlet, for example, the subject should represent a currently authenticated remote user.

Thread-safety note: This method implementation is not synchronized therefore the particular command's Command.execute(Subject, Service, String[], Writer) method must be thread-safe it will be accessed simultaneously.

Parameters:
subject - the subject executing the command
command - the command string
out - the output stream

getWorkingService

public Service getWorkingService(javax.security.auth.Subject subject)
Returns the current working service of the given subject.

Returns:
the service

changeWorkingService

public void changeWorkingService(javax.security.auth.Subject subject,
                                 java.lang.String path)
                          throws CommandException
Changes the current working service of the given subject.

Parameters:
subject - the subject
path - the path to the service
Throws:
CommandException

getServiceByPath

public Service getServiceByPath(javax.security.auth.Subject subject,
                                java.lang.String path)
Returns the target service according to the given path and subject.

The target service is searched relative to the subject's current working directory. The "." and ".." strings may be used in the path argument to reference the current and the parent directory. The "/" path separator must be used to separate the names of the nested services in the path.

Parameters:
subject - the subject
path - the path string
Returns:
the service or null if no such service exists

parseCommandString

public static java.lang.String[] parseCommandString(java.lang.String cmd)
Parses the command string.

The command string is a set of string elements, separated by the white space characters. If a command element must contain white space characters, it must be surrounded by double quotes. The first element is the name of the command, the rest is regarded as the command arguments.

Parameters:
cmd - the command string
Returns:
the command name plus the command arguments in the string array

enableThread

protected boolean enableThread()
Enables or disables the service thread.

Returns true if the command prompt is enabled.

Overrides:
enableThread in class ThreadServer
Returns:
true if the thread is enabled

loop

protected void loop()
Handles the command prompt input.

If the command prompt is enabled, it reads the user input from the command prompt (i.e. from the System.in stream), parses the command line and executes the command provided by the user via the execute(Subject, String, Writer) method.

The command prompt is activated by pressing the Enter key in the system console. If the command prompt is active, logging to the system console is suspended to prevent mixing the command output with the log messages generated by the application. By typing exit the command prompt is deactivated and console logging is resumed.

The subject passed in to the execute(Subject, String, Writer) method contains a ServicePrincipal object representing the console service itself and an instance of the UserPrincipal with the name of the local user executing the application.

Overrides:
loop in class ThreadServer


Copyright (c) 1999-2005, Gnome Ltd. All rights reserved.