Class Server
- java.lang.Object
-
- java.lang.Thread
-
- sunlabs.brazil.server.Server
-
- All Implemented Interfaces:
java.lang.Runnable
public class Server extends java.lang.ThreadYet another HTTP/1.1 server. This class is the core of a light weight Web Server. This server is started as a Thread listening on the supplied port, and dispatches to an implementation of aHandlerto service http requests. If no handler is supplied, then theFileHandleris used. AChainHandleris provided to allow multiple handlers in one server.Limitations:
- Starts a new thread for each connection. This may be expensive.
- Version:
- 2.4
- Author:
- Stephen Uhler (stephen.uhler@sun.com), Colin Stevens (colin.stevens@sun.com)
-
-
Field Summary
Fields Modifier and Type Field Description intacceptCountCount of accepted connections so far.intbufsizeDefault buffer size for copies to and from client sockets.interrorCountCount of errors that occurred so far.Handlerhandlerjava.lang.StringhostNameThe hostname that this Server should use to identify itself in an HTTP Redirect.booleaninitFailureIf set, the server will terminate with an initialization failure just before creating the listen socket.java.net.ServerSocketlistenThe listening socket.static intLOG_DIAGNOSTICstatic intLOG_ERRORstatic intLOG_INFORMATIONALstatic intLOG_LOGstatic intLOG_WARNINGintlogLevelThe diagnostic level.intmaxPostMaximum amout of POST data allowed per request (in bytes) (default = 2Meg).intmaxRequestsMaximum number of consecutive requests allowed on a single kept-alive socket.intmaxThreadsThe max number of threads allowed for the entire VM (default is 250).java.lang.StringnameThe string to return as the value for the "Server:" line in the HTTP response header.java.lang.StringprefixThe handler is passed a prefix to identify which items in the properties object are relevent.java.util.PropertiespropsHashtable containing arbitrary information that may be of interest to a Handler.java.lang.StringprotocolThe protocol used to access this resource.intrequestCountCount of HTTP requests received so far.java.net.InetAddress[]restrictIf non-null, restrict connections to just the specified ip addresses.inttimeoutTime in milliseconds before this Server closes an idle socket or in-progress request.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Stop the server, and kill all pending requestsbooleaninit()voidlog(int level, java.lang.Object obj, java.lang.String message)Logs information about the socket toSystem.out.booleanrestart(java.lang.String newHandler)Restart the server with a new handler.voidrun()Loops, accepting socket connections and replying to HTTP requests.booleansetup(java.net.ServerSocket listen, java.lang.String handlerName, java.util.Properties props)-
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
-
-
-
Field Detail
-
listen
public java.net.ServerSocket listen
The listening socket. Every time a new socket is accepted, a new thread is created to read the HTTP requests from it.
-
handler
public Handler handler
-
props
public java.util.Properties props
Hashtable containing arbitrary information that may be of interest to a Handler. This table is available to both methods of theHandlerinterface, aspropsin theHandler.init(Server, String)method, and as the default properties ofRequest.propsin theHandler.respond(Request)method.
-
hostName
public java.lang.String hostName
The hostname that this Server should use to identify itself in an HTTP Redirect. Ifnull, the hostname is derived by callingInetAddress.getHostAddress.InetAddress.getHostNamewould generally be the wrong thing to return because it returns only the base machine namexxxand not the machine name as it needs to appear to the rest of the network, such asxxx.yyy.com.The default value is
null.
-
protocol
public java.lang.String protocol
The protocol used to access this resource. Normallyhttp, but can be changed forssltohttps
-
restrict
public java.net.InetAddress[] restrict
If non-null, restrict connections to just the specified ip addresses.The default value is
null.
-
name
public java.lang.String name
The string to return as the value for the "Server:" line in the HTTP response header. Ifnull, then no "Server:" line is returned.
-
prefix
public java.lang.String prefix
The handler is passed a prefix to identify which items in the properties object are relevent. By convention, non-empty strings end with ".", allowing nested prefixes to be easily distinguished.
-
timeout
public int timeout
Time in milliseconds before this Server closes an idle socket or in-progress request.The default value is
30000.
-
maxRequests
public int maxRequests
Maximum number of consecutive requests allowed on a single kept-alive socket.The default value is
25.
-
maxThreads
public int maxThreads
The max number of threads allowed for the entire VM (default is 250).
-
maxPost
public int maxPost
Maximum amout of POST data allowed per request (in bytes) (default = 2Meg).
-
bufsize
public int bufsize
Default buffer size for copies to and from client sockets. (default is 8192)
-
acceptCount
public int acceptCount
Count of accepted connections so far.
-
requestCount
public int requestCount
Count of HTTP requests received so far.
-
errorCount
public int errorCount
Count of errors that occurred so far.
-
logLevel
public int logLevel
The diagnostic level. 0->least, 5->most
-
initFailure
public boolean initFailure
If set, the server will terminate with an initialization failure just before creating the listen socket.
-
LOG_ERROR
public static final int LOG_ERROR
- See Also:
- Constant Field Values
-
LOG_WARNING
public static final int LOG_WARNING
- See Also:
- Constant Field Values
-
LOG_LOG
public static final int LOG_LOG
- See Also:
- Constant Field Values
-
LOG_INFORMATIONAL
public static final int LOG_INFORMATIONAL
- See Also:
- Constant Field Values
-
LOG_DIAGNOSTIC
public static final int LOG_DIAGNOSTIC
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Server
public Server(java.net.ServerSocket listen, java.lang.String handlerName, java.util.Properties props)Create a server using the provided listener socket.This server will call the
Handler.respondmethod of the specified handler. The specified handler should either respond to the request or perform further dispatches to other handlers.- Parameters:
listen- The socket this server should listen to. For ordinary sockets, this is simply:new ServerSocket(port), whereportis the network port to listen on. Alternate implementations ofServerSocket, such as ssl versions may be used instead.handlerName- The name of the handler used to process http requests. It must implement theHandlerinterface.props- Arbitrary information made available to the handler. May benull.- See Also:
FileHandler,ChainHandler
-
Server
public Server()
Set up the server. this allows a server to be created with newInstance() followed by setup(), instead of using the above initializer, making it easier to start sub-classes of the server.
-
-
Method Detail
-
setup
public boolean setup(java.net.ServerSocket listen, java.lang.String handlerName, java.util.Properties props)
-
init
public boolean init()
-
restart
public boolean restart(java.lang.String newHandler)
Restart the server with a new handler.- Parameters:
newHandler- Name of the handler to restart the server with
-
run
public void run()
Loops, accepting socket connections and replying to HTTP requests. This is called indirectly via Thread.start().Many things in the server are not initialized until this point, because the user may have set some related configuration options between the time this server was allocated and the time it was started. For instance, the main
Handleris not initialized until now, because itsHandler.initmethod may have wanted to examine server member variables such ashostNameorbufsize.- Specified by:
runin interfacejava.lang.Runnable- Overrides:
runin classjava.lang.Thread
-
close
public void close()
Stop the server, and kill all pending requests
-
log
public void log(int level, java.lang.Object obj, java.lang.String message)Logs information about the socket toSystem.out.- Parameters:
level- Controls the verbosity (0=least 5=most)obj- The object that the message relates to.message- The message to be logged.
-
-