vladimir 2002/11/17 00:56:06
Modified: java/src/org/apache/xindice/client/xmldb/embed DatabaseImpl.java Log: use env. variable to define the configuration file Revision Changes Path 1.5 +59 -37 xml-xindice/java/src/org/apache/xindice/client/xmldb/embed/DatabaseImpl.java Index: DatabaseImpl.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/client/xmldb/embed/DatabaseImpl.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- DatabaseImpl.java 31 Oct 2002 06:58:48 -0000 1.4 +++ DatabaseImpl.java 17 Nov 2002 08:56:06 -0000 1.5 @@ -1,4 +1,3 @@ -package org.apache.xindice.client.xmldb.embed; /* * The Apache Software License, Version 1.1 * @@ -58,26 +57,39 @@ * $Id$ */ +package org.apache.xindice.client.xmldb.embed; + import org.apache.xindice.client.xmldb.CommonConfigurable; import org.apache.xindice.core.Database; +import org.apache.xindice.server.Xindice; import org.apache.xindice.util.Configuration; +import org.apache.xindice.util.XindiceException; import org.apache.xindice.xml.dom.DOMParser; -import org.w3c.dom.*; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.xmldb.api.base.Collection; import org.xmldb.api.base.ErrorCodes; import org.xmldb.api.base.XMLDBException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; + /** * implements XML:DB's <code>Database</code> interface providing * embedded access to a Xindice database. * * @author Kimbro Staken <[EMAIL PROTECTED]> * @author James Bates <[EMAIL PROTECTED]> + * @author <a href="mailto:[EMAIL PROTECTED]">Vladimir R. Bossicard</a> * @version 1 */ public class DatabaseImpl extends CommonConfigurable implements org.xmldb.api.base.Database { - + + private static Log log = LogFactory.getLog("org.apache.xindice.client.embed"); + /* prefix used to denote XML:DB URI's that should use this driver */ static String DRIVER_NAME = "xindice-embed"; @@ -85,37 +97,49 @@ private String CONFORMANCE_LEVEL = "0"; protected Database db; - - /** - * Creates new <code>DatabaseImpl</code> instance - */ - public DatabaseImpl() { - // try to find an existing Database by name - // I assume that the name arg should not be hardcoded, but do - // not know what else to do here (the configXML was hardcoded when I made this change) - // if the configXML is supplied from somewhere else, the name should be taken from there instead - db = Database.getDatabase("db"); - if(db == null) - { - db = new Database(); - } - String configXML = "<root-collection dbroot=\"./db/\" name=\"db\">" + - "<queryengine>" + - "<resolver autoindex=\"false\" class=\"org.apache.xindice.core.query.XPathQueryResolver\" />" + - "<resolver class=\"org.apache.xindice.core.xupdate.XUpdateQueryResolver\" />" + - "</queryengine>" + - "</root-collection>"; - - try { - Document doc = DOMParser.toDocument(configXML); - - db.setConfig(new Configuration(doc.getDocumentElement(), false)); - } - catch (Exception e) { - e.printStackTrace(); - } - - } + + /** + * Creates new <code>DatabaseImpl</code> instance. The configuration is + * loaded from the file defined in the PROP_XINDICE_CONFIGURATION system + * variable. + * + * This is only a temporarly solution since the question of a new init + * method is raised in the xmldb:api group. Another solution could be to + * use the Configurable interface and only create the database when the + * getCollection method is called. + */ + public DatabaseImpl() + throws FileNotFoundException, + XindiceException { + Configuration config; + + String configFile = System.getProperty(Xindice.PROP_XINDICE_CONFIGURATION); + if (configFile != null && !configFile.equals("")) { + FileInputStream configXMLFile = new FileInputStream(new File(configFile)); + + config = new Configuration(DOMParser.toDocument(configXMLFile), false); + } + else { + config = new Configuration(DOMParser.toDocument(Xindice.DEFAULT_CONFIGURATION), false); + } + + config = config.getChild("root-collection", false); + + /* First try to find an existing Database by name. If it doesn't exist, + * we create one and in both cases we configure them with the current + * configuration */ + String dbname = config.getAttribute(Database.NAME); + + this.db = Database.getDatabase(dbname); + if (this.db == null) { + this.db = new Database(); + } + + if (log.isInfoEnabled()) { + log.info("Database name: " + dbname + "'"); + } + db.setConfig(config); + } /** * Checks whether this driver can handle the <code>xmldbURI</code> collection @@ -196,8 +220,6 @@ throw new XMLDBException(ErrorCodes.INVALID_URI, "Collection name must begin with a '/'" ); } - - } /**