Package org.apache.xindice.server

Source Code of org.apache.xindice.server.XindiceServlet

/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* CVS $Id: XindiceServlet.java,v 1.30 2004/02/11 14:03:09 vgritsenko Exp $
*/

package org.apache.xindice.server;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.core.Database;
import org.apache.xindice.server.rpc.RPCMessageInterface;
import org.apache.xindice.util.Configuration;
import org.apache.xindice.util.ConfigurationException;
import org.apache.xindice.xml.dom.DOMParser;
import org.apache.xmlrpc.XmlRpc;
import org.apache.xmlrpc.XmlRpcServer;

import org.w3c.dom.Document;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
* A <code>HttpServlet</code> that enables XML-RPC access to a Xindice
* database instance.
*
* @author <a href="mailto:kstaken@xmldatabases.org">Kimbro Staken</a>
* @author <a href="mailto:vladimir@apache.org">Vladimir R. Bossicard</a>
* @author <a href="mailto:gianugo@apache.org">Gianugo Rabellino</a>
* @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
* @version CVS $Revision: 1.30 $, $Date: 2004/02/11 14:03:09 $
*/
public class XindiceServlet extends HttpServlet {

    private static final Log log = LogFactory.getLog(XindiceServlet.class);

    private static final String DEFAULT_XMLRPC_DRIVER = "xerces";
    protected XmlRpcServer xmlrpcServer;

    public void destroy() {
        // When the servlet engine goes down we need to close the database instance.
        // By the time destroy() is called, no more client requests can come in,
        // so no need to worry about multithreading.
        String[] databases = Database.listDatabases();
        for (int i = 0; i < databases.length; i++) {
            String name = databases[i];
            try {
                Database.getDatabase(name).close();
                log.info("Database '" + name + "' successfully closed");
            } catch (Exception e) {
                log.error("Error closing database '" + name + "'", e);
            }
        }
    }

    /**
     * Delegate GET requests to the UglyBrowser.
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UglyBrowser.doGet(request, response);
    }

    /**
     * Sends an XML query to the server and writes the output back. Currenlty
     * only XML-RPC query is supported.
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        byte[] result = xmlrpcServer.execute(request.getInputStream());
        response.setContentType("text/xml");
        response.setContentLength(result.length);
        OutputStream output = response.getOutputStream();
        output.write(result);
        output.flush();
    }

    /**
     * Initializes database
     */
    public void init(ServletConfig servletConfig) throws ServletException {

        Configuration configuration = loadConfiguration(servletConfig);

        //
        // The configuration is wrapped in a <xindice> element so we need to get the "root-collection" configuration.
        //
        try {
            Configuration[] rootConfigurations = configuration.getChildren("root-collection");
            if (rootConfigurations.length == 0) {
                throw new ConfigurationException("The database configuration is missing the <root-collection> element");
            } else {
                for (int i = 0; i < rootConfigurations.length; i++) {
                    Configuration rootConfiguration = rootConfigurations[i];
                    String name = rootConfiguration.getAttribute(Database.NAME);

                    //
                    // We need to ensure that the database points to a place where it makes
                    // sense. If the path in the system.xml file is an absolute path, then
                    // honor it. If it's not, we first check for the system property "xindice.db.home"
                    // and if the lookup is successful we use it as the database root parent. If
                    // the property is not set, we use /WEB-INF relative to the servlet context, unless
                    // the war has not been unpacked. In this case, we throw an exception and
                    // ask the user to specify the location of database root
                    //
                    String dbRoot = rootConfiguration.getAttribute(Database.DBROOT, Database.DBROOT_DEFAULT);

                    //
                    // If there is no absolute path, we have to perform some checks.
                    //
                    if (!new File(dbRoot).isAbsolute()) {

                        // Stupid hack but spec compliant:
                        // If getRealPath() returns null the war archive has not been unpacked.
                        String realPath = servletConfig.getServletContext().getRealPath("/WEB-INF");

                        // Let's see if the property was specified.
                        String home = System.getProperty(Xindice.PROP_XINDICE_DB_HOME);
                        if (log.isDebugEnabled()) {
                            log.debug(Xindice.PROP_XINDICE_DB_HOME + " is set to " + home);
                        }

                        if (home != null) {
                            dbRoot = new File(home + File.separator + dbRoot).getCanonicalPath();
                        } else if (realPath != null) {
                            dbRoot = new File(realPath + File.separator + dbRoot).getCanonicalPath();
                            log.warn("The database '" + name + "' root directory has been set to " + dbRoot +
                                     ". Keep in mind that if a war upgrade will take place the database will be lost.");
                        } else {
                            throw new ConfigurationException(
                                    "The database '" + name + "' configuration points to a relative path, "
                                    + "but there was no " + Xindice.PROP_XINDICE_DB_HOME + " property set. "
                                    + "Furthermore, the war was not unpacked by the application server "
                                    + "so Xindice was unable to find a database location "
                                    + "Please check /WEB-INF/system.xml and set an absolute path "
                                    + "as the \"dbroot\" attribute of \"root-collection\" "
                                    + "or specify a suitable " + Xindice.PROP_XINDICE_DB_HOME + " system property.");
                        }
                        rootConfiguration.setAttribute(Database.DBROOT, dbRoot);
                    }

                    //
                    // We need to use this method to be consistent between deployments (embed, standalone, etc)
                    // and let the Database object maintain the set of Databases.
                    //
                    Database.getDatabase(rootConfiguration);
                    log.info("Database '" + name + "' successfully opened");
                }
            }

            // Setup the XML-RPC impl to support UTF-8 input via Xerces.
            XmlRpc.setEncoding("UTF8");

            /*
             * Setup the SAX parser XML-RPC impl will use.
             * The XmlRpc.setDriver() method takes either the classname or a shorthand
             * name for the SAX parser it will use.  The default (for backwards compatibility
             * if nothing else) is xerces.
             */
            String xmlrpcDriver = DEFAULT_XMLRPC_DRIVER;

            Configuration xmlRpcConfiguration = configuration.getChild("xml-rpc");
            if (xmlRpcConfiguration != null) {
                Configuration xmlRpcDriverConfiguration = xmlRpcConfiguration.getChild("driver");
                if (xmlRpcDriverConfiguration != null) {
                    // xmlrpcDriver will have non-empty value, guaranteed by providing default value
                    xmlrpcDriver = xmlRpcDriverConfiguration.getAttribute("name", DEFAULT_XMLRPC_DRIVER);
                }
            }

            try {
                XmlRpc.setDriver(xmlrpcDriver);
            } catch (Exception e) {
                throw new ConfigurationException("Failed to set driver for XmlRpc to: " + xmlrpcDriver, e);
            }

            // Create the XML-RPC server and add our handler as the default.
            this.xmlrpcServer = new XmlRpcServer();
            try {
                this.xmlrpcServer.addHandler("$default", new RPCMessageInterface());
            } catch (Exception e) {
                throw new ConfigurationException("Failed to add default handler to XmlRpc server.", e);
            }

            log.info("Xindice server successfully started");
        } catch (Exception e) {
            log.fatal("Failed to initialize database, throwing ServletException", e);
            // Make sure to close database if it was opened already.
            destroy();
            throw new ServletException("Error while handling the configuration", e);
        }
    }

    /**
     * Loads the Xindice configuration file. The file is searched in the following locations:
     * <ul>
     * <li>the <tt>ServletConfig.getInitParameter(Xindice.PROP_XINDICE_CONFIGURATION)</tt> variable located in the servlet
     * configuration file</li>
     * <li>use the default configuration stored in the <tt>Xindice</tt> class</li>
     * </ul>
     *
     * <br/>
     * TODO: we should probably try to load from the file system if we can't load it this way.
     */
    public Configuration loadConfiguration(ServletConfig servletConfig) {
        try {
            Document configurationDocument;

            String path = servletConfig.getInitParameter(Xindice.PROP_XINDICE_CONFIGURATION);
            if (path != null) {
                InputStream inputStream = null;
                if (path.startsWith("/")) {
                    // Absolute file path
                    log.debug("Loading configuration from filesystem path " + path);
                    inputStream = new FileInputStream(path);
                } else {
                    // Relative (to the context) path
                    log.debug("Loading configuration from context path " + path);
                    ServletContext context = servletConfig.getServletContext();
                    inputStream = context.getResourceAsStream(path);
                }

                try {
                    configurationDocument = DOMParser.toDocument(inputStream);
                } finally {
                    try {
                        inputStream.close();
                    } catch (IOException ignored) {
                    }
                }
            } else {
                log.debug("Loading the standard configuration");
                configurationDocument = DOMParser.toDocument(Xindice.DEFAULT_CONFIGURATION);
            }

            return new Configuration(configurationDocument, false);
        } catch (Exception e) {
            throw new ConfigurationException("Failed to load configuration.", e);
        }
    }
}
TOP

Related Classes of org.apache.xindice.server.XindiceServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.