gianugo 2002/11/30 10:32:07
Modified: java/src/org/apache/xindice/server XindiceServlet.java java/src/org/apache/xindice/core Database.java Log: Database root directory fix. If the configuration specifies a relative location, Xindice will look for a xindice.db.home system property and use build an absolute path with it. If such property is not found, it will try to use the webapp /WEB-INF/ directory as the database root directory, issuing a warning to the user. If even that fails (because the WAR is unpacked) an Exception will be thrown and the logs will give a proper advice to the user. Revision Changes Path 1.9 +52 -1 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.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- XindiceServlet.java 21 Nov 2002 07:44:36 -0000 1.8 +++ XindiceServlet.java 30 Nov 2002 18:32:07 -0000 1.9 @@ -75,6 +75,7 @@ import org.w3c.dom.*; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -92,6 +93,7 @@ * * @author <a href="mailto:[EMAIL PROTECTED]">Kimbro Staken</a> * @author <a href="mailto:[EMAIL PROTECTED]">Vladimir R. Bossicard</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Gianugo Rabellino</a> */ public class XindiceServlet extends HttpServlet { @@ -126,7 +128,56 @@ try { conf = conf.getChild("root-collection", false); if (conf != null) { + + // 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 = conf.getAttribute(Database.DBROOT, "./db/"); + File dbRootDir = new File(dbRoot); + + if (dbRootDir.isAbsolute()) { + db.setConfig(conf); + } 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"); + + // Let's see if the property was specified. + String home = System.getProperty("xindice.db.home"); + + if (home != null) { + conf.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."); + conf.setAttribute(Database.DBROOT, realPath + System.getProperty("file.separator") + dbRoot); + } else { + log.fatal("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 " + + "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.db.home system property."); + throw new ServletException("An error occurred initializing the database." + + "Please see the logs for details"); + } + + } + + db.setConfig(conf); + } else { throw new ServletException("The database configuration is missing the <root-collection> element"); } 1.16 +2 -2 xml-xindice/java/src/org/apache/xindice/core/Database.java Index: Database.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/core/Database.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- Database.java 18 Nov 2002 17:18:04 -0000 1.15 +++ Database.java 30 Nov 2002 18:32:07 -0000 1.16 @@ -80,7 +80,7 @@ */ public final class Database extends Collection implements Named { - private static final String DBROOT = "dbroot"; + public static final String DBROOT = "dbroot"; private static final String QUERYENGINE = "queryengine"; private static final String DATABASE = "database"; private static final String COLKEY = "database.xml";