org.mortbay.http
Class HttpServer

java.lang.Object
  extended by org.mortbay.util.Container
      extended by org.mortbay.http.HttpServer
All Implemented Interfaces:
java.io.Serializable, EventProvider, LifeCycle
Direct Known Subclasses:
Server

public class HttpServer
extends Container
implements LifeCycle, EventProvider, java.io.Serializable

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, HttpContexts and WebApplications.

Version:
$Id: HttpServer.java,v 1.70 2005/12/04 11:43:21 gregwilkins Exp $
Author:
Greg Wilkins (gregw)
See Also:
HttpContext, HttpHandler, HttpConnection, HttpListener, Server, Serialized Form

Constructor Summary
HttpServer()
          Constructor.
HttpServer(boolean anonymous)
          Constructor.
 
Method Summary
 HttpContext addContext(HttpContext context)
          Add a context.
 HttpContext addContext(java.lang.String contextPath)
          Create and add a new context.
 HttpContext addContext(java.lang.String virtualHost, HttpContext context)
          Add a context.
 HttpContext addContext(java.lang.String virtualHost, java.lang.String contextPathSpec)
          Create and add a new context.
 void addHostAlias(java.lang.String virtualHost, java.lang.String alias)
          Deprecated. Use HttpContext.addVirtualHost
 HttpListener addListener(HttpListener listener)
          Add a HTTP Listener to the server.
 HttpListener addListener(InetAddrPort address)
          Create and add a SocketListener.
 HttpListener addListener(java.lang.String address)
          Create and add a SocketListener.
 UserRealm addRealm(UserRealm realm)
           
 void destroy()
          Destroy a stopped server.
protected  void doStart()
          Start all handlers then listeners.
protected  void doStop()
          Stop all listeners then all contexts.
 HttpHandler findHandler(java.lang.Class handlerClass, java.lang.String uri, java.lang.String[] vhosts)
          Find handler.
 int getConnections()
           
 long getConnectionsDurationAve()
           
 long getConnectionsDurationMax()
           
 long getConnectionsDurationMin()
           
 long getConnectionsDurationTotal()
           
 int getConnectionsOpen()
           
 int getConnectionsOpenMax()
           
 int getConnectionsOpenMin()
           
 int getConnectionsRequestsAve()
           
 int getConnectionsRequestsMax()
           
 int getConnectionsRequestsMin()
           
 HttpContext getContext(java.lang.String contextPathSpec)
          Get or create context.
 HttpContext getContext(java.lang.String virtualHost, java.lang.String contextPathSpec)
          Get or create context.
 HttpContext getContext(java.lang.String virtualHost, java.lang.String contextPathSpec, int i)
          Get specific context.
 HttpContext[] getContexts()
           
 int getErrors()
           
 java.util.Map getHostMap()
           
static java.util.List getHttpServerList()
          Deprecated. User getHttpServers()
static java.util.Collection getHttpServers()
          Get HttpServer Collection.
 HttpListener[] getListeners()
           
 UserRealm getRealm(java.lang.String realmName)
          Get a named UserRealm.
 RequestLog getRequestLog()
           
 int getRequests()
           
 int getRequestsActive()
           
 int getRequestsActiveMax()
           
 int getRequestsActiveMin()
           
 long getRequestsDurationAve()
           
 long getRequestsDurationMax()
           
 long getRequestsDurationMin()
           
 long getRequestsDurationTotal()
           
 int getRequestsPerGC()
          Get the requests per GC.
 boolean getResolveRemoteHost()
           
 java.lang.String[] getServerClasses()
          Get system classes.
 boolean getStatsOn()
           
 long getStatsOnMs()
           
 boolean getStopGracefully()
           
 java.lang.String[] getSystemClasses()
          Get system classes.
 boolean getTrace()
           
 void join()
          Join the listeners.
static void main(java.lang.String[] args)
          Construct server from command line arguments.
protected  HttpContext newHttpContext()
          Create a new HttpContext.
 boolean removeContext(HttpContext context)
          Remove a context or Web application.
 void removeListener(HttpListener listener)
          Remove a HTTP Listener.
 UserRealm removeRealm(java.lang.String realmName)
           
 void save(java.lang.String saveat)
          Save the HttpServer The server is saved by serialization to the given filename or URL.
 HttpContext service(HttpRequest request, HttpResponse response)
          Service a request.
 void setAnonymous(boolean anonymous)
           
 void setContexts(HttpContext[] contexts)
           
 void setListeners(HttpListener[] listeners)
           
 void setRequestLog(RequestLog log)
          Set the request log.
 void setRequestsPerGC(int requestsPerGC)
          Set the requests per GC.
 void setResolveRemoteHost(boolean resolveRemoteHost)
           
 void setServerClasses(java.lang.String[] classes)
          Set system classes.
 void setStatsOn(boolean on)
           
 void setStopGracefully(boolean graceful)
           
 void setSystemClasses(java.lang.String[] classes)
          Set system classes.
 void setTrace(boolean trace)
           
 void statsReset()
          Reset statistics.
 void stop(boolean graceful)
          Stop all listeners then all contexts.
 
Methods inherited from class org.mortbay.util.Container
addComponent, addEventListener, getComponents, isFailed, isStarted, isStarting, isStopping, removeComponent, removeEventListener, start, stop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.mortbay.util.LifeCycle
isStarted, start, stop
 
Methods inherited from interface org.mortbay.util.EventProvider
addEventListener, removeEventListener
 

Constructor Detail

HttpServer

public HttpServer()
Constructor.


HttpServer

public HttpServer(boolean anonymous)
Constructor.

Parameters:
anonymous - If true, the server is not included in the static server lists and stopAll methods.
Method Detail

getHttpServers

public static java.util.Collection getHttpServers()
Get HttpServer Collection. Get a collection of all known HttpServers. Servers can be removed from this list with the setAnonymous call.

Returns:
Collection of all servers.

getHttpServerList

public static java.util.List getHttpServerList()
Deprecated. User getHttpServers()


setAnonymous

public void setAnonymous(boolean anonymous)
Parameters:
anonymous - If true, the server is not included in the static server lists and stopAll methods.

setStopGracefully

public void setStopGracefully(boolean graceful)

getStopGracefully

public boolean getStopGracefully()

setListeners

public void setListeners(HttpListener[] listeners)
Parameters:
listeners - Array of HttpListeners.

getListeners

public HttpListener[] getListeners()
Returns:
Array of HttpListeners.

addListener

public HttpListener addListener(java.lang.String address)
                         throws java.io.IOException
Create and add a SocketListener. Conveniance method.

Parameters:
address -
Returns:
the HttpListener.
Throws:
java.io.IOException

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 HttpListener 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.

removeListener

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

Parameters:
listener -

setContexts

public void setContexts(HttpContext[] contexts)

getContexts

public HttpContext[] getContexts()

addContext

public HttpContext addContext(HttpContext context)
Add a context.

Parameters:
context -

removeContext

public boolean removeContext(HttpContext context)
                      throws java.lang.IllegalStateException
Remove a context or Web application.

Throws:
java.lang.IllegalStateException - if context not stopped

addContext

public HttpContext addContext(java.lang.String virtualHost,
                              HttpContext context)
Add a context. As contexts cannot be publicly created, this may be used to alias an existing context.

Parameters:
virtualHost - The virtual host or null for all hosts.
context -

addContext

public HttpContext addContext(java.lang.String contextPath)
Create and add a new context. Note that multiple contexts can be created for the same virtualHost and contextPath. Requests are offered to multiple contexts in the order they where added to the HttpServer.

Parameters:
contextPath -
Returns:
A HttpContext instance created by a call to newHttpContext.

addContext

public HttpContext addContext(java.lang.String virtualHost,
                              java.lang.String contextPathSpec)
Create and add a new context. Note that multiple contexts can be created for the same virtualHost and contextPath. Requests are offered to multiple contexts in the order they where added to the HttpServer.

Parameters:
virtualHost - Virtual hostname or null for all hosts.
contextPathSpec - Path specification relative to the context path.
Returns:
A HttpContext instance created by a call to newHttpContext.

getContext

public HttpContext getContext(java.lang.String virtualHost,
                              java.lang.String contextPathSpec,
                              int i)
Get specific context.

Parameters:
virtualHost - The virtual host or null for all hosts.
contextPathSpec - Path specification relative to the context path.
i - Index among contexts of same virtualHost and pathSpec.
Returns:
The HttpContext or null.

getContext

public HttpContext getContext(java.lang.String virtualHost,
                              java.lang.String contextPathSpec)
Get or create context.

Parameters:
virtualHost - The virtual host or null for all hosts.
contextPathSpec -
Returns:
HttpContext. If multiple contexts exist for the same virtualHost and pathSpec, the most recently added context is returned. If no context exists, a new context is created by a call to newHttpContext.

getContext

public HttpContext getContext(java.lang.String contextPathSpec)
Get or create context.

Parameters:
contextPathSpec - Path specification relative to the context path.
Returns:
The HttpContext If multiple contexts exist for the same pathSpec, the most recently added context is returned. If no context exists, a new context is created by a call to newHttpContext.

newHttpContext

protected HttpContext newHttpContext()
Create a new HttpContext. Specialized HttpServer classes may override this method to return subclasses of HttpContext.

Returns:
A new instance of HttpContext or a subclass of HttpContext

getTrace

public boolean getTrace()
Returns:
True if the TRACE method is fully implemented.

setTrace

public void setTrace(boolean trace)
Parameters:
trace - True if the TRACE method is fully implemented.

getRequestsPerGC

public int getRequestsPerGC()
Get the requests per GC. If this is set greater than zero, then the System garbage collector will be invoked after approximately this number of requests. For predictable response, it is often best to have frequent small runs of the GC rather than infrequent large runs. The request count is only approximate as it is not synchronized and multi CPU machines may miss counting some requests.

Returns:
Approx requests per garbage collection.

setRequestsPerGC

public void setRequestsPerGC(int requestsPerGC)
Set the requests per GC. If this is set greater than zero, then the System garbage collector will be invoked after approximately this number of requests. For predictable response, it is often best to have frequent small runs of the GC rather than infrequent large runs. The request count is only approximate as it is not synchronized and multi CPU machines may miss counting some requests.

Parameters:
requestsPerGC - Approx requests per garbage collection.

setSystemClasses

public void setSystemClasses(java.lang.String[] classes)
Set system classes.


getSystemClasses

public java.lang.String[] getSystemClasses()
Get system classes.


setServerClasses

public void setServerClasses(java.lang.String[] classes)
Set system classes.


getServerClasses

public java.lang.String[] getServerClasses()
Get system classes.


doStart

protected void doStart()
                throws java.lang.Exception
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:
doStart in class Container
Throws:
MultiException - A collection of exceptions thrown by start() method of subcomponents of the HttpServer.
java.lang.Exception

doStop

protected void doStop()
               throws java.lang.InterruptedException
Stop all listeners then all contexts. Equivalent to stop(false);

Specified by:
doStop in class Container
Throws:
java.lang.InterruptedException - If interrupted, stop may not have been called on everything.

stop

public void stop(boolean graceful)
          throws java.lang.InterruptedException
Stop all listeners then all contexts.

Parameters:
graceful - If true and statistics are on for a context, then this method will wait for requestsActive to go to zero before stopping that context.
Throws:
java.lang.InterruptedException

join

public void join()
          throws java.lang.InterruptedException
Join the listeners. Join all listeners that are instances of ThreadPool.

Throws:
java.lang.InterruptedException

addHostAlias

public void addHostAlias(java.lang.String virtualHost,
                         java.lang.String alias)
Deprecated. Use HttpContext.addVirtualHost

Define a virtual host alias. All requests to the alias are handled the same as request for the virtualHost.

Parameters:
virtualHost - Host name or IP
alias - Alias hostname or IP

setRequestLog

public void setRequestLog(RequestLog log)
Set the request log.

Parameters:
log - RequestLog to use.

getRequestLog

public RequestLog getRequestLog()

service

public HttpContext 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 HttpContexts. The requests host and path are used to select a list of HttpContexts. 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 HttpContext 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.lang.String[] vhosts)
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
vhosts - 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)
Get a named UserRealm.

Parameters:
realmName - The name of the realm or null.
Returns:
The named realm. If the name is null and only a single realm is known, that is returned.

removeRealm

public UserRealm removeRealm(java.lang.String realmName)

getHostMap

public java.util.Map getHostMap()

getResolveRemoteHost

public boolean getResolveRemoteHost()
Returns:
True if the remote host name of connections is resolved.

setResolveRemoteHost

public void setResolveRemoteHost(boolean resolveRemoteHost)
Parameters:
resolveRemoteHost - True if the remote host name of connections is resolved.

statsReset

public void statsReset()
Reset statistics.


setStatsOn

public void setStatsOn(boolean on)

getStatsOn

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

getStatsOnMs

public long getStatsOnMs()
Returns:
Timestamp stats were started at.

getConnectionsDurationMin

public long getConnectionsDurationMin()
Returns:
Returns the connectionsDurationMin.

getConnectionsDurationTotal

public long getConnectionsDurationTotal()
Returns:
Returns the connectionsDurationTotal.

getConnectionsOpenMin

public int getConnectionsOpenMin()
Returns:
Returns the connectionsOpenMin.

getConnectionsRequestsMin

public int getConnectionsRequestsMin()
Returns:
Returns the connectionsRequestsMin.

getRequestsActiveMin

public int getRequestsActiveMin()
Returns:
Returns the requestsActiveMin.

getRequestsDurationMin

public long getRequestsDurationMin()
Returns:
Returns the requestsDurationMin.

getRequestsDurationTotal

public long getRequestsDurationTotal()
Returns:
Returns the requestsDurationTotal.

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:
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:
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).

save

public void save(java.lang.String saveat)
          throws java.net.MalformedURLException,
                 java.io.IOException
Save the HttpServer The server is saved by serialization to the given filename or URL.

Parameters:
saveat - A file or URL to save the configuration at.
Throws:
java.net.MalformedURLException
java.io.IOException

destroy

public void destroy()
Destroy a stopped server. Remove all components and send notifications to all event listeners. The HttpServer must be stopped before it can be destroyed.

Overrides:
destroy in class Container

main

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

Parameters:
args -


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