With the following patch, you can specify a configuration file where Xindice embedded version will store the database.

Before committing the changes, I would like the opinion of other committers.

-Vladimir

--
Vladimir R. Bossicard
www.bossicard.com
Index: org/apache/xindice/client/xmldb/DatabaseImpl.java
===================================================================
RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/client/xmldb/DatabaseImpl.java,v
retrieving revision 1.9
diff -u -r1.9 DatabaseImpl.java
--- org/apache/xindice/client/xmldb/DatabaseImpl.java   31 Oct 2002 06:58:48 
-0000      1.9
+++ org/apache/xindice/client/xmldb/DatabaseImpl.java   14 Nov 2002 07:15:26 
-0000
@@ -62,6 +62,8 @@
 import org.xmldb.api.base.Database;
 import org.xmldb.api.base.ErrorCodes;
 import org.xmldb.api.base.XMLDBException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * DatabaseImpl is the XML:DB driver implementation for Xindice. It is the 
entry
@@ -79,6 +81,8 @@
 public class DatabaseImpl extends CommonConfigurable
    implements Database {
 
+   private static Log log = 
LogFactory.getLog("org.apache.xindice.client.xmldb");
+
    protected Database driver;
 
    /**
@@ -193,6 +197,9 @@
          }   
       }
       catch (Exception e) {
+         if (log.isInfoEnabled()) {
+            log.info("Exception during creation of the Database", e);
+         }
          throw new XMLDBException(ErrorCodes.INVALID_URI, uri);
       }
    }
Index: org/apache/xindice/client/xmldb/embed/DatabaseImpl.java
===================================================================
RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/client/xmldb/embed/DatabaseImpl.java,v
retrieving revision 1.4
diff -u -r1.4 DatabaseImpl.java
--- org/apache/xindice/client/xmldb/embed/DatabaseImpl.java     31 Oct 2002 
06:58:48 -0000      1.4
+++ org/apache/xindice/client/xmldb/embed/DatabaseImpl.java     14 Nov 2002 
07:15:27 -0000
@@ -1,4 +1,3 @@
-package org.apache.xindice.client.xmldb.embed;
 /*
  * The Apache Software License, Version 1.1
  *
@@ -58,26 +57,39 @@
  * $Id: DatabaseImpl.java,v 1.4 2002/10/31 06:58:48 vladimir Exp $
  */
 
+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 '/'" );
         }
-        
-        
     }
     
     /**
Index: org/apache/xindice/core/Database.java
===================================================================
RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/core/Database.java,v
retrieving revision 1.13
diff -u -r1.13 Database.java
--- org/apache/xindice/core/Database.java       5 Nov 2002 05:55:48 -0000       
1.13
+++ org/apache/xindice/core/Database.java       14 Nov 2002 07:15:31 -0000
@@ -84,7 +84,7 @@
    private static final String QUERYENGINE = "queryengine";
    private static final String DATABASE = "database";
    private static final String COLKEY = "database.xml";
-   private static final String NAME = "name";
+   public static final String NAME = "name";
 
    private static Log log = LogFactory.getLog("org.apache.xindice.core");
 
@@ -116,10 +116,13 @@
       
       String dbroot = config.getAttribute(DBROOT);
       File dbrootDir = new File(dbroot);
-      if (!dbrootDir.isAbsolute())
-            dbrootDir = new File(System.getProperty( PROP_XINDICE_HOME ),
-                  dbroot);
+      if (!dbrootDir.isAbsolute()) {
+         dbrootDir = new File(System.getProperty( PROP_XINDICE_HOME ), dbroot);
+      }
       setCollectionRoot(dbrootDir);
+      if (log.isInfoEnabled()) {
+         log.info("Database points to " + dbrootDir.getAbsolutePath());
+      }
 
       try {
          Configuration queryCfg = config.getChild(QUERYENGINE);
Index: org/apache/xindice/server/Xindice.java
===================================================================
RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/server/Xindice.java,v
retrieving revision 1.8
diff -u -r1.8 Xindice.java
--- org/apache/xindice/server/Xindice.java      31 Oct 2002 07:00:44 -0000      
1.8
+++ org/apache/xindice/server/Xindice.java      14 Nov 2002 07:15:31 -0000
@@ -69,6 +69,7 @@
    // Global Property Information
    public static final String PROP_XINDICE_HOME = "xindice.home";
    public static final String PROP_CMD_HOME = "cmd.home";
+   public static final String PROP_XINDICE_CONFIGURATION = 
"xindice.configuration";
 
    // Version Information
    public static final int MajorVersion = 1;
Index: org/apache/xindice/tools/XMLTools.java
===================================================================
RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/tools/XMLTools.java,v
retrieving revision 1.13
diff -u -r1.13 XMLTools.java
--- org/apache/xindice/tools/XMLTools.java      13 Nov 2002 05:41:02 -0000      
1.13
+++ org/apache/xindice/tools/XMLTools.java      14 Nov 2002 07:15:32 -0000
@@ -65,6 +65,8 @@
 import org.apache.xindice.util.StringUtilities;
 import org.apache.xindice.util.XindiceException;
 import org.apache.xindice.xml.dom.DOMParser;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 import org.w3c.dom.*;
 import org.xmldb.api.DatabaseManager;
@@ -104,8 +106,10 @@
     public static final String AUTO_KEY = "autoKey";
     public static final String NAMESPACES = "namespaces";
     public static final String IMPL_CLASS = "implClass";
-    
-   private Hashtable table;   
+
+   private static Log log = LogFactory.getLog("org.apache.xindice.XMLTools");
+
+   private Hashtable table;
    protected String location = null;
    private boolean initialized = false;
    private static boolean verbose = false;
@@ -270,7 +274,12 @@
          } else if ( token.equalsIgnoreCase("-l") || 
token.equalsIgnoreCase("--localdb") ) {
             table.put( LOCAL, "true");
          } else if ( token.equalsIgnoreCase("-d") || 
token.equalsIgnoreCase("--dbconfig") ) {
-            table.put( DB_CONFIG, at.nextSwitchToken());
+            String configFile = System.getProperty("user.dir") + 
File.separator + at.nextSwitchToken();
+            if (log.isDebugEnabled()) {
+               log.debug(configFile);
+            }
+            System.setProperty(Xindice.PROP_XINDICE_CONFIGURATION, configFile);
+            table.put( DB_CONFIG, configFile);
          } else if ( token.equalsIgnoreCase("-s") || 
token.equalsIgnoreCase("--namespaces") ) {
              table.put(NAMESPACES, at.nextSwitchToken());
             // Index specific options

Reply via email to