sunlabs.brazil.server

Class Server

public class Server extends Thread

Yet 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 a {@link Handler} to service http requests. If no handler is supplied, then the {@link FileHandler} is used. A {@link ChainHandler} is provided to allow multiple handlers in one server.

Limitations:

Version: 2.4

Author: Stephen Uhler (stephen.uhler@sun.com) Colin Stevens (colin.stevens@sun.com)

Field Summary
intacceptCount
Count of accepted connections so far.
intbufsize
Default buffer size for copies to and from client sockets. (default is 8192)
interrorCount
Count of errors that occurred so far.
Handlerhandler
StringhostName
The hostname that this Server should use to identify itself in an HTTP Redirect.
booleaninitFailure
If set, the server will terminate with an initialization failure just before creating the listen socket.
ServerSocketlisten
The listening socket.
intlogLevel
The diagnostic level.
static intLOG_DIAGNOSTIC
static intLOG_ERROR
static intLOG_INFORMATIONAL
static intLOG_LOG
static intLOG_WARNING
intmaxPost
Maximum amout of POST data allowed per request (in bytes) (default = 2Meg).
intmaxRequests
Maximum number of consecutive requests allowed on a single kept-alive socket.
intmaxThreads
The max number of threads allowed for the entire VM (default is 250).
Stringname
The string to return as the value for the "Server:" line in the HTTP response header.
Stringprefix
The handler is passed a prefix to identify which items in the properties object are relevent.
Propertiesprops
Hashtable containing arbitrary information that may be of interest to a Handler.
Stringprotocol
The protocol used to access this resource.
intrequestCount
Count of HTTP requests received so far.
InetAddress[]restrict
If non-null, restrict connections to just the specified ip addresses.
inttimeout
Time in milliseconds before this Server closes an idle socket or in-progress request.
Constructor Summary
Server(ServerSocket listen, String handlerName, Properties props)
Create a server using the provided listener socket.
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 Summary
voidclose()
Stop the server, and kill all pending requests
booleaninit()
voidlog(int level, Object obj, String message)
Logs information about the socket to System.out.
booleanrestart(String newHandler)
Restart the server with a new handler.
voidrun()
Loops, accepting socket connections and replying to HTTP requests.
booleansetup(ServerSocket listen, String handlerName, Properties props)

Field Detail

acceptCount

public int acceptCount
Count of accepted connections so far.

bufsize

public int bufsize
Default buffer size for copies to and from client sockets. (default is 8192)

errorCount

public int errorCount
Count of errors that occurred so far.

handler

public Handler handler

hostName

public String hostName
The hostname that this Server should use to identify itself in an HTTP Redirect. If null, the hostname is derived by calling InetAddress.getHostAddress.

InetAddress.getHostName would generally be the wrong thing to return because it returns only the base machine name xxx and not the machine name as it needs to appear to the rest of the network, such as xxx.yyy.com.

The default value is null.

initFailure

public boolean initFailure
If set, the server will terminate with an initialization failure just before creating the listen socket.

listen

public ServerSocket listen
The listening socket. Every time a new socket is accepted, a new thread is created to read the HTTP requests from it.

logLevel

public int logLevel
The diagnostic level. 0->least, 5->most

LOG_DIAGNOSTIC

public static final int LOG_DIAGNOSTIC

LOG_ERROR

public static final int LOG_ERROR

LOG_INFORMATIONAL

public static final int LOG_INFORMATIONAL

LOG_LOG

public static final int LOG_LOG

LOG_WARNING

public static final int LOG_WARNING

maxPost

public int maxPost
Maximum amout of POST data allowed per request (in bytes) (default = 2Meg).

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

name

public String name
The string to return as the value for the "Server:" line in the HTTP response header. If null, then no "Server:" line is returned.

prefix

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

props

public Properties props
Hashtable containing arbitrary information that may be of interest to a Handler. This table is available to both methods of the {@link Handler} interface, as {@link Server#props} in the {@link Handler#init(Server, String)} method, and as the default properties of {@link Request#props} in the {@link Handler#respond(Request)} method.

protocol

public String protocol
The protocol used to access this resource. Normally http, but can be changed for ssl to https

requestCount

public int requestCount
Count of HTTP requests received so far.

restrict

public InetAddress[] restrict
If non-null, restrict connections to just the specified ip addresses.

The default value is null.

timeout

public int timeout
Time in milliseconds before this Server closes an idle socket or in-progress request.

The default value is 30000.

Constructor Detail

Server

public Server(ServerSocket listen, String handlerName, Properties props)
Create a server using the provided listener socket.

This server will call the Handler.respond method 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), where port is the network port to listen on. Alternate implementations of ServerSocket, such as ssl versions may be used instead. handlerName The name of the handler used to process http requests. It must implement the {@link Handler} interface. props Arbitrary information made available to the handler. May be null.

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

close

public void close()
Stop the server, and kill all pending requests

init

public boolean init()

log

public void log(int level, Object obj, String message)
Logs information about the socket to System.out.

Parameters: level Controls the verbosity (0=least 5=most) obj The object that the message relates to. message The message to be logged.

restart

public boolean restart(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 Handler is not initialized until now, because its Handler.init method may have wanted to examine server member variables such as hostName or bufsize.

setup

public boolean setup(ServerSocket listen, String handlerName, Properties props)