jbates      02/04/30 06:10:14

  Modified:    java/scratchpad/src/org/apache/xindice/server/rpc
                        RPCMessageHandler.java
               java/src/org/apache/xindice/core CollectionManager.java
  Added:       java/scratchpad/src/org/apache/xindice/client/rpc
                        CollectionImpl.java CommonConfigurable.java
                        DatabaseImpl.java XMLResourceImpl.java
               java/scratchpad/src/org/apache/xindice/server/rpc/messages
                        GetCollectionCount.java
  Log:
  Beginning of XML:DB client using XML-RPC
  
  Revision  Changes    Path
  1.1                  
xml-xindice/java/scratchpad/src/org/apache/xindice/client/rpc/CollectionImpl.java
  
  Index: CollectionImpl.java
  ===================================================================
  /*
   * 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 [EMAIL PROTECTED]
   *
   * 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/04/30 13:10:14 jbates Exp $
   */
  
  package org.apache.xindice.client.rpc;
  
  import org.xmldb.api.base.Collection;
  import org.xmldb.api.base.XMLDBException;
  import org.xmldb.api.base.ErrorCodes;
  import org.xmldb.api.base.Service;
  import org.apache.xmlrpc.XmlRpc;
  import org.apache.xmlrpc.XmlRpcClient;
  import org.xmldb.api.base.Resource;
  import org.xmldb.api.modules.XMLResource;
  import org.apache.xindice.server.rpc.RPCDefaultMessage;
  import java.util.Hashtable;
  import java.util.Vector;
  import java.net.MalformedURLException;
  
  /**
   * Implementation of XML:DB's <code>Collection</code> interface using
   * XML-RPC to interact with database server
   *
   * @author James Bates <[EMAIL PROTECTED]>
   * @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.services.XPathQueryServiceImpl",
        "org.apache.xindice.client.services.XUpdateQueryServiceImpl" };
        
      /* Instantiated named services map */
      private Hashtable servicesMap = new Hashtable();
      
      /* path to XML-RPC service on database */
      private static String XINDICE_SERVICE_LOCATION = "/RPC2";
      
      /* 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, 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, "Coudln't 
create "
                         + serviceClassesMap[i] + " service: " + 
e.getMessage());
              } catch (ClassNotFoundException e) {
                  
                  throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "Coudln't 
create "
                         + serviceClassesMap[i] + " service: " + 
e.getMessage());
              } catch (IllegalAccessException e) {
                  
                  throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "Coudln'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
       * @exception XMLDBException thrown if there is no resource named
       *            <code>resourceName</code> or if some other error condition
       *            occurs.
       */
      public Resource getResource(String resourceName) throws XMLDBException {
  
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              params.put(RPCDefaultMessage.NAME, resourceName);
              
              String xml = (String) runRemoteCommand("GetDocument", params);
              
              return new XMLResourceImpl(resourceName, xml, this);
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.NO_SUCH_RESOURCE, 
e.getMessage());
          }
      }
      
      /**
       * 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 ((Long) 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
       * @exception XMLDBException thrown if no mactching service
       *            could be located.
       */
      public Service getService(String name, String version) throws 
XMLDBException {
          
          Service s = (Service) servicesMap.get(name + version);
          if (s == null) {
              
              throw new XMLDBException(ErrorCodes.NO_SUCH_SERVICE);
          }
          
          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 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.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");
          }
          
          if (name == null) {
              
              name = "";
          }
          
          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 (collPath == "/") {
              
              throw new XMLDBException(ErrorCodes.NO_SUCH_COLLECTION,
                      "root collection has no parent");
          }
          
          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);
              return ((Long) runRemoteCommand("GetCollectionsCount", 
params)).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());
          }   
      }
      
  }
  
  
  
  1.1                  
xml-xindice/java/scratchpad/src/org/apache/xindice/client/rpc/CommonConfigurable.java
  
  Index: CommonConfigurable.java
  ===================================================================
  package org.apache.xindice.client.rpc;
  
  /*
   * 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 [EMAIL PROTECTED]
   *
   * 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: CommonConfigurable.java,v 1.1 2002/04/30 13:10:14 jbates Exp $
   */
  
  import java.util.*;
  import org.xmldb.api.base.*;
  
  /**
   * Base class to handle property managment within the API implementation.
   */
  public abstract class CommonConfigurable implements Configurable {
     Hashtable config = null;
  
     /**
      * Constructor for the CommonConfigurable object
      */
     public CommonConfigurable() {
        config = new Hashtable();
     }
  
     /**
      * Sets a property value. If the property doesn't exist it is created, if 
it
      * does exist it is overwritten.
      *
      * @param name The Property name
      * @param value The new Property value
      * @exception XMLDBException
      */
     public void setProperty(String name, String value) throws XMLDBException {
        config.put(name, value);
     }
  
     /**
      * Gets the Property associated with name
      *
      * @param name The name of the property to retrieve.
      * @return The Property value
      * @exception XMLDBException
      */
     public String getProperty(String name) throws XMLDBException {
        return (String) config.get(name);
     }
  }
  
  
  
  
  1.1                  
xml-xindice/java/scratchpad/src/org/apache/xindice/client/rpc/DatabaseImpl.java
  
  Index: DatabaseImpl.java
  ===================================================================
  /*
   * 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 [EMAIL PROTECTED]
   *
   * 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/04/30 13:10:14 jbates Exp $
   */
  
  package org.apache.xindice.client.rpc;
  
  import org.xmldb.api.base.Database;
  import org.xmldb.api.base.Collection;
  import org.xmldb.api.base.XMLDBException;
  import org.xmldb.api.base.ErrorCodes;
  
  /**
   * 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 <[EMAIL PROTECTED]>
   * @version 1
   */
  public class DatabaseImpl extends CommonConfigurable implements Database {
      
      /* prefix used to denote XML:DB URI's that should use this driver */
      private 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 HTTP port */
          if (hostPort.equals("")) {
              
              hostPort = "127.0.0.1";
          }
          
          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/scratchpad/src/org/apache/xindice/client/rpc/XMLResourceImpl.java
  
  Index: XMLResourceImpl.java
  ===================================================================
  /*
   * 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 [EMAIL PROTECTED]
   *
   * 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: XMLResourceImpl.java,v 1.1 2002/04/30 13:10:14 jbates Exp $
   */
  
  package org.apache.xindice.client.rpc;
  
  import org.xmldb.api.base.Collection;
  import org.xmldb.api.base.XMLDBException;
  import org.xmldb.api.base.ErrorCodes;
  import org.xmldb.api.base.Service;
  import org.apache.xmlrpc.XmlRpc;
  import org.apache.xmlrpc.XmlRpcClient;
  import org.xmldb.api.base.Resource;
  import org.xmldb.api.modules.XMLResource;
  import org.apache.xindice.server.rpc.RPCDefaultMessage;
  import java.util.Hashtable;
  import java.util.Vector;
  import java.net.MalformedURLException;
  import org.w3c.dom.Node;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.DocumentBuilder;
  import org.xml.sax.InputSource;
  import org.xml.sax.ContentHandler;
  import java.io.StringReader;
  import javax.xml.parsers.SAXParserFactory;
  import javax.xml.parsers.SAXParser;
  import org.xml.sax.helpers.DefaultHandler;
  import org.xml.sax.XMLReader;
  
  /**
   * Implementation of XML:DB's <code>XMLResource</code> that uses XML-RPC
   * to dialogue with the server
   *
   * @author James Bates <[EMAIL PROTECTED]>
   * @version 1
   */
  public class XMLResourceImpl extends CommonConfigurable implements 
XMLResource {
  
      /* XML data for this resource */
      private String xml;
      
      /* name for this resource */
      private String name;
      
      /* collection this resource belongs to */
      private CollectionImpl coll;
      
      /**
       * Creates new XMLResourceImpl
       *
       * @param name for resource. May be empty, in which case a name can later 
be
       *        assigned using setId()
       * @param content XML data for resource. May be <code>null</code> in 
which 
       *        case content can be set later with <code>setContent()</code>
       */
      XMLResourceImpl(String name, String content, CollectionImpl coll) {
      
          this.name = name;
          this.xml = content;
          this.coll = coll;
      }
  
      /**
       * Creates new XMLResourceImpl with <code>null</code> content
       *
       * @param name for resource. May be empty, in which case a name can later 
be
       *        assigned using setId()
       * @param content XML data for resource. May be <code>null</code> in 
which 
       *        case content can be set later with <code>setContent()</code>
       */
      XMLResourceImpl(String name, CollectionImpl coll) {
      
          this(name, null, coll);
      }
  
      public void setContentAsDOM(Node node)  {
          
          /* TODO: write DOM serializer */
      }
      
      public String getResourceType() {
          
          return "XMLResource";
      }
      
      public ContentHandler setContentAsSAX() {
          
          /* TODO: return string serializer that sets content in
           * <code>endDocument</code>
           */
          return null;
      }
      
      public Object getContent() {
          
          return xml;
      }
      
      public Node getContentAsDOM() throws XMLDBException {
          
          try {
              DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
              factory.setNamespaceAware(true);
              DocumentBuilder db = factory.newDocumentBuilder();
  
              return db.parse(new InputSource(new StringReader(xml)));
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, 
e.getMessage());
          }
      }
      
      public void getContentAsSAX(ContentHandler contentHandler) throws 
XMLDBException {
          
          try {
              
              SAXParserFactory factory = SAXParserFactory.newInstance();
              factory.setNamespaceAware(true);
              XMLReader rd = factory.newSAXParser().getXMLReader();
              rd.setContentHandler(contentHandler);
              rd.parse(new InputSource(new StringReader(xml)));
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, 
e.getMessage());
          }
      }
      
      public String getDocumentId() {
          
          return name;
      }
      
      public String getId() {
          
          return name;
      }
      
      public void setContent(Object xml) throws XMLDBException {
          
          if (!(xml instanceof String)) {
              
              throw new XMLDBException(ErrorCodes.WRONG_CONTENT_TYPE,
                  "XMLResource only accept String content");
          }
          
          try {
              
              SAXParserFactory factory = SAXParserFactory.newInstance();
              factory.setNamespaceAware(true);
              XMLReader rd = factory.newSAXParser().getXMLReader();
              rd.setContentHandler(new DefaultHandler());
              rd.parse(new InputSource(new StringReader((String) xml)));
  
              /* No problems => ok set content */
              this.xml = (String) xml;
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, 
e.getMessage());
          }
      }
      
      public Collection getParentCollection() {
          
          return coll;
      }
      
      void setId(String name) {
          
          this.name = name;
      }
  }
  
  
  
  1.4       +3 -1      
xml-xindice/java/scratchpad/src/org/apache/xindice/server/rpc/RPCMessageHandler.java
  
  Index: RPCMessageHandler.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/scratchpad/src/org/apache/xindice/server/rpc/RPCMessageHandler.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- RPCMessageHandler.java    2 Apr 2002 22:57:05 -0000       1.3
  +++ RPCMessageHandler.java    30 Apr 2002 13:10:14 -0000      1.4
  @@ -56,7 +56,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * $Id: RPCMessageHandler.java,v 1.3 2002/04/02 22:57:05 kstaken Exp $
  + * $Id: RPCMessageHandler.java,v 1.4 2002/04/30 13:10:14 jbates Exp $
    */
   
   /**
  @@ -96,11 +96,13 @@
       }
   
       public void setKernel(Kernel kernel) {
  +        
           super.setKernel(kernel);
           databases = Database.listDatabases();
       }
   
       public void setConfig(Configuration config) {
  +
           this.config = config;
           String name = config.getAttribute(NAME);      
       }
  
  
  
  1.1                  
xml-xindice/java/scratchpad/src/org/apache/xindice/server/rpc/messages/GetCollectionCount.java
  
  Index: GetCollectionCount.java
  ===================================================================
  package org.apache.xindice.server.rpc.messages;
  
  /*
   * 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 [EMAIL PROTECTED]
   *
   * 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: GetCollectionCount.java,v 1.1 2002/04/30 13:10:14 jbates Exp $
   */
  
  import java.util.Hashtable;
  import java.io.*;
  
  import org.w3c.dom.*;
  
  import org.apache.xindice.core.*;
  import org.apache.xindice.xml.*;
  import org.apache.xindice.server.rpc.*;
  
  /**
   * XML-RPC message to list number of child collections in a collection
   *
   * @author James Bates <[EMAIL PROTECTED]>
   * @version 1
   */
  public class GetCollectionCount extends RPCDefaultMessage {   
     
     public Hashtable execute(Hashtable message) throws Exception {
        Collection col = getCollection( (String) message.get(COLLECTION) );
  
        Hashtable result = new Hashtable();
        result.put(RESULT, new Long(col.countCollections()));
        return result;
     }
  }
  
  
  
  1.2       +11 -2     
xml-xindice/java/src/org/apache/xindice/core/CollectionManager.java
  
  Index: CollectionManager.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/CollectionManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CollectionManager.java    6 Dec 2001 21:00:11 -0000       1.1
  +++ CollectionManager.java    30 Apr 2002 13:10:14 -0000      1.2
  @@ -56,7 +56,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * $Id: CollectionManager.java,v 1.1 2001/12/06 21:00:11 bradford Exp $
  + * $Id: CollectionManager.java,v 1.2 2002/04/30 13:10:14 jbates Exp $
    */
   
   import org.apache.xindice.core.objects.*;
  @@ -131,7 +131,16 @@
       * @return The Collection list
       */
      public final String[] listCollections() throws DBException {
  -      return (String[])collections.keySet().toArray(EmptyStrings);
  +      return (String[]) collections.keySet().toArray(EmptyStrings);
  +   }
  +
  +   /**
  +    * Returns number of child collections
  +    *
  +    * @return number of collections
  +    */
  +   public final long countCollections() throws DBException {
  +      return (long) collections.size();
      }
   
      /**
  
  
  

Reply via email to