org.infohazard.maverick
Class Dispatcher

java.lang.Object
  |
  +--javax.servlet.GenericServlet
        |
        +--javax.servlet.http.HttpServlet
              |
              +--org.infohazard.maverick.Dispatcher
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class Dispatcher
extends javax.servlet.http.HttpServlet

Dispatcher is the central command processor of the Maverick framework. All commands are routed to this servlet by way of extension mapping (say, *.m). From here requests are routed through the "workflow" tree of Command, View, and Pipeline objects built from the maverick config file.

Commands can be gracefully chained together; if a view references another Maverick command, the same MaverickContext object is used.

The Dispatcher object is made available to Controllers (or anyone else as an object in the application-scope aka ServletContext attribute collection. The attribute key is the value of the MAVERICK_APPLICATION_KEY constant.

Note that there is are two special pseudocommands defined by this servlet: "*" and "reload".

"reload" triggers a reload of the maverick config file. This can safely be done on running system; all commands currently being processed will complete using the old data. New command requests will use the new data as soon as it is finished loading. Note that the actual command name used for "reload" is determined by an init param.

"*" is a special command which can be defined in the config file. If a command request cannot be mapped to a command (because the requested command was not defined), the "*" command will be used instead. If there is no "*" command defined in the config file, bad requests return 404.

See Also:
Serialized Form

Field Summary
protected  java.util.Map commands
          Maps command name -> Command objects.
protected  org.jdom.Document configDocument
          The current configuration document.
protected static java.lang.String DEFAULT_CONFIG_FILE
          Default location of the xml configuration file.
protected  java.lang.String defaultRequestCharset
          If set, this string is set as the character encoding for http requests.
protected static java.lang.String INITPARAM_CONFIG_FILE
          Name of the servlet init parameter which defines the path to the Maverick configuration file.
protected static java.lang.String INITPARAM_CONFIG_TRANSFORM
          Name of the servlet init parameter which defines the path to a transform which will be applied to the config XML before loading.
protected static java.lang.String INITPARAM_CURRENT_CONFIG_COMMAND
          Name of the servlet init parameter which defines the name of the command which displays the current configuration.
protected static java.lang.String INITPARAM_DEFAULT_REQUEST_CHARSET
          Name of the servlet init parameter which defines the charset to use by default for request parameter decoding.
protected static java.lang.String INITPARAM_LIMIT_TRANSFORMS_PARAM
          The name of an http request parameter which, if specified, causes transformation to be halted after the specified number of transforms.
protected static java.lang.String INITPARAM_RELOAD_COMMAND
          Name of the servlet init parameter which defines the name of the reload command.
protected  java.lang.String limitTransformsParam
          If set, the name of a http request parameter which determines the number of transforms to run before halting.
static java.lang.String MAVERICK_APPLICATION_KEY
          The key in the Application (ServletContext) attribute collection under which the Dispatcher will be made available.
protected static java.lang.String SAVED_MAVCTX_KEY
          The MaverickContext object is stored in the request attributes with this key so that it can be recovered for recursive maverick execution.
 
Constructor Summary
Dispatcher()
           
 
Method Summary
protected  java.lang.String extractCommandName(javax.servlet.http.HttpServletRequest request)
          Extracts the command name from the request.
protected  Command getCommand(java.lang.String name)
          Returns the command object associated with the specified name.
 org.jdom.Document getConfigDocument()
           
 java.lang.String getLimitTransformsParam()
          The name of the parameter to use to limit transformation, or null if this should be disabled.
 void init()
          Initializes the Dispatcher by loading the config file.
protected  org.jdom.Document loadConfigDocument()
           
protected  void reloadConfig()
          Reloads the xml configuration data.
protected  void service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          The main entry point of the servlet; this processes an HTTP request.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAVERICK_APPLICATION_KEY

public static final java.lang.String MAVERICK_APPLICATION_KEY
The key in the Application (ServletContext) attribute collection under which the Dispatcher will be made available.

See Also:
Constant Field Values

INITPARAM_CONFIG_FILE

protected static final java.lang.String INITPARAM_CONFIG_FILE
Name of the servlet init parameter which defines the path to the Maverick configuration file. Defaults to DEFAULT_CONFIG_FILE.

See Also:
Constant Field Values

INITPARAM_CONFIG_TRANSFORM

protected static final java.lang.String INITPARAM_CONFIG_TRANSFORM
Name of the servlet init parameter which defines the path to a transform which will be applied to the config XML before loading. Defaults to null, which means perform no transformation.

See Also:
Constant Field Values

INITPARAM_RELOAD_COMMAND

protected static final java.lang.String INITPARAM_RELOAD_COMMAND
Name of the servlet init parameter which defines the name of the reload command. The value will typically be something like "reload".

See Also:
Constant Field Values

INITPARAM_CURRENT_CONFIG_COMMAND

protected static final java.lang.String INITPARAM_CURRENT_CONFIG_COMMAND
Name of the servlet init parameter which defines the name of the command which displays the current configuration. The value will typically be something like "currentConfig".

See Also:
Constant Field Values

INITPARAM_DEFAULT_REQUEST_CHARSET

protected static final java.lang.String INITPARAM_DEFAULT_REQUEST_CHARSET
Name of the servlet init parameter which defines the charset to use by default for request parameter decoding. If not present, the default charset will be whatever the container chooses (probably ISO-8859-1 aka Latin-1).

See Also:
Constant Field Values

INITPARAM_LIMIT_TRANSFORMS_PARAM

protected static final java.lang.String INITPARAM_LIMIT_TRANSFORMS_PARAM
The name of an http request parameter which, if specified, causes transformation to be halted after the specified number of transforms. If this initparam is not specified, this feature is disabled and all transforms will run to completion.

See Also:
Constant Field Values

DEFAULT_CONFIG_FILE

protected static final java.lang.String DEFAULT_CONFIG_FILE
Default location of the xml configuration file.

See Also:
Constant Field Values

SAVED_MAVCTX_KEY

protected static final java.lang.String SAVED_MAVCTX_KEY
The MaverickContext object is stored in the request attributes with this key so that it can be recovered for recursive maverick execution.

See Also:
Constant Field Values

commands

protected java.util.Map commands
Maps command name -> Command objects.


configDocument

protected org.jdom.Document configDocument
The current configuration document.


defaultRequestCharset

protected java.lang.String defaultRequestCharset
If set, this string is set as the character encoding for http requests. This affects the input form parameters. It is set from a servlet init parameter. Null means do nothing special.


limitTransformsParam

protected java.lang.String limitTransformsParam
If set, the name of a http request parameter which determines the number of transforms to run before halting.

Constructor Detail

Dispatcher

public Dispatcher()
Method Detail

init

public void init()
          throws javax.servlet.ServletException
Initializes the Dispatcher by loading the config file.

Overrides:
init in class javax.servlet.GenericServlet
javax.servlet.ServletException

service

protected void service(javax.servlet.http.HttpServletRequest request,
                       javax.servlet.http.HttpServletResponse response)
                throws java.io.IOException,
                       javax.servlet.ServletException
The main entry point of the servlet; this processes an HTTP request.

Overrides:
service in class javax.servlet.http.HttpServlet
java.io.IOException
javax.servlet.ServletException

extractCommandName

protected java.lang.String extractCommandName(javax.servlet.http.HttpServletRequest request)
Extracts the command name from the request. Extension and leading / will be removed.


reloadConfig

protected void reloadConfig()
                     throws ConfigException
Reloads the xml configuration data. Can be done on-the-fly. Any requests being serviced are allowed to complete with the old data.

ConfigException

getCommand

protected Command getCommand(java.lang.String name)
Returns the command object associated with the specified name. If the command is not found, a command with name "*" is returned. If there is no command with id "*", null is returned.


loadConfigDocument

protected org.jdom.Document loadConfigDocument()
                                        throws ConfigException
Returns:
a loaded JDOM document containing the configuration information.
ConfigException

getConfigDocument

public org.jdom.Document getConfigDocument()
Returns:
the current configuration as a JDOM Document.

getLimitTransformsParam

public java.lang.String getLimitTransformsParam()
The name of the parameter to use to limit transformation, or null if this should be disabled.