Class DynamicConfigHandler
- java.lang.Object
-
- sunlabs.brazil.handler.DynamicConfigHandler
-
- All Implemented Interfaces:
Handler
public class DynamicConfigHandler extends java.lang.Object implements Handler
TheDynamicConfigHandlerallows the user to change the configuration of the server and its handlers on the fly. This handler can therefore be used to swap in and out functionality, for example, by dynamically adding a newAuthHandlerto add a new form of authentication to a running server.This handler uses a special set of URLs to allow a new set of configuration properties to be uploaded. The new configuration replaces the old configuration.
The name of another
Handleris supplied when thisDynamicConfigHandleris initialized. ThisHandleris the helper or sub-handler for theDynamicConfigHandler. When theDynamicConfigHandlerreceives a regular HTTP request (that matches the URL prefix described below), it redirects that request to therespondmethod of the sub-handler.The uploaded configuration properties are kept in a separate properties object from the server's properties. The server's properties are in fact not accessible from the sub-handler; the sub-handler can only access and/or change the properties owned by the
DynamicConfigHandler.This handler uses the following configuration properties:
- handler
- The name of the initial sub-handler that this
DynamicConfigHandlerwill use to process requests. When new properties are uploaded, the sub-handler will be replaced with whatever is specified in the newly uploadedhandlerproperty. - prefix
- Only URLs beginning with this string will be redirected to the
sub-handler. This property belongs to the
DynamicConfigHandlerand is not changed when new properties are uploaded. The default is "/". - config
- URLs beginning with this prefix can be used to upload or download
new configuration properties to this handler, which will also
dynamically change which sub-handler is installed. This property
belongs to the
DynamicConfigHandlerand is not changed when new properties are uploaded. The default is "/config/".Properties may be uploaded by sending them as "name=value" pairs in the body of a POST or in the "?" query parameters. The URL for uploading properties is "config/set".
The current set of properties may be retrieved from this handler by sending the URL "config/get"
handler=sunlabs.brazil.server.ChainHandler port=8081 log=5 handlers=dyn cgi dyn.class=sunlabs.brazil.server.DynamicConfigHandler dyn.prefix=/sparky/ dyn.config=/config-sparky/ dyn.handler=chain chain.class=sunlabs.brazil.server.ChainHandler chain.handlers=foo baz garply foo.class=sunlabs.brazil.handler.HomeDirHandler foo.home=/home/users/ baz.class=sunlabs.brazil.handler.FileHandler garply.class=sunlabs.brazil.handler.NotFoundHandler garply.root="/errors/" garply.fileName="nofile.html" cgi.class = sunlabs.brazil.handler.CgiHandler . . .
These parameters set up a normalServeron port 8081, running aChainHandlerwhich dispatches to aDynamicConfigHandlerand aCgiHandler.The
DynamicConfigHandlerwill listen for HTTP requests beginning with "/sparky/" and dispatch to its dynamically generated list of handlers, and listen for requests beginning with "/config-sparky/" to dynamically change that set of handlers.To give this
DynamicConfigHandlersomething to do, an initial set of handlers is provided with the same prefix ("dyn") as theDynamicConfigHandleritself. The prefix is stripped off those properties and the revised set of properties is passed to theDynamicConfigHandlerto initialize its dynamically configurable world.- Version:
- 2.1, 02/10/01
- Author:
- Colin Stevens (colin.stevens@sun.com)
-
-
Constructor Summary
Constructors Constructor Description DynamicConfigHandler()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleaninit(Server server, java.lang.String prefix)Initializes thisDynamicConfigHandlerby loading the initial handler.booleanrespond(Request request)Responds to an HTTP request by examining the "Host:" request header and dispatching to the main handler of the server that handles that virtual host.
-
-
-
Method Detail
-
init
public boolean init(Server server, java.lang.String prefix)
Initializes thisDynamicConfigHandlerby loading the initial handler. An initial handler does not need to be defined, however, since the handler configuration can be downloaded later.- Specified by:
initin interfaceHandler- Parameters:
server- The HTTP server that created this handler.prefix- A prefix to prepend to all of the keys that this handler uses to extract configuration information.- Returns:
falseif the initial handler was specified but could not be initialized,trueotherwise.
-
respond
public boolean respond(Request request) throws java.io.IOException
Responds to an HTTP request by examining the "Host:" request header and dispatching to the main handler of the server that handles that virtual host. If the "Host:" request header was not specified, or named a virtual host that was not initialized ininitfrom the list of virtual hosts, this method returns without handling the request.- Specified by:
respondin interfaceHandler- Parameters:
request- The HTTP request to be forwarded to one of the sub-servers.- Returns:
trueif the sub-server handled the message,falseif it did not.falseis also returned if the "Host:" was unspecified or unknown.- Throws:
java.io.IOException- if there was an I/O error while sending the response to the client. Typically, in that case, theServerwill (try to) send an error message to the client and then close the client's connection.The
IOExceptionshould not be used to silently ignore problems such as being unable to access some server-side resource (for example getting aFileNotFoundExceptiondue to not being able to open a file). In that case, theHandler's duty is to turn thatIOExceptioninto a HTTP response indicating, in this case, that a file could not be found.
-
-