Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.mortbay.http
Class HttpServer  view HttpServer download HttpServer.java

java.lang.Object
  extended byorg.mortbay.http.HttpServer
All Implemented Interfaces:
org.mortbay.util.LifeCycle, java.io.Serializable

public class HttpServer
extends java.lang.Object
implements org.mortbay.util.LifeCycle, 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.54 2003/11/22 16:06:02 gregwilkins Exp $

Nested Class Summary
 class HttpServer.ComponentEvent
           
static interface HttpServer.ComponentEventListener
           
 
Field Summary
private static java.lang.String[] __noVirtualHost
           
private static java.util.Collection __roServers
           
private static java.util.WeakHashMap __servers
           
private  java.util.List _components
           
private  int _connections
           
private  long _connectionsDurationAve
           
private  long _connectionsDurationMax
           
private  int _connectionsOpen
           
private  int _connectionsOpenMax
           
private  int _connectionsRequestsAve
           
private  int _connectionsRequestsMax
           
private  int _errors
           
private  java.util.List _eventListeners
           
private  int _gcRequests
           
private  java.util.List _listeners
           
private  HttpContext _notFoundContext
           
private  java.util.HashMap _realmMap
           
private  RequestLog _requestLog
           
private  int _requests
           
private  int _requestsActive
           
private  int _requestsActiveMax
           
private  long _requestsDurationAve
           
private  long _requestsDurationMax
           
private  int _requestsPerGC
           
private  boolean _resolveRemoteHost
           
private  java.lang.Object _statsLock
           
private  boolean _statsOn
           
private  long _statsStartedAt
           
private  boolean _trace
           
private  org.mortbay.util.StringMap _virtualHostMap
           
private static org.apache.commons.logging.Log log
           
private static org.mortbay.util.LogSupport logx
           
 
Constructor Summary
HttpServer()
          Constructor.
HttpServer(boolean anonymous)
          Constructor.
 
Method Summary
private  void addComponent(java.lang.Object o)
           
 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 addEventListener(java.util.EventListener listener)
          Add a server event listener.
 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(org.mortbay.util.InetAddrPort address)
          Create and add a SocketListener.
 HttpListener addListener(java.lang.String address)
          Create and add a SocketListener.
(package private)  void addMapping(java.lang.String virtualHost, HttpContext context)
           
(package private)  void addMappings(HttpContext context)
           
 UserRealm addRealm(UserRealm realm)
           
 void destroy()
          Destroy a stopped server.
 HttpHandler findHandler(java.lang.Class handlerClass, java.lang.String uri, java.lang.String[] vhosts)
          Find handler.
 int getConnections()
           
 long getConnectionsDurationAve()
           
 long getConnectionsDurationMax()
           
 int getConnectionsOpen()
           
 int getConnectionsOpenMax()
           
 int getConnectionsRequestsAve()
           
 int getConnectionsRequestsMax()
           
 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()
           
 long getRequestsDurationAve()
           
 long getRequestsDurationMax()
           
 int getRequestsPerGC()
          Get the requests per GC.
 boolean getResolveRemoteHost()
           
 boolean getStatsOn()
           
 long getStatsOnMs()
           
 boolean getTrace()
           
 boolean isStarted()
           
 void join()
          Join the listeners.
(package private)  void log(HttpRequest request, HttpResponse response, int length)
          Log a request to the request log
static void main(java.lang.String[] args)
          Construct server from command line arguments.
protected  HttpContext newHttpContext()
          Create a new HttpContext.
private  void readObject(java.io.ObjectInputStream in)
           
private  void removeComponent(java.lang.Object o)
           
 boolean removeContext(HttpContext context)
          Remove a context or Web application.
 void removeEventListener(java.util.EventListener listener)
           
 void removeListener(HttpListener listener)
          Remove a HTTP Listener.
(package private)  boolean removeMapping(java.lang.String virtualHost, HttpContext context)
           
(package private)  boolean removeMappings(HttpContext context)
           
 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 setStatsOn(boolean on)
           
 void setTrace(boolean trace)
           
 void start()
          Start all handlers then listeners.
(package private)  void statsCloseConnection(long duration, int requests)
           
(package private)  void statsEndRequest(long duration, boolean ok)
           
(package private)  void statsGotRequest()
           
(package private)  void statsOpenConnection()
           
 void statsReset()
          Reset statistics.
 void stop()
          Stop all listeners then all contexts.
 void stop(boolean graceful)
          Stop all listeners then all contexts.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static org.apache.commons.logging.Log log

logx

private static org.mortbay.util.LogSupport logx

__servers

private static java.util.WeakHashMap __servers

__roServers

private static java.util.Collection __roServers

__noVirtualHost

private static java.lang.String[] __noVirtualHost

_listeners

private java.util.List _listeners

_realmMap

private java.util.HashMap _realmMap

_virtualHostMap

private org.mortbay.util.StringMap _virtualHostMap

_trace

private boolean _trace

_requestLog

private RequestLog _requestLog

_requestsPerGC

private int _requestsPerGC

_resolveRemoteHost

private boolean _resolveRemoteHost

_gcRequests

private transient int _gcRequests

_notFoundContext

private transient HttpContext _notFoundContext

_eventListeners

private transient java.util.List _eventListeners

_components

private transient java.util.List _components

_statsOn

private boolean _statsOn

_statsLock

private transient java.lang.Object _statsLock

_statsStartedAt

private transient long _statsStartedAt

_connections

private transient int _connections

_connectionsOpen

private transient int _connectionsOpen

_connectionsOpenMax

private transient int _connectionsOpenMax

_connectionsDurationAve

private transient long _connectionsDurationAve

_connectionsDurationMax

private transient long _connectionsDurationMax

_connectionsRequestsAve

private transient int _connectionsRequestsAve

_connectionsRequestsMax

private transient int _connectionsRequestsMax

_errors

private transient int _errors

_requests

private transient int _requests

_requestsActive

private transient int _requestsActive

_requestsActiveMax

private transient int _requestsActiveMax

_requestsDurationAve

private transient long _requestsDurationAve

_requestsDurationMax

private transient long _requestsDurationMax
Constructor Detail

HttpServer

public HttpServer()
Constructor.


HttpServer

public HttpServer(boolean anonymous)
Constructor.

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.


getHttpServerList

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


readObject

private void readObject(java.io.ObjectInputStream in)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException

setAnonymous

public void setAnonymous(boolean anonymous)

setListeners

public void setListeners(HttpListener[] listeners)

getListeners

public HttpListener[] getListeners()

addListener

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


addListener

public HttpListener addListener(org.mortbay.util.InetAddrPort address)
                         throws java.io.IOException
Create and add a SocketListener. Conveniance method.


addListener

public HttpListener addListener(HttpListener listener)
                         throws java.lang.IllegalArgumentException
Add a HTTP Listener to the server.


removeListener

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


setContexts

public void setContexts(HttpContext[] contexts)

getContexts

public HttpContext[] getContexts()

addContext

public HttpContext addContext(HttpContext context)
Add a context.


removeContext

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


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.


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.


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.


getContext

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


getContext

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


getContext

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


newHttpContext

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


addMapping

void addMapping(java.lang.String virtualHost,
                HttpContext context)

addMappings

void addMappings(HttpContext context)

removeMapping

boolean removeMapping(java.lang.String virtualHost,
                      HttpContext context)

removeMappings

boolean removeMappings(HttpContext context)

getTrace

public boolean getTrace()

setTrace

public void setTrace(boolean trace)

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.


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.


start

public void start()
           throws org.mortbay.util.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 org.mortbay.util.LifeCycle

isStarted

public boolean isStarted()
Specified by:
isStarted in interface org.mortbay.util.LifeCycle

stop

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

Specified by:
stop in interface org.mortbay.util.LifeCycle

stop

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


join

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


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.


setRequestLog

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


getRequestLog

public RequestLog getRequestLog()

log

void log(HttpRequest request,
         HttpResponse response,
         int length)
Log a request to the request log


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.


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.


addRealm

public UserRealm addRealm(UserRealm realm)

getRealm

public UserRealm getRealm(java.lang.String realmName)
Get a named UserRealm.


removeRealm

public UserRealm removeRealm(java.lang.String realmName)

getHostMap

public java.util.Map getHostMap()

getResolveRemoteHost

public boolean getResolveRemoteHost()

setResolveRemoteHost

public void setResolveRemoteHost(boolean resolveRemoteHost)

statsReset

public void statsReset()
Reset statistics.


setStatsOn

public void setStatsOn(boolean on)

getStatsOn

public boolean getStatsOn()

getStatsOnMs

public long getStatsOnMs()

getConnections

public int getConnections()

getConnectionsOpen

public int getConnectionsOpen()

getConnectionsOpenMax

public int getConnectionsOpenMax()

getConnectionsDurationAve

public long getConnectionsDurationAve()

getConnectionsDurationMax

public long getConnectionsDurationMax()

getConnectionsRequestsAve

public int getConnectionsRequestsAve()

getConnectionsRequestsMax

public int getConnectionsRequestsMax()

getErrors

public int getErrors()

getRequests

public int getRequests()

getRequestsActive

public int getRequestsActive()

getRequestsActiveMax

public int getRequestsActiveMax()

getRequestsDurationAve

public long getRequestsDurationAve()

getRequestsDurationMax

public long getRequestsDurationMax()

statsOpenConnection

void statsOpenConnection()

statsGotRequest

void statsGotRequest()

statsEndRequest

void statsEndRequest(long duration,
                     boolean ok)

statsCloseConnection

void statsCloseConnection(long duration,
                          int requests)

addComponent

private void addComponent(java.lang.Object o)

removeComponent

private void removeComponent(java.lang.Object o)

addEventListener

public void addEventListener(java.util.EventListener listener)
Add a server event listener. Listeners are sent HttpServer.ComponentEvent instances when components such as listeners and contexts are added to the HttpServer.


removeEventListener

public void removeEventListener(java.util.EventListener listener)

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.


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.


main

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