org.mortbay.http

Class HttpServer

public class HttpServer extends Container implements LifeCycle, EventProvider, 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

Constructor Summary
HttpServer()
Constructor.
HttpServer(boolean anonymous)
Constructor.
Method Summary
HttpContextaddContext(HttpContext context)
Add a context.
HttpContextaddContext(String virtualHost, HttpContext context)
Add a context.
HttpContextaddContext(String contextPath)
Create and add a new context.
HttpContextaddContext(String virtualHost, String contextPathSpec)
Create and add a new context.
voidaddHostAlias(String virtualHost, String alias)
Define a virtual host alias.
HttpListeneraddListener(String address)
Create and add a SocketListener.
HttpListeneraddListener(InetAddrPort address)
Create and add a SocketListener.
HttpListeneraddListener(HttpListener listener)
Add a HTTP Listener to the server.
UserRealmaddRealm(UserRealm realm)
voiddestroy()
Destroy a stopped server.
protected voiddoStart()
Start all handlers then listeners.
protected voiddoStop()
Stop all listeners then all contexts.
HttpHandlerfindHandler(Class handlerClass, String uri, String[] vhosts)
Find handler.
intgetConnections()
longgetConnectionsDurationAve()
longgetConnectionsDurationMax()
longgetConnectionsDurationMin()
longgetConnectionsDurationTotal()
intgetConnectionsOpen()
intgetConnectionsOpenMax()
intgetConnectionsOpenMin()
intgetConnectionsRequestsAve()
intgetConnectionsRequestsMax()
intgetConnectionsRequestsMin()
HttpContextgetContext(String virtualHost, String contextPathSpec, int i)
Get specific context.
HttpContextgetContext(String virtualHost, String contextPathSpec)
Get or create context.
HttpContextgetContext(String contextPathSpec)
Get or create context.
HttpContext[]getContexts()
intgetErrors()
MapgetHostMap()
static ListgetHttpServerList()
static CollectiongetHttpServers()
Get HttpServer Collection.
HttpListener[]getListeners()
UserRealmgetRealm(String realmName)
Get a named UserRealm.
RequestLoggetRequestLog()
intgetRequests()
intgetRequestsActive()
intgetRequestsActiveMax()
intgetRequestsActiveMin()
longgetRequestsDurationAve()
longgetRequestsDurationMax()
longgetRequestsDurationMin()
longgetRequestsDurationTotal()
intgetRequestsPerGC()
Get the requests per GC.
booleangetResolveRemoteHost()
String[]getServerClasses()
Get system classes.
booleangetStatsOn()
longgetStatsOnMs()
booleangetStopGracefully()
String[]getSystemClasses()
Get system classes.
booleangetTrace()
voidjoin()
Join the listeners.
static voidmain(String[] args)
Construct server from command line arguments.
protected HttpContextnewHttpContext()
Create a new HttpContext.
booleanremoveContext(HttpContext context)
Remove a context or Web application.
voidremoveListener(HttpListener listener)
Remove a HTTP Listener.
UserRealmremoveRealm(String realmName)
voidsave(String saveat)
Save the HttpServer The server is saved by serialization to the given filename or URL.
HttpContextservice(HttpRequest request, HttpResponse response)
Service a request.
voidsetAnonymous(boolean anonymous)
voidsetContexts(HttpContext[] contexts)
voidsetListeners(HttpListener[] listeners)
voidsetRequestLog(RequestLog log)
Set the request log.
voidsetRequestsPerGC(int requestsPerGC)
Set the requests per GC.
voidsetResolveRemoteHost(boolean resolveRemoteHost)
voidsetServerClasses(String[] classes)
Set system classes.
voidsetStatsOn(boolean on)
voidsetStopGracefully(boolean graceful)
voidsetSystemClasses(String[] classes)
Set system classes.
voidsetTrace(boolean trace)
voidstatsReset()
Reset statistics.
voidstop(boolean graceful)
Stop all listeners then all contexts.

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

addContext

public HttpContext addContext(HttpContext context)
Add a context.

Parameters: context

addContext

public HttpContext addContext(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(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(String virtualHost, 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.

addHostAlias

public void addHostAlias(String virtualHost, 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

addListener

public HttpListener addListener(String address)
Create and add a SocketListener. Conveniance method.

Parameters: address

Returns: the HttpListener.

Throws: IOException

addListener

public HttpListener addListener(InetAddrPort address)
Create and add a SocketListener. Conveniance method.

Parameters: address

Returns: the HttpListener.

Throws: IOException

addListener

public HttpListener addListener(HttpListener listener)
Add a HTTP Listener to the server.

Parameters: listener The Listener.

Throws: IllegalArgumentException If the listener is not for this server.

addRealm

public UserRealm addRealm(UserRealm realm)

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.

doStart

protected void doStart()
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.

Throws: MultiException A collection of exceptions thrown by start() method of subcomponents of the HttpServer.

doStop

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

Throws: InterruptedException If interrupted, stop may not have been called on everything.

findHandler

public HttpHandler findHandler(Class handlerClass, String uri, 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

getConnections

public int getConnections()

Returns: Number of connections accepted by the server 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).

getConnectionsDurationMin

public long getConnectionsDurationMin()

Returns: Returns the connectionsDurationMin.

getConnectionsDurationTotal

public long getConnectionsDurationTotal()

Returns: Returns the connectionsDurationTotal.

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

getConnectionsOpenMin

public int getConnectionsOpenMin()

Returns: Returns the connectionsOpenMin.

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

getConnectionsRequestsMin

public int getConnectionsRequestsMin()

Returns: Returns the connectionsRequestsMin.

getContext

public HttpContext getContext(String virtualHost, 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(String virtualHost, 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(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.

getContexts

public HttpContext[] getContexts()

getErrors

public int getErrors()

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

getHostMap

public Map getHostMap()

getHttpServerList

public static List getHttpServerList()

Deprecated: User getHttpServers()

getHttpServers

public static 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.

getListeners

public HttpListener[] getListeners()

Returns: Array of HttpListeners.

getRealm

public UserRealm getRealm(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.

getRequestLog

public RequestLog getRequestLog()

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

getRequestsActiveMin

public int getRequestsActiveMin()

Returns: Returns the requestsActiveMin.

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

getRequestsDurationMin

public long getRequestsDurationMin()

Returns: Returns the requestsDurationMin.

getRequestsDurationTotal

public long getRequestsDurationTotal()

Returns: Returns the requestsDurationTotal.

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.

getResolveRemoteHost

public boolean getResolveRemoteHost()

Returns: True if the remote host name of connections is resolved.

getServerClasses

public String[] getServerClasses()
Get system classes.

UNKNOWN: Use HttpContext

getStatsOn

public boolean getStatsOn()

Returns: True if statistics collection is turned on.

getStatsOnMs

public long getStatsOnMs()

Returns: Timestamp stats were started at.

getStopGracefully

public boolean getStopGracefully()

getSystemClasses

public String[] getSystemClasses()
Get system classes.

UNKNOWN: Use HttpContext

getTrace

public boolean getTrace()

Returns: True if the TRACE method is fully implemented.

join

public void join()
Join the listeners. Join all listeners that are instances of ThreadPool.

Throws: InterruptedException

main

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

Parameters: args

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

removeContext

public boolean removeContext(HttpContext context)
Remove a context or Web application.

Throws: IllegalStateException if context not stopped

removeListener

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

Parameters: listener

removeRealm

public UserRealm removeRealm(String realmName)

save

public void save(String saveat)
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: MalformedURLException IOException

service

public HttpContext service(HttpRequest request, HttpResponse response)
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: IOException HttpException

setAnonymous

public void setAnonymous(boolean anonymous)

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

setContexts

public void setContexts(HttpContext[] contexts)

setListeners

public void setListeners(HttpListener[] listeners)

Parameters: listeners Array of HttpListeners.

setRequestLog

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

Parameters: log RequestLog to use.

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.

setResolveRemoteHost

public void setResolveRemoteHost(boolean resolveRemoteHost)

Parameters: resolveRemoteHost True if the remote host name of connections is resolved.

setServerClasses

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

UNKNOWN: Use HttpContext

setStatsOn

public void setStatsOn(boolean on)

setStopGracefully

public void setStopGracefully(boolean graceful)

setSystemClasses

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

UNKNOWN: Use HttpContext

setTrace

public void setTrace(boolean trace)

Parameters: trace True if the TRACE method is fully implemented.

statsReset

public void statsReset()
Reset statistics.

stop

public void stop(boolean graceful)
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.

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