|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object pygmy.core.Server
public class Server
Server is the core of the system. A server glues together Handler
s
and EndPoint
s. EndPoint
s are responsible for reading the
HttpRequest
from a source and sending the HttpResponse over that source.
EndPoint
s then sends the request to the Handler
by calling the post()
method on the server to send the request to this server's Handler
s. Handler
s
process the HttpRequest
and produce an appropriate HttpResponse
.
The server contains the configuration for the entire server. What are the expected values in the configuration is mainly controlled by what handlers and endpoints are configured. Depending on which handlers and endpoints have been enabled, the configuration will vary. The only two parameters are required: handler and <handler's name>.class. Here is an example configuration:
handler=my\ handler my\ handler.class=pygmy.handlers.DefaultChainHandler my\ handler.chain=handler1, handler2 my\ handler.url-prefix=/ handler1.class=pygmy.handlers.FileHandler handler1.root=C:\temp handler1.url-prefix=/home-directory handler2.class=pygmy.handlers.ResourceHandler handler2.url-prefix=/jar handler2.resourceMount=/html handler2.default=index.htmlIn the above configuration, handler property is the name of first handler. The name is used to find all the other properties for that particular handler. The .class property is used to tell the Server the name of the class to instantiate. The two other properties, .chain and .url-prefix, are particular to theDefaultChainHandler
.Server's only have one
Handler
. However, the power ofHandler
s is the ability to have more than one. TheDefaultChainHandler
provides the ability to create a chain of multiple handlers. SeeDefaultChainHandler
for information on configuring it.Server also contains a set of
EndPoint
s. When the server initializes itself it looks in the configuration for the endpoints parameter. The endpoints parameter contains a space seperated list of the names of the endpoints this server will create. For each name in the list it will look for a config parameter <name of endpoint>.class in the configuration. It will instantiate the classname using the no-argument constructor and add it to the set of endpoints in the server.If the server does not find the endpoints parameter, then it will create a default EndPoint of type
ServerSocketEndPoint
named http. Here is an example of using the endpoints parameter:endpoints=endpoint1 endpoint2 handler=handler1 endpoint1.class=my.package.MyEndPoint endpoint1.param1=foo endpoint1.param2=bar endpoint2.class=my.package.AnotherEndPoint endpoint2.param1=foo endpoint2.param2=bar endpoint2.param3=baz ...Server class looks for the following properties in the configuration:
Parameter Name | Default Value | Required |
handler | None | Yes |
endpoints | http | No |
<handler name>.class | None | Yes |
<endpoint name>.class | None | iff endpoints param is defined |
threadpool.size | 5 | No |
Constructor Summary | |
---|---|
Server(java.util.Properties config)
This creates a server using the given configuration. |
|
Server(java.lang.String filename)
This creates a server using the given filename as the configuration for this server. |
|
Server(java.lang.String[] args)
This creates a server from commandline arguments. |
Method Summary | |
---|---|
void |
addEndPoint(java.lang.String name,
EndPoint endpoint)
This method adds an EndPoint to this server. |
java.lang.Object |
constructPygmyObject(java.lang.String objectName)
This is the method used to construct pygmy objects. |
java.lang.Object |
get(java.lang.Object key)
This returns the object stored under the given key. |
java.util.Properties |
getConfig()
Returns the configuration for the server. |
java.lang.String |
getProperty(java.lang.String key)
Returns the property stored under the key. |
java.lang.String |
getProperty(java.lang.String key,
java.lang.String defaultValue)
Returns the property stored under the key. |
java.lang.Object |
getRegisteredComponent(java.lang.Class clazz)
|
ResponseListener |
getResponseListeners()
Returns the instance of the ResponseListener for this Server. |
boolean |
hasProperty(java.lang.String key)
Returns true iff the key is in the configuration |
protected void |
initializeHandler()
|
protected void |
loadConfiguration(java.lang.String config,
java.util.Properties props)
|
static void |
main(java.lang.String[] args)
|
boolean |
post(Request request,
Response response)
This method is used to post a HttpRequest to the server's handler. |
void |
post(java.lang.Runnable runnable)
This method posts a Runnable onto the Server's task queue. |
protected void |
processArguments(java.lang.String[] args,
java.util.Properties props)
|
void |
putProperty(java.lang.Object key,
java.lang.Object value)
This puts a configuration property into the server's configuration. |
void |
registerComponent(java.lang.Object object)
This is called when a program wants to register a shared component for Handlers to have access to. |
void |
run()
This is called when the server is shutdown thread is called. |
void |
setResponseListener(ResponseListener listener)
This sets the ResponseListener for entire server. |
void |
shutdown()
This method will shutdown the Handler, and call EndPoint.shutdown(pygmy.core.Server) on each EndPoint. |
void |
start()
This method is called to start the web server. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Server(java.lang.String filename) throws java.io.IOException
Properties
file.
filename
- the path to a file to use as the configuration of this server.
java.io.IOException
public Server(java.util.Properties config)
config
- the configuration to use for this server.public Server(java.lang.String[] args) throws java.io.IOException
args
- an array of config parameters, the format of the list is a '-' followed by either 'config' or some
name of a parameter (i.e. http.port), and a space and a value for that property. All -config will load a file
either from the filesystem or the class path if the file doesn't exist on the filesystem.
java.io.IOException
Method Detail |
---|
public void addEndPoint(java.lang.String name, EndPoint endpoint)
EndPoint
to this server. It will be initialized once the start()
method is called.
name
- The name of this EndPoint instance.endpoint
- The instance of the endpoint to add.public void putProperty(java.lang.Object key, java.lang.Object value)
key
- The unique key to store the value under.value
- The value of the key.public java.lang.String getProperty(java.lang.String key)
key
- the configuration key to look up.
public java.lang.String getProperty(java.lang.String key, java.lang.String defaultValue)
key
- the configuration key to look up.defaultValue
- the defaultValue returned if nothing is found under the key.
public boolean hasProperty(java.lang.String key)
key
- the name of the key.
public java.lang.Object get(java.lang.Object key)
key
- the key to look up the stored object.
public java.util.Properties getConfig()
public java.lang.Object getRegisteredComponent(java.lang.Class clazz)
public void registerComponent(java.lang.Object object)
object
- the object the user wants to make available to Handler instances.public void start()
EndPoint.start()
on each EndPoint. This method will return after the above steps are
done.
protected void initializeHandler()
public java.lang.Object constructPygmyObject(java.lang.String objectName)
objectName
- the name of the object defined in the server's configuration.
public void run()
run
in interface java.lang.Runnable
public void shutdown()
EndPoint.shutdown(pygmy.core.Server)
on each EndPoint.
public boolean post(Request request, Response response) throws java.io.IOException
HttpRequest
to the server's handler. It will create a HttpResponse
for the EndPoint to send to the client.
request
-
java.io.IOException
public void post(java.lang.Runnable runnable)
ThreadPool
will service the
runnable once a thread is freed up.
runnable
- An instance of Runnable that the user wishes to run on the server's ThreadPool
.public ResponseListener getResponseListeners()
public void setResponseListener(ResponseListener listener)
listener
- the instance of a ResponseListener to use for this Server.public static void main(java.lang.String[] args) throws java.io.IOException
java.io.IOException
protected void processArguments(java.lang.String[] args, java.util.Properties props) throws java.io.IOException
java.io.IOException
protected void loadConfiguration(java.lang.String config, java.util.Properties props) throws java.io.IOException
java.io.IOException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |