FileDocCategorySizeDatePackage
LightweightXmlRpcServer.javaAPI DocExample7216Wed Sep 19 09:19:24 BST 2001javaxml2

LightweightXmlRpcServer

public class LightweightXmlRpcServer extends Object
LightweightXmlRpcServer is a utility class that will start an XML-RPC server listening for HTTP requests and register a set of handlers, defined in a configuration file.

Fields Summary
private helma.xmlrpc.WebServer
server
The XML-RPC server utility class
private int
port
Port number to listen on
private String
configFile
Configuration file to use
Constructors Summary
public LightweightXmlRpcServer(int port, String configFile)

This will store the requested port and configuration file for the server to use.

param
port int number of port to listen to
param
configFile String filename to read for configuration information.

        this.port = port;
        this.configFile = configFile;
    
Methods Summary
private java.util.HashtablegetHandlers()

This is a method that parses the configuration file (in a very simplistic manner) and reads the handler definitions supplied.

return
Hashtable - class id/class pairs.
throws
IOException - when errors occur in reading/parsing the file.


        Hashtable handlers = new Hashtable();
        
        BufferedReader reader = 
            new BufferedReader(new FileReader(configFile));
        String line = null;
        
        while ((line = reader.readLine()) != null) {
            // Syntax is "handlerName, handlerClass"
            int comma;
            
            // Skip comments
            if (line.startsWith("#")) {
                continue;
            }
            
            // Skip empty or useless lines            
            if ((comma = line.indexOf(",")) < 2) {
                continue;
            }
            
            // Add the handler name and the handler class
            handlers.put(line.substring(0, comma), 
                         line.substring(comma+1));
        }
        
        return handlers;        
    
public static voidmain(java.lang.String[] args)

Provide a static entry point.

      
        if (args.length < 2) {
            System.out.println(
                "Usage: " +
                "java com.oreilly.xml.LightweightXmlRpcServer " +
                "[port] [configFile]");
            System.exit(-1);
        }
        
        LightweightXmlRpcServer server =
            new LightweightXmlRpcServer(Integer.parseInt(args[0]),
                                        args[1]);   

        try {
            // Start the server
            server.start();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }                               
    
private voidregisterHandlers(java.util.Hashtable handlers)

This will register the handlers supplied in the XML-RPC server (typically from {@link #getHandlers()}.

param
handlers Hashtable of handlers to register.

        
        Enumeration handlerNames = handlers.keys();
        
        // Loop through the requested handlers
        while (handlerNames.hasMoreElements()) {
            String handlerName = (String)handlerNames.nextElement();
            String handlerClass = (String)handlers.get(handlerName);
            
            // Add this handler to the server
            try {
                server.addHandler(handlerName, 
                    Class.forName(handlerClass).newInstance());
                
                System.out.println("Registered handler " + handlerName +
                                   " to class " + handlerClass);
            } catch (Exception e) {
                System.out.println("Could not register handler " + 
                                   handlerName + " with class " + 
                                   handlerClass);
            }
        }
    
public voidstart()

This will start up the server.

throws
IOException when problems occur.

        try {
            // Use Apache Xerces SAX Parser
            XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");

            System.out.println("Starting up XML-RPC Server...");
            server = new WebServer(port);                      
            
            // Register handlers
            registerHandlers(getHandlers());

            
        } catch (ClassNotFoundException e) {
            throw new IOException("Error loading SAX parser: " + 
                e.getMessage());
        }