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.
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.
- 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.
virtualHost
- Virtual hostname or null for all hosts.contextPathSpec
- Path specification relative to the context path.
- 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.
virtualHost
- The virtual host or null for all hosts.context
-
addHostAlias
public void addHostAlias(String virtualHost,
String alias)
Use HttpContext.addVirtualHost
Define a virtual host alias.
All requests to the alias are handled the same as request for
the virtualHost.
virtualHost
- Host name or IPalias
- Alias hostname or IP
addListener
public HttpListener addListener(String address)
throws IOException
Create and add a SocketListener.
Conveniance method.
addListener
public HttpListener addListener(HttpListener listener)
throws IllegalArgumentException
Add a HTTP Listener to the server.
addListener
public HttpListener addListener(InetAddrPort address)
throws IOException
Create and add a SocketListener.
Conveniance method.
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.
- 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.
- doStart in interface Container
doStop
protected void doStop()
throws InterruptedException
Stop all listeners then all contexts.
Equivalent to stop(false);
- 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.
uri
- URI that must be satisfied by the servlet handlervhosts
- null or a list of virtual hosts names to search
getConnections
public int getConnections()
- Number of connections accepted by the server since
statsReset() called. Undefined if setStatsOn(false).
getConnectionsDurationAve
public long getConnectionsDurationAve()
- Average duration in milliseconds of open connections
since statsReset() called. Undefined if setStatsOn(false).
getConnectionsDurationMax
public long getConnectionsDurationMax()
- Maximum duration in milliseconds of an open connection
since statsReset() called. Undefined if setStatsOn(false).
getConnectionsDurationMin
public long getConnectionsDurationMin()
- Returns the connectionsDurationMin.
getConnectionsDurationTotal
public long getConnectionsDurationTotal()
- Returns the connectionsDurationTotal.
getConnectionsOpen
public int getConnectionsOpen()
- Number of connections currently open that were opened
since statsReset() called. Undefined if setStatsOn(false).
getConnectionsOpenMax
public int getConnectionsOpenMax()
- Maximum number of connections opened simultaneously
since statsReset() called. Undefined if setStatsOn(false).
getConnectionsOpenMin
public int getConnectionsOpenMin()
- Returns the connectionsOpenMin.
getConnectionsRequestsAve
public int getConnectionsRequestsAve()
- Average number of requests per connection
since statsReset() called. Undefined if setStatsOn(false).
getConnectionsRequestsMax
public int getConnectionsRequestsMax()
- Maximum number of requests per connection
since statsReset() called. Undefined if setStatsOn(false).
getConnectionsRequestsMin
public int getConnectionsRequestsMin()
- Returns the connectionsRequestsMin.
getContext
public HttpContext getContext(String contextPathSpec)
Get or create context.
contextPathSpec
- Path specification relative to the context path.
- 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.
virtualHost
- The virtual host or null for all hosts.contextPathSpec
-
- 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.
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.
getErrors
public int getErrors()
- Number of errors generated while handling requests.
since statsReset() called. Undefined if setStatsOn(false).
getHostMap
public Map getHostMap()
getHttpServerList
public static List getHttpServerList()
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.
- Collection of all servers.
getRealm
public UserRealm getRealm(String realmName)
Get a named UserRealm.
realmName
- The name of the realm or null.
- The named realm. If the name is null and only a single realm
is known, that is returned.
getRequests
public int getRequests()
- Number of requests
since statsReset() called. Undefined if setStatsOn(false).
getRequestsActive
public int getRequestsActive()
- Number of requests currently active.
Undefined if setStatsOn(false).
getRequestsActiveMax
public int getRequestsActiveMax()
- Maximum number of active requests
since statsReset() called. Undefined if setStatsOn(false).
getRequestsActiveMin
public int getRequestsActiveMin()
- Returns the requestsActiveMin.
getRequestsDurationAve
public long getRequestsDurationAve()
- Average duration of request handling in milliseconds
since statsReset() called. Undefined if setStatsOn(false).
getRequestsDurationMax
public long getRequestsDurationMax()
- Get maximum duration in milliseconds of request handling
since statsReset() called. Undefined if setStatsOn(false).
getRequestsDurationMin
public long getRequestsDurationMin()
- Returns the requestsDurationMin.
getRequestsDurationTotal
public long getRequestsDurationTotal()
- 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.
- Approx requests per garbage collection.
getResolveRemoteHost
public boolean getResolveRemoteHost()
- True if the remote host name of connections is resolved.
getServerClasses
public String[] getServerClasses()
Get system classes.
getStatsOn
public boolean getStatsOn()
- True if statistics collection is turned on.
getStatsOnMs
public long getStatsOnMs()
- Timestamp stats were started at.
getStopGracefully
public boolean getStopGracefully()
getSystemClasses
public String[] getSystemClasses()
Get system classes.
getTrace
public boolean getTrace()
- 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.
newHttpContext
protected HttpContext newHttpContext()
Create a new HttpContext.
Specialized HttpServer classes may override this method to
return subclasses of HttpContext.
- 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.
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.
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.
- The HttpContext that completed handling of the request or null.
setAnonymous
public void setAnonymous(boolean anonymous)
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)
listeners
- Array of HttpListeners.
setRequestLog
public void setRequestLog(RequestLog log)
Set the request log.
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.
requestsPerGC
- Approx requests per garbage collection.
setResolveRemoteHost
public void setResolveRemoteHost(boolean resolveRemoteHost)
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)
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.
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.