org.mortbay.http
Class HttpServer

java.lang.Object
  |
  +--java.beans.beancontext.BeanContextChildSupport
        |
        +--java.beans.beancontext.BeanContextSupport
              |
              +--org.mortbay.http.HttpServer
All Implemented Interfaces:
java.beans.beancontext.BeanContext, java.beans.beancontext.BeanContextChild, java.beans.beancontext.BeanContextServiceRevokedListener, java.beans.beancontext.BeanContextServicesListener, java.util.Collection, java.beans.DesignMode, java.util.EventListener, LifeCycle, java.beans.PropertyChangeListener, java.io.Serializable, java.beans.VetoableChangeListener, java.beans.Visibility
Direct Known Subclasses:
Server

public class HttpServer
extends java.beans.beancontext.BeanContextSupport
implements LifeCycle

HTTP Server. Services HTTP requests by maintaining a mapping between a collection of HttpListeners which generate requests and HttpContexts which contain collections of HttpHandlers. This class is configured by API calls. The org.mortbay.jetty.Server class uses XML configuration files to configure instances of this class. The HttpServer implements the BeanContext API so that membership events may be generated for HttpListeners, HandlerContexts and WebApplications.

Version:
$Id: HttpServer.java,v 1.7 2001/11/05 04:11:07 gregwilkins Exp $
Author:
Greg Wilkins (gregw)
See Also:
HttpContext, HttpHandler, HttpConnection, HttpListener, Server, Serialized Form

Fields inherited from class java.beans.beancontext.BeanContextChildSupport
beanContextChildPeer
 
Fields inherited from interface java.beans.beancontext.BeanContext
globalHierarchyLock
 
Fields inherited from interface java.beans.DesignMode
PROPERTYNAME
 
Constructor Summary
HttpServer()
          Constructor.
 
Method Summary
 HandlerContext addContext(java.lang.String contextPath)
          Create and add a new context.
 void addContext(java.lang.String host, HandlerContext context)
          Add a context.
 HandlerContext addContext(java.lang.String host, java.lang.String contextPathSpec)
          Create and add a new context.
 void addHostAlias(java.lang.String host, java.lang.String alias)
          Define a virtual host alias.
 void addListener(HttpListener listener)
          Add a HTTP Listener to the server.
 HttpListener addListener(InetAddrPort address)
          Create and add a SocketListener.
 HttpListener addListener(java.lang.String listenerClass)
          Add a HTTP Listener to the server.
 UserRealm addRealm(UserRealm realm)
           
 void destroy()
          Stop all listeners then handlers.
 HttpHandler findHandler(java.lang.Class handlerClass, java.lang.String uri, java.util.List hosts)
          Find handler.
 int getConnections()
           
 long getConnectionsDurationAve()
           
 long getConnectionsDurationMax()
           
 int getConnectionsOpen()
           
 int getConnectionsOpenMax()
           
 int getConnectionsRequestsAve()
           
 int getConnectionsRequestsMax()
           
 HandlerContext getContext(java.lang.String host, java.lang.String contextPath)
          Get or create context.
 HandlerContext getContext(java.lang.String host, java.lang.String contextPathSpec, int i)
           
 int getErrors()
           
 java.util.Set getHandlerContexts()
           
 java.util.Set getHandlers()
           
 java.util.Map getHostMap()
           
 HttpEncoding getHttpEncoding()
           
static java.util.List getHttpServerList()
           
 java.util.Collection getListeners()
           
 LogSink getLogSink()
          Deprecated. use getRequestLogSink()
 UserRealm getRealm(java.lang.String realmName)
           
 RequestLogFormat getRequestLogFormat()
           
 LogSink getRequestLogSink()
           
 int getRequests()
           
 int getRequestsActive()
           
 int getRequestsActiveMax()
           
 long getRequestsDurationAve()
           
 long getRequestsDurationMax()
           
 boolean getStatsOn()
           
 boolean isChunkingForced()
           
 boolean isDestroyed()
           
 boolean isStarted()
          Start all handlers then listeners.
static void main(java.lang.String[] args)
          Construct server from command line arguments.
 void removeContext(HandlerContext context)
          Remove a context or Web application.
 void removeContext(java.lang.String host, java.lang.String contextPathSpec, int i)
          Remove a context or Web application.
 void removeListener(HttpListener listener)
          Remove a HTTP Listener.
 UserRealm removeRealm(java.lang.String realmName)
           
 HandlerContext service(HttpRequest request, HttpResponse response)
          Service a request.
 void setChunkingForced(boolean forced)
          Set Chunking Forced.
 void setHttpEncoding(HttpEncoding httpEncoding)
          The HttpEncoding instance is used to extend the transport encodings supprted by this server.
 void setLogSink(LogSink logSink)
          Deprecated. use setRequestLogSink()
 void setRequestLogFormat(RequestLogFormat format)
          Set the requestLogFormat.
 void setRequestLogSink(LogSink logSink)
          Set the request log.
 void setStatsOn(boolean on)
           
 void start()
          Start all handlers then listeners.
 void statsReset()
          Reset statistics.
 void stop()
          Stop all listeners then handlers.
 
Methods inherited from class java.beans.beancontext.BeanContextSupport
add, addAll, addBeanContextMembershipListener, avoidingGui, clear, contains, containsAll, containsKey, dontUseGui, getBeanContextPeer, getLocale, getResource, getResourceAsStream, instantiateChild, isDesignTime, isEmpty, isSerializing, iterator, needsGui, okToUseGui, propertyChange, readChildren, remove, removeAll, removeBeanContextMembershipListener, retainAll, setDesignTime, setLocale, size, toArray, toArray, vetoableChange, writeChildren
 
Methods inherited from class java.beans.beancontext.BeanContextChildSupport
addPropertyChangeListener, addVetoableChangeListener, firePropertyChange, fireVetoableChange, getBeanContext, getBeanContextChildPeer, isDelegated, removePropertyChangeListener, removeVetoableChangeListener, serviceAvailable, serviceRevoked, setBeanContext, validatePendingSetBeanContext
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.beans.beancontext.BeanContextChild
addPropertyChangeListener, addVetoableChangeListener, getBeanContext, removePropertyChangeListener, removeVetoableChangeListener, setBeanContext
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Constructor Detail

HttpServer

public HttpServer()
Constructor.
Method Detail

getHttpEncoding

public HttpEncoding getHttpEncoding()
Returns:
The HttpEncoding helper instance.

setHttpEncoding

public void setHttpEncoding(HttpEncoding httpEncoding)
The HttpEncoding instance is used to extend the transport encodings supprted by this server.
Parameters:
httpEncoding - The HttpEncoding helper instance.

start

public void start()
           throws MultiException
Start all handlers then listeners. If a subcomponent fails to start, it's exception is added to a org.mortbay.util.MultiException and the start method continues.
Specified by:
start in interface LifeCycle
Throws:
MultiException - A collection of exceptions thrown by start() method of subcomponents of the HttpServer.

isStarted

public boolean isStarted()
Start all handlers then listeners.
Specified by:
isStarted in interface LifeCycle
Following copied from interface: org.mortbay.util.LifeCycle
Returns:
True if the LifeCycle has been started.

stop

public void stop()
          throws java.lang.InterruptedException
Stop all listeners then handlers.
Specified by:
stop in interface LifeCycle
Throws:
java.lang.InterruptedException - If interrupted, stop may not have been called on everything.

destroy

public void destroy()
Stop all listeners then handlers. All the handlers are unmapped and the listeners removed.
Specified by:
destroy in interface LifeCycle

isDestroyed

public boolean isDestroyed()
Specified by:
isDestroyed in interface LifeCycle
Following copied from interface: org.mortbay.util.LifeCycle
Returns:
True if the LifeCycle has been destroyed.

addListener

public HttpListener addListener(InetAddrPort address)
                         throws java.io.IOException
Create and add a SocketListener. Conveniance method.
Parameters:
address -  
Returns:
the HttpListener.
Throws:
java.io.IOException -  

addListener

public void addListener(HttpListener listener)
                 throws java.lang.IllegalArgumentException
Add a HTTP Listener to the server.
Parameters:
listener - The Listener.
Throws:
java.lang.IllegalArgumentException - If the listener is not for this server.

addListener

public HttpListener addListener(java.lang.String listenerClass)
                         throws java.lang.IllegalArgumentException
Add a HTTP Listener to the server.
Parameters:
listenerClass - The Listener classname, or null for the default Listener class.
Throws:
java.lang.IllegalArgumentException -  

removeListener

public void removeListener(HttpListener listener)
Remove a HTTP Listener.
Parameters:
listener -  

getListeners

public java.util.Collection getListeners()
Returns:
Set of all listeners.

addHostAlias

public void addHostAlias(java.lang.String host,
                         java.lang.String alias)
Define a virtual host alias. All requests to the alias are handled the same as request for the host.
Parameters:
host - Host name or IP
alias - Alias hostname or IP

addContext

public HandlerContext addContext(java.lang.String contextPath)
Create and add a new context. Note that multiple contexts can be created for the same host and contextPath. Requests are offered to multiple contexts in the order they where added to the HttpServer.
Parameters:
contextPath -  
Returns:
A HandlerContext instance created by a call to newHandlerContext.

addContext

public HandlerContext addContext(java.lang.String host,
                                 java.lang.String contextPathSpec)
Create and add a new context. Note that multiple contexts can be created for the same host and contextPath. Requests are offered to multiple contexts in the order they where added to the HttpServer.
Parameters:
host - Virtual hostname or null for all hosts.
contextPathSpec -  
Returns:
A HandlerContext instance created by a call to newHandlerContext.

addContext

public void addContext(java.lang.String host,
                       HandlerContext context)
Add a context. As contexts cannot be publicly created, this may be used to alias an existing context.
Parameters:
host - The virtual host or null for all hosts.
context -  

removeContext

public void removeContext(java.lang.String host,
                          java.lang.String contextPathSpec,
                          int i)
                   throws java.lang.IllegalStateException
Remove a context or Web application.
Parameters:
host - The virtual host or null for all hosts.
contextPathSpec -  
i - Index among contexts of same host and pathSpec.
Throws:
java.lang.IllegalStateException - if context not stopped

removeContext

public void removeContext(HandlerContext context)
                   throws java.lang.IllegalStateException
Remove a context or Web application.
Throws:
java.lang.IllegalStateException - if context not stopped

getContext

public HandlerContext getContext(java.lang.String host,
                                 java.lang.String contextPathSpec,
                                 int i)
Parameters:
host - The virtual host or null for all hosts.
contextPathSpec -  
i - Index among contexts of same host and pathSpec.
Returns:
The HandlerContext or null.

getContext

public HandlerContext getContext(java.lang.String host,
                                 java.lang.String contextPath)
Get or create context.
Parameters:
host - The virtual host or null for all hosts.
contextPath -  
Returns:
HandlerContext. If multiple contexts exist for the same host and pathSpec, the most recently added context is returned. If no context exists, a new context is created by a call to newHandlerContext.

getHandlers

public java.util.Set getHandlers()
Returns:
Collection of all handler from all contexts

getHandlerContexts

public java.util.Set getHandlerContexts()
Returns:
Collection of all handler.

getLogSink

public LogSink getLogSink()
Deprecated. use getRequestLogSink()


setLogSink

public void setLogSink(LogSink logSink)
Deprecated. use setRequestLogSink()


getRequestLogSink

public LogSink getRequestLogSink()

setRequestLogSink

public void setRequestLogSink(LogSink logSink)
Set the request log. Set the LogSink to be used for the request log.
Parameters:
logSink -  

getRequestLogFormat

public RequestLogFormat getRequestLogFormat()

setRequestLogFormat

public void setRequestLogFormat(RequestLogFormat format)
Set the requestLogFormat. Set the format instance to be used for formatting the request log. The default requestLogFormat is an extended NCSARequestLogFormat using the date format of the request LogSink in the GMT timezone.
Parameters:
format -  

service

public HandlerContext service(HttpRequest request,
                              HttpResponse response)
                       throws java.io.IOException,
                              HttpException
Service a request. Handle the request by passing it to the HttpHandler contained in the mapped HandlerContexts. The requests host and path are used to select a list of HandlerContexts. Each HttpHandler in these context is offered the request in turn, until the request is handled. If no handler handles the request, 404 Not Found is returned.
Parameters:
request -  
response -  
Returns:
The HandlerContext that completed handling of the request or null.
Throws:
java.io.IOException -  
HttpException -  

findHandler

public HttpHandler findHandler(java.lang.Class handlerClass,
                               java.lang.String uri,
                               java.util.List hosts)
Find handler. Find a handler for a URI. This method is provided for the servlet context getContext method to search for another context by URI. A list of hosts may be passed to qualify the search.
Parameters:
uri - URI that must be satisfied by the servlet handler
hosts - null or a list of virtual hosts names to search
Returns:
HttpHandler

addRealm

public UserRealm addRealm(UserRealm realm)

getRealm

public UserRealm getRealm(java.lang.String realmName)

removeRealm

public UserRealm removeRealm(java.lang.String realmName)

isChunkingForced

public boolean isChunkingForced()

setChunkingForced

public void setChunkingForced(boolean forced)
Set Chunking Forced. By default chunking is not forced on resources of known length.
Parameters:
forced - If true, chunking is used for all HTTP/1.1 responses, even if a content-length was known.

getHttpServerList

public static java.util.List getHttpServerList()

getHostMap

public java.util.Map getHostMap()

statsReset

public void statsReset()
Reset statistics.

setStatsOn

public void setStatsOn(boolean on)

getStatsOn

public boolean getStatsOn()
Returns:
True if statistics collection is turned on.

getConnections

public int getConnections()
Returns:
Number of connections accepted by the server since statsReset() called. Undefined if setStatsOn(false).

getConnectionsOpen

public int getConnectionsOpen()
Returns:
Number of connections currently open that were opened since statsReset() called. Undefined if setStatsOn(false).

getConnectionsOpenMax

public int getConnectionsOpenMax()
Returns:
Maximum number of connections opened simultaneously since statsReset() called. Undefined if setStatsOn(false).

getConnectionsDurationAve

public long getConnectionsDurationAve()
Returns:
Sliding average duration in milliseconds of open connections since statsReset() called. Undefined if setStatsOn(false).

getConnectionsDurationMax

public long getConnectionsDurationMax()
Returns:
Maximum duration in milliseconds of an open connection since statsReset() called. Undefined if setStatsOn(false).

getConnectionsRequestsAve

public int getConnectionsRequestsAve()
Returns:
Sliding average number of requests per connection since statsReset() called. Undefined if setStatsOn(false).

getConnectionsRequestsMax

public int getConnectionsRequestsMax()
Returns:
Maximum number of requests per connection since statsReset() called. Undefined if setStatsOn(false).

getErrors

public int getErrors()
Returns:
Number of errors generated while handling requests. since statsReset() called. Undefined if setStatsOn(false).

getRequests

public int getRequests()
Returns:
Number of requests since statsReset() called. Undefined if setStatsOn(false).

getRequestsActive

public int getRequestsActive()
Returns:
Number of requests currently active. Undefined if setStatsOn(false).

getRequestsActiveMax

public int getRequestsActiveMax()
Returns:
Maximum number of active requests since statsReset() called. Undefined if setStatsOn(false).

getRequestsDurationAve

public long getRequestsDurationAve()
Returns:
Average duration of request handling in milliseconds since statsReset() called. Undefined if setStatsOn(false).

getRequestsDurationMax

public long getRequestsDurationMax()
Returns:
Get maximum duration in milliseconds of request handling since statsReset() called. Undefined if setStatsOn(false).

main

public static void main(java.lang.String[] args)
Construct server from command line arguments.
Parameters:
args -  


Copyright © 2000 Mortbay Consulting Pty. Ltd. All Rights Reserved.