pygmy.core
Class Server

java.lang.Object
  extended by pygmy.core.Server
All Implemented Interfaces:
java.lang.Runnable

public class Server
extends java.lang.Object
implements java.lang.Runnable

Server is the core of the system. A server glues together Handlers and EndPoints. EndPoints are responsible for reading the HttpRequest from a source and sending the HttpResponse over that source. EndPoints then sends the request to the Handler by calling the post() method on the server to send the request to this server's Handlers. Handlers 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.html
In 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 the DefaultChainHandler.

Server's only have one Handler. However, the power of Handlers is the ability to have more than one. The DefaultChainHandler provides the ability to create a chain of multiple handlers. See DefaultChainHandler for information on configuring it.

Server also contains a set of EndPoints. 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 NameDefault ValueRequired
handlerNoneYes
endpointshttpNo
<handler name>.classNoneYes
<endpoint name>.classNoneiff endpoints param is defined
threadpool.size5No


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

Server

public Server(java.lang.String filename)
       throws java.io.IOException
This creates a server using the given filename as the configuration for this server. The configuration file should follow format of normal Properties file.

Parameters:
filename - the path to a file to use as the configuration of this server.
Throws:
java.io.IOException

Server

public Server(java.util.Properties config)
This creates a server using the given configuration.

Parameters:
config - the configuration to use for this server.

Server

public Server(java.lang.String[] args)
       throws java.io.IOException
This creates a server from commandline arguments.

Parameters:
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.
Throws:
java.io.IOException
Method Detail

addEndPoint

public void addEndPoint(java.lang.String name,
                        EndPoint endpoint)
This method adds an EndPoint to this server. It will be initialized once the start() method is called.

Parameters:
name - The name of this EndPoint instance.
endpoint - The instance of the endpoint to add.

putProperty

public void putProperty(java.lang.Object key,
                        java.lang.Object value)
This puts a configuration property into the server's configuration.

Parameters:
key - The unique key to store the value under.
value - The value of the key.

getProperty

public java.lang.String getProperty(java.lang.String key)
Returns the property stored under the key.

Parameters:
key - the configuration key to look up.
Returns:
the value stored in the configuration at this key.

getProperty

public java.lang.String getProperty(java.lang.String key,
                                    java.lang.String defaultValue)
Returns the property stored under the key. If there isn't a property called key, then it returns the defaultValue.

Parameters:
key - the configuration key to look up.
defaultValue - the defaultValue returned if nothing is found under the key.
Returns:
the value stored in the configuration at this key.

hasProperty

public boolean hasProperty(java.lang.String key)
Returns true iff the key is in the configuration

Parameters:
key - the name of the key.
Returns:
true if and only if the key is contained in the configuration. False otherwise.

get

public java.lang.Object get(java.lang.Object key)
This returns the object stored under the given key.

Parameters:
key - the key to look up the stored object.
Returns:
the object stored at the given key.

getConfig

public java.util.Properties getConfig()
Returns the configuration for the server.

Returns:
the configuration for the server.

getRegisteredComponent

public java.lang.Object getRegisteredComponent(java.lang.Class clazz)

registerComponent

public void registerComponent(java.lang.Object object)
This is called when a program wants to register a shared component for Handlers to have access to. The objects's class will be the defining key for identitying the object. Registering more than one instance will not be supported. If you're program must differ between two instances, then register a manager and allow the Handler's to interact with that to differentiate the individual instances.

Parameters:
object - the object the user wants to make available to Handler instances.

start

public void start()
This method is called to start the web server. It will initialize the server's Handler and all the EndPoints then call the EndPoint.start() on each EndPoint. This method will return after the above steps are done.


initializeHandler

protected void initializeHandler()

constructPygmyObject

public java.lang.Object constructPygmyObject(java.lang.String objectName)
This is the method used to construct pygmy objects. Given the object name it appends .class onto the end and looks for the classname in the server's configuration. It then analyzes the class's constructor parameters for objects that it depends on. Then it looks those objects up by class in the registered object pool. Finally, it calls the constructor using reflection passing any registered objects as arguments. It returns the newly constructed object or null if there was a problem.

Parameters:
objectName - the name of the object defined in the server's configuration.
Returns:
the newly constructed object, or null there was a problem instantiated the object.

run

public void run()
This is called when the server is shutdown thread is called.

Specified by:
run in interface java.lang.Runnable

shutdown

public void shutdown()
This method will shutdown the Handler, and call EndPoint.shutdown(pygmy.core.Server) on each EndPoint.


post

public boolean post(Request request,
                    Response response)
             throws java.io.IOException
This method is used to post a HttpRequest to the server's handler. It will create a HttpResponse for the EndPoint to send to the client.

Parameters:
request -
Returns:
A HttpResponse that corresponds to the HttpRequest being handled.
Throws:
java.io.IOException

post

public void post(java.lang.Runnable runnable)
This method posts a Runnable onto the Server's task queue. The server's ThreadPool will service the runnable once a thread is freed up.

Parameters:
runnable - An instance of Runnable that the user wishes to run on the server's ThreadPool.

getResponseListeners

public ResponseListener getResponseListeners()
Returns the instance of the ResponseListener for this Server.

Returns:
the ResponseListener for this Server, or null if there is none.

setResponseListener

public void setResponseListener(ResponseListener listener)
This sets the ResponseListener for entire server. All replys being sent to any client will be notified to this instance.

Parameters:
listener - the instance of a ResponseListener to use for this Server.

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Throws:
java.io.IOException

processArguments

protected void processArguments(java.lang.String[] args,
                                java.util.Properties props)
                         throws java.io.IOException
Throws:
java.io.IOException

loadConfiguration

protected void loadConfiguration(java.lang.String config,
                                 java.util.Properties props)
                          throws java.io.IOException
Throws:
java.io.IOException