kevinross 2003/07/14 11:30:55
Modified: java/src/org/apache/xindice/server XindiceServlet.java Log: refactored for: 1. consistency of loading between embed and xmlrpc options 2. reduce double checks for log levels when not doing an expensive logging operation 3. exceptions. Wow. was try/catch, don't always log, sometimes do, catches everywhere. Revision Changes Path 1.16 +113 -121 xml-xindice/java/src/org/apache/xindice/server/XindiceServlet.java Index: XindiceServlet.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/server/XindiceServlet.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- XindiceServlet.java 14 Jul 2003 16:30:12 -0000 1.15 +++ XindiceServlet.java 14 Jul 2003 18:30:55 -0000 1.16 @@ -76,7 +76,7 @@ import org.apache.xindice.core.Database; import org.apache.xindice.server.rpc.RPCMessageInterface; import org.apache.xindice.util.Configuration; -import org.apache.xindice.util.XindiceException; +import org.apache.xindice.util.ConfigurationException; import org.apache.xindice.xml.dom.DOMParser; import org.apache.xmlrpc.XmlRpc; import org.apache.xmlrpc.XmlRpcServer; @@ -95,7 +95,7 @@ private static final String DEFAULT_XMLRPC_DRIVER = "xerces"; protected static Log log = LogFactory.getLog("org.apache.xindice.servlet"); protected Database database; - protected XmlRpcServer xmlrpc; + protected XmlRpcServer xmlrpcServer; private String xmlRpcDriver; @@ -107,9 +107,8 @@ if (database != null) { database.close(); } - if (log.isInfoEnabled()) { - log.info("Database successfully closed"); - } + + log.info("Database successfully closed"); } catch (Exception e) { log.error("Error in destroy", e); @@ -128,7 +127,7 @@ * only XML-RPC query is supported. */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - byte[] result = xmlrpc.execute(request.getInputStream()); + byte[] result = xmlrpcServer.execute(request.getInputStream()); response.setContentType("text/xml"); response.setContentLength(result.length); OutputStream output = response.getOutputStream(); @@ -152,70 +151,62 @@ * TODO: verify that if is an error occured, the database will be closed * propertly */ - public void init(ServletConfig config) throws ServletException { + public void init(ServletConfig servletConfig) throws ServletException { - /* note: there no need to call the DatabaseManager since we already - * know the database we are using: the XML-RPC one */ - database = new Database(); + Configuration configuration = loadConfiguration(servletConfig); - /* holds the database configuration */ - Document configurationDocument; + // + // The configuration is wrapped in a <xindice> element so we need to get the "root-collection" configuration. + // try { - configurationDocument = loadConfiguration(config); - } - catch (Exception e) { - throw new ServletException("Could not load database configuration", e); - } + Configuration rootCollectionConfiguration = configuration.getChild("root-collection"); - Configuration configuration = new Configuration(configurationDocument, false); + if (rootCollectionConfiguration == null) { - // The configuration is wrapped in a <xindice> element so we need to get - // the "root-collection" configuration. - try { - Configuration collConf = configuration.getChild("root-collection"); - if (collConf != null) { + throw new ConfigurationException("The database configuration is missing the <root-collection> element"); + } + else { + // // 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 = collConf.getAttribute(Database.DBROOT, "./db/"); + // 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 = rootCollectionConfiguration.getAttribute(Database.DBROOT, "./db/"); File dbRootDir = new File(dbRoot); - if (dbRootDir.isAbsolute()) { - database.setConfig(collConf); - } - else { - // OK, no absolute path. We have to perform some checks. - - // Stupid hack but spec compliant. If getRealPath() returns null - // the war archive has not been unpacked. - String realPath = config.getServletContext().getRealPath("/WEB-INF"); + // + // If there is no absolute path, we have to perform some checks. + // + if (!dbRootDir.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.db.home"); if (home != null) { - collConf.setAttribute(Database.DBROOT, home + System.getProperty("file.separator") + dbRoot); + rootCollectionConfiguration.setAttribute(Database.DBROOT, home + System.getProperty("file.separator") + dbRoot); } else if (realPath != null) { - if (log.isWarnEnabled()) { - log.warn( - "The database root directory has been set to " - + realPath - + System.getProperty("file.separator") - + dbRoot - + ". Keep in mind that if a war upgrade will take place the database" - + " will be lost."); - } - collConf.setAttribute(Database.DBROOT, realPath + System.getProperty("file.separator") + dbRoot); + log.warn( + "The database root directory has been set to " + + realPath + + System.getProperty("file.separator") + + dbRoot + + ". Keep in mind that if a war upgrade will take place the database will be lost."); + + rootCollectionConfiguration.setAttribute(Database.DBROOT, realPath + System.getProperty("file.separator") + dbRoot); } else { - log.fatal( + throw new ConfigurationException( "The database configuration points to a relative path, " + "but there was no xindice.home property set. " + "Furthermore, the war was not unpacked by the application server " @@ -223,104 +214,105 @@ + "Please check /WEB-INF/system.xml and set an absolute path " + "as the \"dbroot\" attribute of \"root-collection\" " + "or specify a suitable xindice.db.home system property."); - throw new ServletException("An error occurred initializing the database." + "Please see the logs for details"); } - } - database.setConfig(collConf); + // + // We need to use this method to be consistent between deployments (embed, standalone, etc) + // and let the Database object maintain the set of Databases. + // + this.database = Database.getDatabase(rootCollectionConfiguration); + } + + // 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 = xmlRpcDriverConfiguration.getAttribute("name", DEFAULT_XMLRPC_DRIVER); + if (xmlrpcDriver == null || xmlrpcDriver.equals("")) { // this will probably never happen due to the default. + + throw new ConfigurationException( + "The xml-rpc configuration (in '" + + servletConfig.getInitParameter(Xindice.PROP_XINDICE_CONFIGURATION) + + "' contains a malformed 'driver' element." + + " The 'driver' element must have a 'name' attribute specifying the driver (XML-RPC terminology for SAX parser) to use."); + } + } + } + try { + XmlRpc.setDriver(xmlrpcDriver); } - else { - throw new ServletException("The database configuration is missing the <root-collection> element"); + catch (Exception e) { + throw new ConfigurationException("Failed to set driver for XmlRpc to: " + xmlrpcDriver, e); } - } - catch (Exception e) { - throw new ServletException("Error while handling the configuration", e); - } - // 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 = xmlRpcDriverConfiguration.getAttribute("name", DEFAULT_XMLRPC_DRIVER); - if (xmlrpcDriver == null || xmlrpcDriver.equals("")) { // this will probably never happen due to the default. - - throw new ServletException( - "The xml-rpc configuration (in '" - + config.getInitParameter(Xindice.PROP_XINDICE_CONFIGURATION) - + "' contains a malformed 'driver' element." - + " The 'driver' element must have a 'name' attribute specifying the driver" - + " (XML-RPC terminology for SAX parser) to use."); - } + // 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); } - } - try { - XmlRpc.setDriver(xmlrpcDriver); - } - catch (Exception e) { - throw new ServletException("XML-RPC error message: '" + e.getMessage() + "'", e); + log.info("Database successfully started"); } + catch (RuntimeException e) { - // Create the XML-RPC server and add our handler as the default. - xmlrpc = new XmlRpcServer(); - try { - xmlrpc.addHandler("$default", new RPCMessageInterface()); + log.fatal(e); + throw new ServletException("Error while handling the configuration", e); } catch (Exception e) { - throw new ServletException("Startup error", e); - } - if (log.isInfoEnabled()) { - log.info("Database successfully started"); - //log.info("database location: '" + first.getFirstChild().getNodeValue() + "'"); + log.fatal(e); + throw new ServletException("Error while handling the configuration", e); } } /** - * Loads the Xindice configuration file. The file is searched in the - * following locations: + * 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> - * TODO: we should probably try to load from the file system if we - * can't load it this way. + * TODO: we should probably try to load from the file system if we can't load it this way. */ - public Document loadConfiguration(ServletConfig config) throws XindiceException, IOException { - Document result; + public Configuration loadConfiguration(ServletConfig servletConfig) { - String path = config.getInitParameter(Xindice.PROP_XINDICE_CONFIGURATION); - if (path != null) { - if (log.isDebugEnabled()) { + try { + Document configurationDocument; + + String path = servletConfig.getInitParameter(Xindice.PROP_XINDICE_CONFIGURATION); + if (path != null) { log.debug("loading configuration from " + path); + ServletContext context = servletConfig.getServletContext(); + InputStream inputStream = context.getResourceAsStream(path); + configurationDocument = DOMParser.toDocument(inputStream); + inputStream.close(); } - ServletContext context = config.getServletContext(); - InputStream fis = context.getResourceAsStream(path); - result = DOMParser.toDocument(fis); - fis.close(); - } - else { - if (log.isDebugEnabled()) { + else { log.debug("loading the standard configuration"); + configurationDocument = DOMParser.toDocument(Xindice.DEFAULT_CONFIGURATION); } - result = DOMParser.toDocument(Xindice.DEFAULT_CONFIGURATION); + + return new Configuration(configurationDocument, false); } + catch (Exception e) { - return result; + throw new ConfigurationException("Failed to load configuration.", e); + } } - }