You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by ks...@apache.org on 2002/07/11 09:57:06 UTC

cvs commit: xml-xindice/java/src/org/apache/xindice/client/xmldb/xmlrpc/services CollectionManagementServiceImpl.java QueryService.java XPathQueryServiceImpl.java XUpdateQueryServiceImpl.java

kstaken     2002/07/11 00:57:06

  Added:       java/src/org/apache/xindice/client/xmldb/xmlrpc
                        CollectionImpl.java DatabaseImpl.java
               java/src/org/apache/xindice/client/xmldb/xmlrpc/services
                        CollectionManagementServiceImpl.java
                        QueryService.java XPathQueryServiceImpl.java
                        XUpdateQueryServiceImpl.java
  Log:
  Adding the basic XML-RPC based XML:DB implementation to the main tree. This
  isn't complete yet, but it's getting better.
  
  Revision  Changes    Path
  1.1                  xml-xindice/java/src/org/apache/xindice/client/xmldb/xmlrpc/CollectionImpl.java
  
  Index: CollectionImpl.java
  ===================================================================
  package org.apache.xindice.client.xmldb.xmlrpc;
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: CollectionImpl.java,v 1.1 2002/07/11 07:57:06 kstaken Exp $
   */
  import org.xmldb.api.base.*;
  import org.xmldb.api.modules.*;
  import org.apache.xmlrpc.*;
  import org.apache.xindice.util.*;
  import org.apache.xindice.server.rpc.RPCDefaultMessage;
  import java.util.Hashtable;
  import java.util.Vector;
  import java.net.MalformedURLException;
  import org.apache.xindice.client.xmldb.resources.XMLResourceImpl;
  import org.apache.xindice.xml.TextWriter;
  import org.xmldb.api.base.ResourceSet;
  import javax.xml.parsers.DocumentBuilderFactory;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  import org.xml.sax.InputSource;
  import java.io.StringReader;
  import org.apache.xindice.client.xmldb.CommonConfigurable;
  import org.apache.xindice.client.xmldb.ResourceSetImpl;
  import org.apache.xindice.client.xmldb.ResourceIteratorImpl;
  
  /**
   * Implementation of XML:DB's <code>Collection</code> interface using
   * XML-RPC to interact with database server
   *
   * @author James Bates <ja...@amplexor.com>
   * @author Kimbro Staken <ks...@xmldatabases.org>
   * @version 1
   */
  public class CollectionImpl extends CommonConfigurable implements Collection {
  
      /* Named service classes map to instantiate for each collection */
      private static String[] serviceClassesMap =
      {  "org.apache.xindice.client.xmldb.xmlrpc.services.XPathQueryServiceImpl",
        "org.apache.xindice.client.xmldb.xmlrpc.services.XUpdateQueryServiceImpl",
        "org.apache.xindice.client.xmldb.xmlrpc.services.CollectionManagementServiceImpl" };
        
      /* Instantiated named services map */
      private Hashtable servicesMap = new Hashtable();
      
      /* path to XML-RPC service on database */
      private static String XINDICE_SERVICE_LOCATION = "/RPC2";
  
      /* Xindice query result meta-info namespace */
      public static final String QUERY_NS = "http://xml.apache.org/xindice/Query";
      
      /* host and port number of server */
      private String hostPort;
      
      /* path to collection on target server */
      private String collPath;
      
      /* the XML-RPC client stub, connected to server */
      private XmlRpcClient client = null;
  
  
      /**
      /* Creates new <code>CollectionImpl</code> instance representing connection
       * to server collection.
       *
       * @param hostPort hostname and port number in <code>host:port</code> format.
       *        Port no is optional, in which case HTTP default is assumed.
       * @exception XMLDBException thrown if a connection could not be established,
       *            because of URL syntax errors, or connection failure, or if no
       *            collection with path <code>collPath</code> could be located.
       */
      public CollectionImpl(String hostPort, String collPath) throws XMLDBException {
          
          this.hostPort = hostPort;
          this.collPath = collPath;
          String xmlrpcURI = "http://" + hostPort + XINDICE_SERVICE_LOCATION;
          XmlRpc.setEncoding("UTF8");
          try {
              
              XmlRpc.setDriver("xerces");
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "Xerces needed");
          }
       
          try {
              
              client = new XmlRpcClient(xmlrpcURI);
              
              /* Just check the collection does actually exist */
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              runRemoteCommand("GetCollectionConfiguration", params);
          } catch (MalformedURLException e) {
              
              throw new XMLDBException(ErrorCodes.INVALID_URI);
          } catch (Exception e) {
  
              throw new XMLDBException(ErrorCodes.NO_SUCH_COLLECTION,
                                       "Collection not found: " + collPath);
          }
          
          for (int i=0; i < serviceClassesMap.length; i++) {
  
              try {
              
                  /*
                   * Look up service constructor that accepts CollectionImpl
                   * argument
                   */
                  Service s = (Service) 
                          Class.forName(serviceClassesMap[i]).newInstance();
                  s.setCollection(this);
                  
                  servicesMap.put(s.getName() + s.getVersion(), s);
              } catch (InstantiationException e) {
              
                  throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "Couldn't create "
                         + serviceClassesMap[i] + " service: " + e.getMessage());
              } catch (ClassNotFoundException e) {
                  
                  throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "Couldn't create "
                         + serviceClassesMap[i] + " service: " + e.getMessage());
              } catch (IllegalAccessException e) {
                  
                  throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "Couldn't create "
                         + serviceClassesMap[i] + " service: " + e.getMessage());
              }
          } 
      }
      
      /**
       * Submits a command for RPC to database server
       *
       * @param cmdName command name
       * @param params hashtable containing named parameters to send to server
       * @return the return value from the server. Type of return value depends on
       *         command.
       *
       * @exception Exception thrown if XML-RPC reports an exception.
       */
      private Object runRemoteCommand(String cmdName, Hashtable params) throws Exception {
          
          params.put("message", cmdName);
          
          Vector v = new Vector();
          v.add(params);
          return ((Hashtable) client.execute("run",v)).get(RPCDefaultMessage.RESULT);
      }
          
          
      /**
       * Returns the available services for this collection
       *
       * @return an array with one entry per available service
       */
      public Service[] getServices() {
  
          return (Service[]) servicesMap.entrySet().toArray(
                  new Service[servicesMap.size()]);
      }
      
      /**
       * Returns the named resource in this collection
       *
       * @param resourceName resource name
       * @return the resource
       */
      public Resource getResource(String resourceName) throws XMLDBException {
  
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              params.put(RPCDefaultMessage.NAME, resourceName);
              params.put(RPCDefaultMessage.COMPRESSED, "true");
              
              Object result = runRemoteCommand("GetDocument", params);
              /*
               * If we get a Hashtable back then the result is compressed.
               */
              if ( result instanceof Hashtable ) {
                 Hashtable compressed = (Hashtable) result;
                 SymbolDeserializer symbolDeserial = new SymbolDeserializer();
                 return new XMLResourceImpl(resourceName, this,
                                            symbolDeserial.getSymbols(compressed),
                                            (byte []) compressed.get("document"));
              }
              else {
                 return new XMLResourceImpl(resourceName, (String) result, this);   
              }
              
          } catch (Exception e) {           
              return null;
          }
      }
      
      /**
       * Returns number of resources in this collection.
       *
       * @return the number of resources
       * @exception XMLDBException thrown if count could not be obtained for
       *            some unexpected reason
       */
      public int getResourceCount() throws XMLDBException {
          
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              return ((Integer) runRemoteCommand(
                      "GetDocumentCount", params)).intValue();
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }
      }
      
      /**
       * Stores contents of reource back to database
       *
       * @param resource the resource to store in database
       * @exception XMLDBException thrown if resource was of incompatible type,
       *            or if some other error condition arises.
       */
      public void storeResource(Resource resource) throws XMLDBException {
          
          if (!(resource instanceof XMLResource)) {
           
              throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
                      "Only XML resources supported");
          }
          
          if (resource.getContent() == null) {
              
              throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
                      "no resource data");
          }
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              params.put(RPCDefaultMessage.NAME, resource.getId());
              params.put(RPCDefaultMessage.DOCUMENT, resource.getContent());
  
              String name = (String) runRemoteCommand("InsertDocument", params);
              ((XMLResourceImpl) resource).setId(name);
              
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }
      }
      
      /**
       * Looks up named service for this collection.
       *
       * @param name service name
       * @param version service version
       */
      public Service getService(String name, String version) throws XMLDBException {
          
          Service s = (Service) servicesMap.get(name + version);
          
          return s;
      }
      
      /**
       * Checks if the collection is still open. Only open collections are safe
       * to work with.
       *
       * @return whether the collection is still open
       */
      public boolean isOpen() {
  
         return (client != null);
      }
      
      /**
       * Throws an exception if collection is no longer open
       *
       * @exception XMLDBException thrown if collection is closed
       */
      private void checkOpen() throws XMLDBException {
         
          if (!isOpen()) {
              
              throw new XMLDBException(ErrorCodes.COLLECTION_CLOSED);
          }
      }
      
      /**
       * Returns this collection's name
       *
       * @return collection name
       */
      public String getName() {
  
          return collPath.substring(collPath.lastIndexOf('/') + 1);
      }
          
      /**
       * Returns complete path to collection
       *
       * @return the collection path
       */
      public String getPath() {
          
          return collPath;
      }
      
      /**
       * Returns XML:DB URI that would retrieve this collection
       *
       * @return a complete XML:DB URI
       */
      public String getURI() {
          
          return "xmldb:" + DatabaseImpl.DRIVER_NAME + "://"
                  + hostPort + collPath;
      }
      
      /**
       * Returns child collection of this collection with specified name
       *
       * @param colName child's name
       * @return the child collection
       * @exception XMLDBException thrown if there is no such child, or some
       *            other connection error occurred
       */
      public Collection getChildCollection(String collName) throws XMLDBException {
  
          if (collName.indexOf('/') != -1) {
              
              throw new XMLDBException(ErrorCodes.INVALID_COLLECTION);
          }
          
          return new CollectionImpl(hostPort, collPath + "/" + collName);
      }
          
      /**
       * Creates a new identifier that can be used as name for a resource in this
       * collection.
       *
       * @return the new identifier name
       * @exception XMLDBException thrown in case of some unknwon error condition
       */
      public String createId() throws XMLDBException {
  
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              return (String) runRemoteCommand("CreateNewOID", params);
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }
      }
      
      /**
       * Closes physical connection to server containing this collection.
       *
       * After calling this method, most methods will throw a <code>XMLDBException</code>
       * exception with code <code>ErrorCodes.COLLECTION_CLOSED</code>. The collection
       * object will in effect become useless.
       */
      public void close() throws org.xmldb.api.base.XMLDBException {
      
          client = null;
      }
      
      /**
       * Constructs a new resource that will belong in this collection.
       *
       * Only XML resources are supported. To save the resource to the database, you
       * must first set the resource's XML data, and then call
       * <code>storeResource()</code>.
       *
       * @param name name for new resource. If empty or <code>null</code>, a name
       *        will be assigned when storing the resource in the database.
       * @param type must be <code>XMLResource</code>.
       * @exception XMLDBException thrown in case of an invalid resource type or name
       */
      public Resource createResource(String name, String type) throws XMLDBException {
          if (name == null) {
  
              name = "";
          }
          
          if (name.indexOf('/') != -1) {
              
              throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, 
                      "Name cannot contain '/'");
          }
          
          if (!"XMLResource".equals(type)) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_RESOURCE_TYPE,
                      "only XMLResources supported");
          }
          
          return new XMLResourceImpl(name, this);
      }
      
      /**
       * Returns the parent collection, if any, of this collection
       *
       * @return the parent collectionm
       * @exception XMLDBException thrown if this is the root collection (which
       *            has no parent), or in case of some other unknown error.
       */
      public Collection getParentCollection() throws XMLDBException {
  
          // If there's only one slash then it's the root.
          if (collPath.lastIndexOf("/") == 0) {
              return null;            
          }
    
          return new CollectionImpl(hostPort, collPath.substring(0,
                  collPath.lastIndexOf('/')));
      }
      
      /**
       * Removes a (XML) resource from this collection.
       *
       * The resource object itself is still valid after removal, and can be used
       * to modify the resource and possibly store it back to the database again.
       *
       * @param resource resource to remove from the databasew
       * @exception XMLDBException thrown if the resource couldn't be located, or
       *            in case of some other error
       */
      public void removeResource(Resource resource) throws XMLDBException {
          
          if (!(resource instanceof XMLResource)) {
           
              throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
                      "Only XML resources supported");
          }
          
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              params.put(RPCDefaultMessage.NAME, resource.getId());
              runRemoteCommand("RemoveDocument", params);
          } catch (Exception e) {            
              throw new XMLDBException(ErrorCodes.NO_SUCH_RESOURCE, e.getMessage());
          }
      }
      
      /**
       * Lists the available child collections of this collection
       *
       * @return array containing child collection names
       * @exception XMLDBException thrown in case of unknwon error condition
       */
      public String[] listChildCollections() throws XMLDBException {
          
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              Vector list = (Vector) runRemoteCommand("ListCollections", params);
              
              return (String[]) list.toArray(new String[list.size()]);
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }   
      }
      
      /**
       * Returns number of child collections in this collection
       *
       * @return the number of child collections
       * @exception XMLDBException thrown in case of unknwon error condition
       */
      public int getChildCollectionCount() throws XMLDBException {
  
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              Integer result = (Integer) runRemoteCommand("GetCollectionCount", params);
              return result.intValue();
              
          } catch (Exception e) {
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }   
      }
      
      /**
       * Lists the available documents of this collection
       *
       * @return array containing resource names
       * @exception XMLDBException thrown in case of unknwon error condition
       */
      public String[] listResources() throws XMLDBException {
          
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              Vector list = (Vector) runRemoteCommand("ListDocuments", params);
              
              return (String[]) list.toArray(new String[list.size()]);
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }   
      }  
  
      /**
       * Queries the entire collection and resturns the result
       *
       * @param name name of document to query
       * @param queryLang <code>XUpdate</code> or <code>XPath</code> 
       * @param query the text of the query statement
       * @param nsMap namespace bindings to use when evaluating query
       * @return set containing result of query
       * @throws XMLDBException thrown in case of invalid query or other error
       */
      public ResourceSet query(String queryLang, String query, Hashtable nsMap)
              throws XMLDBException {
                  
          return query(null, queryLang, query, nsMap);
      }
  
      /**
       * Queries a specific document or the entire collection and returns result
       *
       * @param name name of document to query, or <code>null</code> to query
       *        entire collection.
       * @param queryLang <code>XUpdate</code> or <code>XPath</code>.
       * @param query the text of the query statement
       * @param nsMap namespace bindings to use when evaluating query
       * @return set containing result of query
       * @throws XMLDBException thrown in case of invalid query or other error
       */
      public ResourceSet query(String name, String queryLang, String query, Hashtable nsMap)
              throws XMLDBException {
                  
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              params.put(RPCDefaultMessage.TYPE, queryLang);
              params.put(RPCDefaultMessage.NAMESPACES, nsMap);
              params.put(RPCDefaultMessage.QUERY, query);
              
              if (name != null) {
                  
                  params.put(RPCDefaultMessage.NAME, name);
              }
              
              String result = (String) runRemoteCommand("Query", params);
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              dbf.setNamespaceAware(true);
              Document resultDoc = dbf.newDocumentBuilder().parse(
                  new InputSource(new StringReader(result)));
              
              NodeList nodes = resultDoc.getDocumentElement().getChildNodes();
              ResourceSetImpl rs = new ResourceSetImpl(this, null);
              
              for (int i = 0; i < nodes.getLength(); i++) {
                  
                  Node n = nodes.item(i);
              
                  String documentId = ((Element) n).getAttributeNS(
                          QUERY_NS, "key");
              
                  XMLResourceImpl resource = new XMLResourceImpl(documentId, 
                          TextWriter.toString(n), this);
                  rs.addResource(resource);
              }
           
              return rs;
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, 
                      "Query error: " + e.getMessage());
          }
      }
      
      /**
       * Creates a new child collection in this collection
       *
       * @param childName name for new child collection
       * @return object representing newly created collection
       * @exception XMLDBException thrown if collection createion fails for some
       *                           reason
       */
      public Collection createCollection(String childName)
              throws XMLDBException {
  
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              params.put(RPCDefaultMessage.NAME, childName);
              runRemoteCommand("CreateCollection", params);
              
              return getChildCollection(childName);
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.INVALID_COLLECTION,
                      "Cannot create child collection: " + e.getMessage());
          }
      }
      
      /**
       * Removes child collection from this collection
       *
       * @param childName name of child collection
       * @exception XMLDBException thrown if collection createion fails for some
       *                           reason
       */
      public void removeCollection(String childName) throws XMLDBException {
          
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              params.put(RPCDefaultMessage.NAME, childName);
              runRemoteCommand("RemoveCollection", params);
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.INVALID_COLLECTION,
                      "Cannot remove child collection: " + e.getMessage());
          }
      }
      
  }
  
  
  
  1.1                  xml-xindice/java/src/org/apache/xindice/client/xmldb/xmlrpc/DatabaseImpl.java
  
  Index: DatabaseImpl.java
  ===================================================================
  package org.apache.xindice.client.xmldb.xmlrpc;
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: DatabaseImpl.java,v 1.1 2002/07/11 07:57:06 kstaken Exp $
   */
  import org.xmldb.api.base.Database;
  import org.xmldb.api.base.Collection;
  import org.xmldb.api.base.XMLDBException;
  import org.xmldb.api.base.ErrorCodes;
  import org.apache.xindice.client.xmldb.CommonConfigurable;
  
  /**
   * implements XML:DB's <code>Database</code> interface using XML-RPC to
   * communicate with the Xindice server.
   *
   * Note this class is a database <em>driver</em>, and one class of this database
   * could be used to connect to <em>many</em> different databases.
   *
   * @author James Bates <ja...@amplexor.com>
   * @version 1
   */
  public class DatabaseImpl extends CommonConfigurable implements Database {
      
      /* prefix used to denote XML:DB URI's that should use this driver */
      static String DRIVER_NAME = "xindice-rpc";
      
      /* XML:DB conformance level of this driver */
      private String CONFORMANCE_LEVEL = "0";
      
      /**
       * Creates new <code>DatabaseImpl</code> instance
       */
      public DatabaseImpl() {
  
      }
  
      /**
       * Checks whether this driver can handle the <code>xmldbURI</code> collection
       * URI.
       *
       * @param xmldbURI XML:DB URI to check, without the 'xmldb:' prefix
       * @return whether this driver can handle it
       */
      public boolean acceptsURI(String xmldbURI) throws XMLDBException {
  
          return ((xmldbURI != null) && xmldbURI.startsWith(getName() + "://"));
      }
      
      /**
       * Returns the collection referred to by <code>xmldbURI</code>, attempting
       * to authenticate access to it using <code>userName</code> and
       * <code>password</code> credentials.
       *
       * @param xmldbURI XML:DB URI to collection, without the 'xmldb:' prefix
       * @param userName username used to log into database server
       * @param password password used to log into database server
       * @exception XMLDBException thrown in case a connection could not be
       *            established
       */
      public Collection getCollection(String xmldbURI, String userName, String password)
              throws XMLDBException {
                  
          /* TODO: introduce authentication some day */
                  
          if (!acceptsURI(xmldbURI)) {
              
              throw new XMLDBException(ErrorCodes.INVALID_URI);
          }
  
          /* Chop off driver prefix, and '://' */
          xmldbURI = xmldbURI.substring(getName().length() + 3);
          
          /* Extract host name & port, if present */
          int firstSlash = xmldbURI.indexOf('/');
          if (firstSlash == -1) {
              
              throw new XMLDBException(ErrorCodes.INVALID_URI);
          }
          
          String hostPort = xmldbURI.substring(0, firstSlash);
          String collPath = xmldbURI.substring(firstSlash);
  
          /* Absent host defaults to localhost and standard Xindice HTTP port */
          if (hostPort.equals("")) {
  
              hostPort = "127.0.0.1:4080";
          }
          
          return new CollectionImpl(hostPort, collPath);
      }
      
      /**
       * Returns the prefix used in XML:DB to denote URI's that this driver can
       * handle.
       *
       * @return the prefix driver name
       */
      public String getName() throws XMLDBException {
  
          return DRIVER_NAME;
      }
          
      public String getConformanceLevel() throws XMLDBException {
  
          return CONFORMANCE_LEVEL;
      }
      
  }
  
  
  
  1.1                  xml-xindice/java/src/org/apache/xindice/client/xmldb/xmlrpc/services/CollectionManagementServiceImpl.java
  
  Index: CollectionManagementServiceImpl.java
  ===================================================================
  package org.apache.xindice.client.xmldb.xmlrpc.services;
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: CollectionManagementServiceImpl.java,v 1.1 2002/07/11 07:57:06 kstaken Exp $
   */
  
  import org.xmldb.api.modules.CollectionManagementService;
  import org.xmldb.api.base.Collection;
  import org.xmldb.api.base.XMLDBException;
  import org.xmldb.api.base.ErrorCodes;
  import org.apache.xindice.client.xmldb.CommonConfigurable;
  import org.apache.xindice.client.xmldb.xmlrpc.CollectionImpl;
  
  /**
   * XML:DB CollectionManagementService implementation using XML-RPC to
   * communicate with Xindice.
   *
   * @author James Bates <ja...@amplexor.com>
   * @version 1.0
   */
  public class CollectionManagementServiceImpl extends CommonConfigurable
          implements CollectionManagementService {
  
      /* The collection this service operates on */
      private Collection coll = null;
      
      /* service version */
      private final static String SERVICE_VERSION = "1.0";
      
      /* service name */
      private final static String SERVICE_NAME = "CollectionManagementService";
      
      /**
       * Returns service version
       *
       * @return <code>1.0</code>
       */
      public String getVersion() throws XMLDBException {
          
          return SERVICE_VERSION;
      }
      
      /**
       * Inserts new child collection into this collection
       *
       * @param childCollName name for new child collection
       * @return the newly created child
       */
      public Collection createCollection(String childCollName)
              throws XMLDBException {
                  
          if (coll == null) {
              
              throw new XMLDBException(ErrorCodes.INVALID_COLLECTION,
                  "Must set collection for this service");
          }
          
          return ((CollectionImpl) coll).createCollection(childCollName);
      }
      
      /**
       * Removes child collection from this collection
       *
       * @param childCollName name of child collection to remove
       */
      public void removeCollection(String childCollName) throws XMLDBException {
          if (coll == null) {
              
              throw new XMLDBException(ErrorCodes.INVALID_COLLECTION,
                  "Must set collection for this service");
          }
          
          ((CollectionImpl) coll).removeCollection(childCollName);
      }
      
      /**
       * Returns service name
       *
       * @return <code>CollectionManagementService</code>
       */
      public String getName() {
          
          return SERVICE_NAME;
      }
      
      /**
       * Sets collection this service should operate on
       *
       * @param collection the collection this service should operate on
       */
      public void setCollection(Collection collection) {
          
          this.coll = collection;
      }    
  }
  
  
  
  1.1                  xml-xindice/java/src/org/apache/xindice/client/xmldb/xmlrpc/services/QueryService.java
  
  Index: QueryService.java
  ===================================================================
  package org.apache.xindice.client.xmldb.xmlrpc.services;
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: QueryService.java,v 1.1 2002/07/11 07:57:06 kstaken Exp $
   */
  
  import org.xmldb.api.base.Service;
  import org.xmldb.api.modules.XPathQueryService;
  import org.xmldb.api.base.Collection;
  import org.apache.xindice.client.xmldb.CommonConfigurable;
  import org.xmldb.api.base.ResourceSet;
  import org.xmldb.api.base.XMLDBException;
  import org.xmldb.api.base.ErrorCodes;
  import org.apache.xindice.client.xmldb.xmlrpc.CollectionImpl;
  import java.util.Hashtable;
  
  /**
   * Abstract query service.
   * 
   * Used to implemented both XUpdate and XPath query
   * service, as the interface for these queries on the XML-RPC API is more
   * or less identical.
   *
   * @author James Bates <ja...@amplexor.com>
   * @version 1.0
   */
  public abstract class QueryService extends CommonConfigurable implements Service {
  
      /* XPath or XUpdate. This property should be set by subclasses */
      protected String queryLang;
      
      /* Collection that this query service should query in */
      Collection collection;
      
      /* namespace bindings in use for this query */
      Hashtable nsMap = new Hashtable();
      
      public String getVersion() {
          
          return "1.0";
      }    
      
      public String getName() {
          
          return queryLang + "QueryService";
      }    
      
      public void setCollection(Collection collection) {
          
          this.collection = collection;
      }
      
      public ResourceSet queryResource(String name, String query)
              throws XMLDBException {
          
          return ((CollectionImpl) collection).query(name, queryLang, query, nsMap);
      }    
  
      public void clearNamespaces() {
          
          nsMap.clear();
      }
      
      public String getNamespace(String prefix) {
          
          return (String) nsMap.get(prefix);
      }
      
      public void setNamespace(String prefix, String namespaceURI) {
          
          nsMap.put(prefix, namespaceURI);
      }
      
      public ResourceSet query(String query) throws XMLDBException {
          
          return ((CollectionImpl) collection).query(queryLang, query, nsMap);
      }
      
      public void removeNamespace(String prefix) throws XMLDBException {
          
          nsMap.remove(prefix);
      }
      
  }
  
  
  
  1.1                  xml-xindice/java/src/org/apache/xindice/client/xmldb/xmlrpc/services/XPathQueryServiceImpl.java
  
  Index: XPathQueryServiceImpl.java
  ===================================================================
  package org.apache.xindice.client.xmldb.xmlrpc.services;
  
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: XPathQueryServiceImpl.java,v 1.1 2002/07/11 07:57:06 kstaken Exp $
   */
  
  import org.xmldb.api.modules.XPathQueryService;
  import org.xmldb.api.base.XMLDBException;
  
  /**
   * XML:DB XPathQueryService implementation that uses XML-RPC communication
   * with server
   *
   * @author James Bates <ja...@amplexor.com>
   * @version 1.0
   */
  public class XPathQueryServiceImpl extends QueryService implements XPathQueryService {
  
      /**
       * Creates new XPathQueryService
       */
      public XPathQueryServiceImpl() {
          
          super();
          queryLang = "XPath";
      }
  }
  
  
  
  1.1                  xml-xindice/java/src/org/apache/xindice/client/xmldb/xmlrpc/services/XUpdateQueryServiceImpl.java
  
  Index: XUpdateQueryServiceImpl.java
  ===================================================================
  package org.apache.xindice.client.xmldb.xmlrpc.services;
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: XUpdateQueryServiceImpl.java,v 1.1 2002/07/11 07:57:06 kstaken Exp $
   */
  
  import org.xmldb.api.modules.XUpdateQueryService;
  import org.xmldb.api.base.XMLDBException;
  import org.xmldb.api.base.ErrorCodes;
  import org.xmldb.api.modules.XMLResource;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  /**
   * XML:DB XUpdateQueryService implementation that uses XML-RPC communication
   * with server
   *
   * @author James Bates <ja...@amplexor.com>
   * @version 1.0
   */
  public class XUpdateQueryServiceImpl extends QueryService implements XUpdateQueryService {
  
      /**
       * Creates new XUpdateQueryService
       */
      public XUpdateQueryServiceImpl() {
          
          super();
          queryLang = "XUpdate";
      }
      
      /**
       * Extracts modified count from the pseudo-result returned from query
       */
      private long getResultCount(XMLResource resource) throws XMLDBException {
          
          Document doc = (Document) resource.getContentAsDOM();
        
          // Get the src:modified element
          Node node = doc.getDocumentElement().getFirstChild();
        
          // The count is a text node within the element.
          String count = node.getFirstChild().getNodeValue();
        
          try {
             
              return Long.parseLong(count);
         } catch (Exception e) {
           
             throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage());
         }
      }
     
      /**
       * Performs an XUpdate operation on the entire collection
       */
      public long update(String query) throws XMLDBException {
          
          return getResultCount((XMLResource) query(query).getResource(0));
      }
      
      /**
       * Preforms an XUpdate operation on the specified document
       */
      public long updateResource(String name, String query) throws XMLDBException {
          
          return getResultCount((XMLResource) queryResource(name, query).getResource(0));
      }
  }