org.mortbay.http

Class HttpServer

Implemented Interfaces:
EventProvider, LifeCycle, Serializable
Known Direct Subclasses:
Server

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

HttpContext
addContext(String contextPath)
Create and add a new context.
HttpContext
addContext(String virtualHost, String contextPathSpec)
Create and add a new context.
HttpContext
addContext(String virtualHost, HttpContext context)
Add a context.
HttpContext
addContext(HttpContext context)
Add a context.
void
addHostAlias(String virtualHost, String alias)
Deprecated. Use HttpContext.addVirtualHost
HttpListener
addListener(String address)
Create and add a SocketListener.
HttpListener
addListener(HttpListener listener)
Add a HTTP Listener to the server.
HttpListener
addListener(InetAddrPort 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(Class handlerClass, String uri, 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(String contextPathSpec)
Get or create context.
HttpContext
getContext(String virtualHost, String contextPathSpec)
Get or create context.
HttpContext
getContext(String virtualHost, String contextPathSpec, int i)
Get specific context.
HttpContext[]
getContexts()
int
getErrors()
Map
getHostMap()
static List
getHttpServerList()
Deprecated. User getHttpServers()
static Collection
getHttpServers()
Get HttpServer Collection.
HttpListener[]
getListeners()
UserRealm
getRealm(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()
String[]
getServerClasses()
Get system classes.
boolean
getStatsOn()
long
getStatsOnMs()
boolean
getStopGracefully()
String[]
getSystemClasses()
Get system classes.
boolean
getTrace()
void
join()
Join the listeners.
static void
main(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(String realmName)
void
save(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(String[] classes)
Set system classes.
void
setStatsOn(boolean on)
void
setStopGracefully(boolean graceful)
void
setSystemClasses(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, destroy, doStart, doStop, getComponents, isStarted, isStarting, isStopping, removeComponent, removeEventListener, start, stop

Constructor Details

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 Details

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.

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(HttpContext context)
Add a context.
Parameters:
context -

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)
            throws IOException
Create and add a SocketListener. Conveniance method.
Parameters:
address -
Returns:
the HttpListener.

addListener

public HttpListener addListener(HttpListener listener)
            throws IllegalArgumentException
Add a HTTP Listener to the server.
Parameters:
listener - The Listener.

addListener

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

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.
Overrides:
destroy in interface Container

doStart

protected void doStart()
            throws 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.
Overrides:
doStart in interface Container

doStop

protected void doStop()
            throws InterruptedException
Stop all listeners then all contexts. Equivalent to stop(false);
Overrides:
doStop in interface Container

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

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

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.

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.

getTrace

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

join

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

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)
            throws IllegalStateException
Remove a context or Web application.

removeListener

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

removeRealm

public UserRealm removeRealm(String realmName)

save

public void save(String saveat)
            throws MalformedURLException,
                   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.

service

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

setStatsOn

public void setStatsOn(boolean on)

setStopGracefully

public void setStopGracefully(boolean graceful)

setSystemClasses

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

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

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