FileDocCategorySizeDatePackage
XmlRpcConfiguration.javaAPI DocExample12480Sat Jun 03 18:15:44 BST 2000com.oreilly.xml

XmlRpcConfiguration

public class XmlRpcConfiguration extends Object
XmlRpcConfiguration is a utility class that will load configuration information for XML-RPC servers and clients to use.
author
Brett McLaughlin
version
1.0

Fields Summary
private InputStream
in
The stream to read the XML configuration from
private int
portNumber
Port number server runs on
private String
hostname
Hostname server runs on
private String
driverClass
SAX Driver Class to load
private Hashtable
handlers
Handlers to register in XML-RPC server
private org.jdom.Document
doc
JDOM Document tied to underlying XML
Constructors Summary
public XmlRpcConfiguration(String filename)

This will set a filename to read configuration information from.

param
filename String name of XML configuration file.


        this(new FileInputStream(filename));
    
public XmlRpcConfiguration(InputStream in)

This will set a filename to read configuration information from.

param
in InputStream to read configuration information from.


        this.in = in;
        portNumber = 0;
        hostname = "";
        handlers = new Hashtable();

        // Parse the XML configuration information
        parseConfiguration();
    
Methods Summary
public java.lang.StringgetDriverClass()

This returns the SAX driver class to load.

return
String - name of SAX driver class.

        return driverClass;
    
public java.util.HashtablegetHandlers()

This returns the handlers the server should register.

return
Hashtable of handlers.

        return handlers;
    
public java.lang.StringgetHostname()

This returns the hostname the server listens on.

return
String - hostname of server.

        return hostname;
    
public intgetPortNumber()

This returns the port number the server listens on.

return
int - number of server port.

        return portNumber;
    
private voidparseConfiguration()

Parse the XML configuration information and make it available to clients.

throws
IOException when errors occur.


        try {
            // Request DOM Implementation and Xerces Parser
            Builder builder =
                new DOMBuilder("org.jdom.adapters.XercesDOMAdapter");

            // Get the Configuration Document, with validation
            doc = builder.build(in);

            // Get the root element
            Element root = doc.getRootElement();

            // Get the JavaXML namespace
            Namespace ns = Namespace.getNamespace("JavaXML",
                           "http://www.oreilly.com/catalog/javaxml/");

            // Load the hostname, port, and handler class
            hostname = root.getChild("hostname", ns).getContent();
            driverClass = root.getChild("parserClass", ns).getContent();
            portNumber =
                Integer.parseInt(root.getChild("port", ns).getContent());

            // Get the handlers
            List handlerElements =
                root.getChild("xmlrpc-server", ns)
                    .getChild("handlers", ns)
                    .getChildren("handler", ns);

            Iterator i = handlerElements.iterator();
            while (i.hasNext()) {
                Element current = (Element)i.next();
                handlers.put(current.getChild("identifier", ns).getContent(),
                             current.getChild("class", ns).getContent());
            }
        } catch (JDOMException e) {
            throw new IOException(e.getMessage());
        }
    
public synchronized voidsaveConfiguration(java.lang.String filename)

This will save the current state out to the XML-RPC configuration file.

throws
IOException - when errors occur in saving.


        saveConfigurationFromScratch(new FileOutputStream(filename));
    
public synchronized voidsaveConfiguration(java.io.OutputStream out)

This will save the current state out to the specified OutputStream.

throws
IOException - when errors occur in saving.


        try {
            Element root = doc.getRootElement();

            // Get the JavaXML namespace
            Namespace ns = Namespace.getNamespace("JavaXML",
                           "http://www.oreilly.com/catalog/javaxml/");

            // Update the hostname
            root.getChild("hostname", ns)
                .setContent(hostname);

            // Update the SAX driver class
            root.getChild("parserClass", ns)
                .setContent(driverClass);

            // Update the port number
            root.getChild("port", ns)
                .setContent(portNumber + "");

            // Easier to remove and re-add handlers
            Element handlersElement =
                root.getChild("xmlrpc-server", ns)
                    .getChild("handlers", ns);
            handlersElement.removeChildren("handler", ns);

            // Add new handlers
            Enumeration handlerIDs = handlers.keys();
            while (handlerIDs.hasMoreElements()) {
                String handlerID =
                    (String)handlerIDs.nextElement();

                // Ensure we don't register any blank string
                if (handlerID.trim().equals("")) {
                    continue;
                }

                String handlerClass =
                    (String)handlers.get(handlerID);

                handlersElement.addChild(
                    new Element("handler", ns)
                        .addChild(
                            new Element("identifier", ns)
                                .setContent(handlerID))
                        .addChild(
                            new Element("class", ns)
                                .setContent(handlerClass))
                        );
            }

            // Output the document, use standard formatter
            XMLOutputter fmt = new XMLOutputter();
            fmt.output(doc, out);

        } catch (JDOMException e) {
            // Log an error
            throw new IOException(e.getMessage());
        }
    
public synchronized voidsaveConfigurationFromScratch(java.io.OutputStream out)

This will save the current state out to the specified OutputStream.

throws
IOException - when errors occur in saving.


        // Get the JavaXML namespace
        Namespace ns = Namespace.getNamespace("JavaXML",
                       "http://www.oreilly.com/catalog/javaxml/");

        // Create the root element
        Element root = new Element("xmlrpc-config", ns);
        Document doc = new Document(root);
        doc.setDocType(new DocType("JavaXML:xmlrpc-config",
                                   "DTD/XmlRpc.dtd"));

        root.addChild(new Element("hostname", ns)
                .setContent(hostname))
            .addChild(new Element("port", ns)
                .addAttribute("type", "unprotected")
                .setContent(portNumber + ""))
            .addChild(new Element("parserClass", ns)
                .setContent(driverClass));

        Element handlersElement = new Element("handlers", ns);
        Enumeration e = handlers.keys();
        while (e.hasMoreElements()) {
            String handlerID = (String)e.nextElement();
            String handlerClass = (String)handlers.get(handlerID);

            handlersElement.addChild(new Element("handler", ns)
                .addChild(new Element("identifier", ns)
                    .setContent(handlerID))
                .addChild(new Element("class", ns)
                    .setContent(handlerClass))
                );
        }

        root.addChild(new Element("xmlrpc-server", ns)
                .addChild(handlersElement));

        // Output the document, use standard formatter
        XMLOutputter fmt = new XMLOutputter();
        fmt.output(doc, out);
    
public voidsetDriverClass(java.lang.String driverClass)

This will set the driver class for parsing.

param
driverClass String name of parser class.

        this.driverClass = driverClass;
    
public voidsetHandlers(java.util.Hashtable handlers)

This will set the handlers to register.

param
handlers Hashtable of handler to register.

        this.handlers = handlers;
    
public voidsetHostname(java.lang.String hostname)

This will set the hostname for the server to listen to.

param
hostname String name of server's host.

        this.hostname = hostname;
    
public voidsetPortNumber(int portNumber)

This will set the port number to listen to.

param
portNumber int port to listen to.

        this.portNumber = portNumber;