You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2003/04/30 03:50:03 UTC

cvs commit: cocoon-2.1/src/blocks/xmldb/java/org/apache/cocoon/components/source/impl XMLDBSource.java XMLDBSourceFactory.java

vgritsenko    2003/04/29 18:50:02

  Modified:    .        changes.xml
               .        status.xml
               src/java/org/apache/cocoon/components/source
                        XMLDBSource.java XMLDBSourceFactory.java
               src/blocks/xmldb/java/org/apache/cocoon/components/source/impl
                        XMLDBSource.java XMLDBSourceFactory.java
  Added:       src/java/org/apache/cocoon/components/source/helpers
                        SourceCredential.java
  Log:
  Cleanup XMLDB source implementation. Guess it was never working in multithreaded/multi-DB setup.
  Port optimizations into the 2.1.
  Port user/pwd config into the 2.0.
  
  Revision  Changes    Path
  1.4       +6 -2      cocoon-2.0/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.0/changes.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- changes.xml	27 Apr 2003 18:06:32 -0000	1.3
  +++ changes.xml	30 Apr 2003 01:50:01 -0000	1.4
  @@ -41,6 +41,10 @@
    </devs>
   
    <release version="@version@" date="@date@">
  +  <action dev="VG" type="update">
  +    Ported from 2.1 branch: Updated the XML:DB Source. Now it will accept, in cocoon.xconf,
  +    optional authentication attributes, called "user" and "password".
  +  </action>
     <action dev="VG" type="fix" fixes-bug="10242">
       TextSerializer now can handle absence of root element in the incoming XML.
     </action>
  @@ -145,7 +149,7 @@
       SimpleFormTransfomer: fixed error tags when no validation data available.
     </action>
    </release>
  -<release version="2.0.4" date="December 06 2002">
  + <release version="2.0.4" date="December 06 2002">
     <action dev="SW" type="update">
      The HTMLGenerator now accepts a JTidy configuration file for fine-grained
      control on the generated document.
  
  
  
  1.9       +4 -5      cocoon-2.1/status.xml
  
  Index: status.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/status.xml,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- status.xml	29 Apr 2003 08:41:08 -0000	1.8
  +++ status.xml	30 Apr 2003 01:50:01 -0000	1.9
  @@ -204,18 +204,17 @@
       </action>
   
      </actions>
  -  
     </todo>
   
     <changes>
   
    <release version="@version@" date="@date@">
  -   <action dev="CZ">
  -     PLACEHOLDER
  -   </action>
  +  <action dev="VG" type="fix" fixes-bug="18683">
  +    Sync XML:DB source with 2.0 branch: Conect to the XML:DB once, from the
  +    XMLDBSourceFactory.
  +  </action>
    </release>
    <release version="2.1-M1" date="April 29 2003">
  - 
     <action dev="VG" type="fix" fixes-bug="10242">
       TextSerializer now can handle absence of root element in the incoming XML.
     </action>
  
  
  
  1.2       +42 -35    cocoon-2.0/src/java/org/apache/cocoon/components/source/XMLDBSource.java
  
  Index: XMLDBSource.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.0/src/java/org/apache/cocoon/components/source/XMLDBSource.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLDBSource.java	9 Mar 2003 00:02:32 -0000	1.1
  +++ XMLDBSource.java	30 Apr 2003 01:50:02 -0000	1.2
  @@ -53,6 +53,7 @@
   import org.apache.avalon.framework.component.ComponentManager;
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.ResourceNotFoundException;
  +import org.apache.cocoon.components.source.helpers.SourceCredential;
   import org.apache.cocoon.environment.Environment;
   import org.apache.cocoon.xml.IncludeXMLConsumer;
   import org.apache.log.Logger;
  @@ -80,8 +81,14 @@
       /** The requested URL */
       protected String url;
   
  +    /** The supplied user */
  +    protected String user;
  +
  +    /** The supplied password */
  +    protected String password;
  +
       /** The part of URL after # sign */
  -    protected String query = null;
  +    protected String query;
   
       /** The System ID */
       protected String systemId;
  @@ -122,22 +129,29 @@
   
       protected static final String CDATA  = "CDATA";
   
  +
       /**
        * The constructor.
        *
  -     * @param environment the Cocoon Environment.
  +     * @param logger the Logger instance.
  +     * @param credential username and password
        * @param url the URL being queried.
  -     * @param driver the XML:DB driver class name.
  +     * @param manager component manager
  +     * @param environment the Cocoon Environment.
        */
  -    public XMLDBSource(Environment environment,
  +    public XMLDBSource(Logger logger,
  +                       SourceCredential credential,
  +                       String url,
                          ComponentManager manager,
  -                       Logger logger,
  -                       String url) {
  +                       Environment environment) {
   
           super(environment, manager, logger);
  -        int start;
   
  -        if ((start = url.indexOf('#')) != -1) {
  +        this.user = credential.getPrincipal();
  +        this.password = credential.getPassword();
  +
  +        int start = url.indexOf('#');
  +        if (start != -1) {
               this.url = url.substring(0, start);
               this.query = url.substring(start + 1);
           } else {
  @@ -149,24 +163,22 @@
        * Stream SAX events to a given ContentHandler. If the requested
        * resource is a collection, build an XML view of it.
        */
  -    public void toSAX(ContentHandler handler)
  -        throws SAXException, ProcessingException
  -    {
  -        if (url.endsWith("/"))
  +    public void toSAX(ContentHandler handler) throws SAXException, ProcessingException {
  +        if (url.endsWith("/")) {
               this.collectionToSAX(handler);
  -        else
  +        } else {
               this.resourceToSAX(handler);
  +        }
       }
   
  -    private void resourceToSAX(ContentHandler handler)
  -            throws SAXException, ProcessingException
  -    {
  +    private void resourceToSAX(ContentHandler handler) throws SAXException, ProcessingException {
  +
           final String col = url.substring(0, url.lastIndexOf('/'));
           final String res = url.substring(url.lastIndexOf('/') + 1);
   
           Collection collection = null;
           try {
  -            collection = DatabaseManager.getCollection(col);
  +            collection = DatabaseManager.getCollection(col, user, password);
               if (collection == null) {
                   throw new ResourceNotFoundException("Document " + url + " not found");
               }
  @@ -193,8 +205,7 @@
               }
           } catch (XMLDBException xde) {
               String error = "Unable to fetch content. Error "
  -                     + xde.errorCode + ": " + xde.getMessage();
  -            this.log.debug(error, xde);
  +                           + xde.errorCode + ": " + xde.getMessage();
               throw new SAXException(error, xde);
           } finally {
               if (collection != null) {
  @@ -206,14 +217,13 @@
           }
       }
   
  -    private void collectionToSAX(ContentHandler handler)
  -            throws SAXException, ProcessingException
  -    {
  +    private void collectionToSAX(ContentHandler handler) throws SAXException, ProcessingException {
  +
           AttributesImpl attributes = new AttributesImpl();
   
           Collection collection = null;
           try {
  -            collection = DatabaseManager.getCollection(url);
  +            collection = DatabaseManager.getCollection(url, user, password);
               if (collection == null) {
                   throw new ResourceNotFoundException("Collection " + url +
                           " not found");
  @@ -271,7 +281,6 @@
               }
           } catch (XMLDBException xde) {
               String error = "Collection listing failed. Error " + xde.errorCode + ": " + xde.getMessage();
  -            this.log.debug(error, xde);
               throw new SAXException(error, xde);
           } finally {
               if (collection != null) {
  @@ -283,9 +292,8 @@
           }
       }
   
  -    private void queryToSAX(ContentHandler handler, Collection collection, String resource)
  -            throws SAXException
  -    {
  +    private void queryToSAX(ContentHandler handler, Collection collection, String resource) throws SAXException {
  +
           AttributesImpl attributes = new AttributesImpl();
   
           try {
  @@ -333,21 +341,20 @@
               handler.endDocument();
           } catch (XMLDBException xde) {
               String error = "Query failed. Error " + xde.errorCode + ": " + xde.getMessage();
  -            this.log.debug(error, xde);
               throw new SAXException(error, xde);
           }
       }
   
  -    public void recycle()
  -    {
  -        this.log = null;
  -        this.manager = null;
  +    public void recycle() {
  +        super.log = null;
  +        super.manager = null;
           this.url = null;
  +        this.user = null;
  +        this.password = null;
           this.query = null;
       }
   
  -    public String getSystemId()
  -    {
  +    public String getSystemId() {
           return url;
       }
   }
  
  
  
  1.2       +45 -58    cocoon-2.0/src/java/org/apache/cocoon/components/source/XMLDBSourceFactory.java
  
  Index: XMLDBSourceFactory.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.0/src/java/org/apache/cocoon/components/source/XMLDBSourceFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLDBSourceFactory.java	9 Mar 2003 00:02:32 -0000	1.1
  +++ XMLDBSourceFactory.java	30 Apr 2003 01:50:02 -0000	1.2
  @@ -62,11 +62,12 @@
   import org.apache.avalon.framework.configuration.ConfigurationException;
   import org.apache.avalon.framework.logger.AbstractLoggable;
   import org.apache.cocoon.ProcessingException;
  +import org.apache.cocoon.components.source.helpers.SourceCredential;
   import org.apache.cocoon.environment.Environment;
   import org.apache.cocoon.environment.Source;
  -import org.xmldb.api.DatabaseManager;
   import org.xmldb.api.base.Database;
   import org.xmldb.api.base.XMLDBException;
  +import org.xmldb.api.DatabaseManager;
   
   /**
    * This class implements the xmldb:// pseudo-protocol and allows to get XML
  @@ -75,63 +76,58 @@
    * @author <a href="mailto:gianugo@rabellino.it">Gianugo Rabellino</a>
    * @version CVS $Id$
    */
  -
   public final class XMLDBSourceFactory
  -  extends AbstractLoggable
  -  implements SourceFactory, Configurable, Composable {
  -
  -
  -    /** The driver implementation class */
  -    protected String driver;
  +        extends AbstractLoggable
  +        implements SourceFactory, Configurable, Composable {
   
  -    /** The driver implementation class */
  +    /** The Component Manager class */
       protected ComponentManager m_manager;
   
  -    /** A Map containing the driver list */
  -    protected HashMap driverMap;
  +    /** A Map containing the authentication credentials */
  +    protected HashMap credentialMap;
   
       /**
  -     * Configure the instance.
  +     * Configure the instance and initialize XML:DB connections (load and register the drivers).
        */
       public void configure(final Configuration conf)
               throws ConfigurationException {
   
  -        if (conf != null) {
  -            driverMap = new HashMap();
  +        credentialMap = new HashMap();
   
  -            Configuration[] xmldbConfigs = conf.getChildren("driver");
  +        Configuration[] xmldbConfigs = conf.getChildren("driver");
   
  -            for (int i = 0; i < xmldbConfigs.length; i++) {
  -                String type = xmldbConfigs[i].getAttribute("type");
  -                String driver = xmldbConfigs[i].getAttribute("class");
  -                driverMap.put(type, driver);
  +        for (int i = 0; i < xmldbConfigs.length; i++) {
  +            String type = xmldbConfigs[i].getAttribute("type");
  +            String driver = xmldbConfigs[i].getAttribute("class");
   
  -                if (getLogger().isDebugEnabled()) {
  -                    getLogger().debug("Initializing XML:DB connection, using driver " + driver);
  -                }
  +            SourceCredential credential = new SourceCredential(null, null);
  +            credential.setPrincipal(xmldbConfigs[i].getAttribute("user", null));
  +            credential.setPassword(xmldbConfigs[i].getAttribute("password", null));
  +            credentialMap.put(type, credential);
   
  -                try {
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("Initializing XML:DB connection, using driver " + driver);
  +            }
  +
  +            try {
   
  -                    Class c = Class.forName(driver);
  -                    DatabaseManager.registerDatabase((Database)c.newInstance());
  +                Class c = Class.forName(driver);
  +                DatabaseManager.registerDatabase((Database)c.newInstance());
   
  -                } catch (XMLDBException xde) {
  +            } catch (XMLDBException xde) {
   
  -                    String error = "Unable to connect to the XMLDB database. Error "
  -                                   + xde.errorCode + ": " + xde.getMessage();
  -                    getLogger().debug(error, xde);
  -                    throw new ConfigurationException(error, xde);
  +                String error = "Unable to connect to the XMLDB database. Error "
  +                               + xde.errorCode + ": " + xde.getMessage();
  +                getLogger().debug(error, xde);
  +                throw new ConfigurationException(error, xde);
   
  -                } catch (Exception e) {
  +            } catch (Exception e) {
   
  -                    getLogger().error("There was a problem setting up the connection");
  -                    getLogger().error("Make sure that your driver is available");
  -                    throw new ConfigurationException("Problem setting up the connection to XML:DB: "
  -                                                     + e.getMessage(), e);
  -                }
  +                getLogger().warn("There was a problem setting up the connection. "
  +                                 + "Make sure that your driver is available");
  +                throw new ConfigurationException("Problem setting up the connection to XML:DB: "
  +                                                 + e.getMessage(), e);
               }
  -        } else {
  -            throw new ConfigurationException("XMLDB configuration not found");
           }
       }
   
  @@ -147,38 +143,29 @@
        * Resolve the source
        */
       public Source getSource(Environment environment, String location)
  -      throws ProcessingException, IOException, MalformedURLException {
  +            throws ProcessingException, MalformedURLException, IOException {
   
  -      int start = location.indexOf(':') + 1;
  -      int end = location.indexOf(':', start);
  +        int start = location.indexOf(':') + 1;
  +        int end = location.indexOf(':', start);
   
           if (start == -1 || end == -1) {
  -            if (this.getLogger().isWarnEnabled()) {
  -                this.getLogger().warn("Mispelled XML:DB URL. " +
  -                    "The syntax is \"xmldb:databasetype://host/collection/resource\"");
  -                throw new MalformedURLException("Mispelled XML:DB URL. " +
  -                    "The syntax is \"xmldb:databasetype://host/collection/resource\"");
  -            }
  +            throw new MalformedURLException("Mispelled XML:DB URL. " +
  +                                            "The syntax is \"xmldb:databasetype://host/collection/resource\"");
           }
   
           String type = location.substring(start, end);
  -        driver = (String)driverMap.get(type);
  -
  -        if (driver == null) {
  -            this.getLogger().error("Unable to find a driver for the \"" +
  -                type + " \" database type, please check the configuration");
  -            throw new ProcessingException("Unable to find a driver for the \"" +
  -                type + " \" database type, please check the configuration");
  -        }
  +        SourceCredential credential = (SourceCredential)credentialMap.get(type);
   
  -        return new XMLDBSource(environment, m_manager, this.getLogger(), location);
  +        return new XMLDBSource(this.getLogger(),
  +                               credential, location,
  +                               this.m_manager, environment);
       }
   
       /**
        * Resolve the source
        */
       public Source getSource(Environment environment, URL base, String location)
  -        throws ProcessingException, IOException, MalformedURLException {
  +            throws ProcessingException, IOException, MalformedURLException {
           return getSource(environment, base.toExternalForm() + location);
       }
   }
  
  
  
  1.1                  cocoon-2.0/src/java/org/apache/cocoon/components/source/helpers/SourceCredential.java
  
  Index: SourceCredential.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 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 "Apache Cocoon" 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.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.cocoon.components.source.helpers;
  
  /**
   * This class represents a credential for a given user
   *
   * @author <a href="mailto:stephan@vern.chem.tu-berlin.de">Stephan Michels</a>
   * @version CVS $Id: SourceCredential.java,v 1.1 2003/04/30 01:50:02 vgritsenko Exp $
   */
  public class SourceCredential {
  
      private String principal = "guest";
      private String password = "guest";
  
      /**
       * Create a new credential
       *
       * @param principal The user name
       */
      public SourceCredential(String principal) {
          this.principal = principal;
      }
  
      /**
       * Create a new credential
       *
       * @param principal The user name
       * @param password Password
       */
      public SourceCredential(String principal, String password) {
          this.principal = principal;
          this.password  = password;
      }
  
      /**
       * Sets the principal
       *
       * @param principal The user name
       */
      public void setPrincipal(String principal) {
          this.principal = principal;
      }
  
      /**
       * Returns the principal
       * 
       * @return Principal
       */
      public String getPrincipal() {
          return this.principal;
      }
  
      /**
       * Sets the password
       *
       * @param password Password
       */
      public void setPassword(String password) {
          this.password = password;
      }
  
      /**
       * Returns the password
       * 
       * @return Password
       */
      public String getPassword() {
          return this.password;
      }
  }
  
  
  
  1.2       +47 -82    cocoon-2.1/src/blocks/xmldb/java/org/apache/cocoon/components/source/impl/XMLDBSource.java
  
  Index: XMLDBSource.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/xmldb/java/org/apache/cocoon/components/source/impl/XMLDBSource.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLDBSource.java	12 Mar 2003 09:35:39 -0000	1.1
  +++ XMLDBSource.java	30 Apr 2003 01:50:02 -0000	1.2
  @@ -74,7 +74,6 @@
   import org.xml.sax.helpers.AttributesImpl;
   import org.xmldb.api.DatabaseManager;
   import org.xmldb.api.base.Collection;
  -import org.xmldb.api.base.Database;
   import org.xmldb.api.base.ResourceIterator;
   import org.xmldb.api.base.ResourceSet;
   import org.xmldb.api.base.XMLDBException;
  @@ -92,30 +91,21 @@
   public class XMLDBSource extends AbstractLogEnabled  
       implements Source, XMLizable {
   
  -    /** The driver implementation class */
  -    protected String driver;
  -
  -    /** The connection status. */
  -    protected boolean connected = false;
  -
       /** The requested URL */
       protected String url;
   
       /** The supplied user */
  -    protected String user = null;
  +    protected String user;
   
       /** The supplied password */
       protected String password;
   
       /** The part of URL after # sign */
  -    protected String query = null;
  +    protected String query;
   
       /** The System ID */
       protected String systemId;
   
  -    /** The scheme */
  -    protected String scheme;
  -    
       /** ComponentManager */
       protected ComponentManager manager;
       
  @@ -155,29 +145,27 @@
   
       protected static final String CDATA  = "CDATA";
   
  +
       /**
        * The constructor.
        *
  -     * @param environment the Cocoon Environment.
  +     * @param logger the Logger instance.
  +     * @param credential username and password
        * @param url the URL being queried.
  -     * @param driver the XML:DB driver class name.
  +     * @param manager component manager
        */
       public XMLDBSource(Logger logger,
  -                       String driver,
                          SourceCredential credential,
                          String url,
  -                       String scheme,
                          ComponentManager manager) {
           this.enableLogging(logger);
           this.manager = manager;
  -        int start;
   
  -        this.driver = driver;
  -        this.scheme = scheme;
           this.user = credential.getPrincipal();
           this.password = credential.getPassword();
   
  -        if ((start = url.indexOf('#')) != -1) {
  +        int start = url.indexOf('#');
  +        if (start != -1) {
               this.url = url.substring(0, start);
               this.query = url.substring(start + 1);
           } else {
  @@ -186,58 +174,20 @@
       }
   
       /**
  -     * Initialize the XML:DB connection.
  -     *
  -     */
  -    public void connect()
  -    throws ProcessingException {
  -
  -        if (this.getLogger().isDebugEnabled()) {
  -            this.getLogger().debug("Initializing XML:DB connection, using driver " + driver);
  -        }
  -
  -        try {
  -
  -            Class c = Class.forName(driver);
  -            DatabaseManager.registerDatabase((Database)c.newInstance());
  -
  -        } catch (XMLDBException xde) {
  -
  -            String error = "Unable to connect to the XMLDB database. Error "
  -                    + xde.errorCode + ": " + xde.getMessage();
  -            this.getLogger().debug(error, xde);
  -            throw new ProcessingException(error, xde);
  -
  -        } catch (Exception e) {
  -
  -            throw new ProcessingException("Problem setting up the connection to XML:DB: "
  -                    + e.getMessage() + ". Make sure that your driver is available.", e);
  -
  -        }
  -
  -        this.connected = true;
  -    }
  -
  -
  -    /**
        * Stream SAX events to a given ContentHandler. If the requested
        * resource is a collection, build an XML view of it.
  -     *
        */
       public void toSAX(ContentHandler handler) throws SAXException {
   
           try {
  -            if (!connected) {
  -                this.connect();
  -            }
  -            if (url.endsWith("/"))
  +            if (url.endsWith("/")) {
                   this.collectionToSAX(handler);
  -            else
  +            } else {
                   this.resourceToSAX(handler);
  +            }
           } catch (ProcessingException pe) {
               throw new SAXException("ProcessingException", pe);
           }
  -
       }
   
       private void resourceToSAX(ContentHandler handler) throws SAXException, ProcessingException {
  @@ -245,8 +195,9 @@
           final String col = url.substring(0, url.lastIndexOf('/'));
           final String res = url.substring(url.lastIndexOf('/') + 1);
   
  +        Collection collection = null;
           try {
  -            Collection collection = DatabaseManager.getCollection(col, user, password);
  +            collection = DatabaseManager.getCollection(col, user, password);
               if (collection == null) {
                   throw new ResourceNotFoundException("Document " + url + " not found");
               }
  @@ -258,25 +209,30 @@
   
               if (query != null) {
                   // Query resource
  -                if (this.getLogger().isDebugEnabled()) {
  -                    this.getLogger().debug("Querying resource " + res + " from collection " + url + "; query= " + this.query);
  +                if (getLogger().isDebugEnabled()) {
  +                    getLogger().debug("Querying resource " + res + " from collection " + url + "; query= " + this.query);
                   }
   
                   queryToSAX(handler, collection, res);
               } else {
                   // Return entire resource
  -                if (this.getLogger().isDebugEnabled()) {
  -                    this.getLogger().debug("Obtaining resource " + res + " from collection " + col);
  +                if (getLogger().isDebugEnabled()) {
  +                    getLogger().debug("Obtaining resource " + res + " from collection " + col);
                   }
   
                   xmlResource.getContentAsSAX(handler);
               }
  -
  -            collection.close();
           } catch (XMLDBException xde) {
               String error = "Unable to fetch content. Error "
  -                     + xde.errorCode + ": " + xde.getMessage();
  +                           + xde.errorCode + ": " + xde.getMessage();
               throw new SAXException(error, xde);
  +        } finally {
  +            if (collection != null) {
  +                try {
  +                    collection.close();
  +                } catch (XMLDBException ignored) {
  +                }
  +            }
           }
       }
   
  @@ -284,8 +240,9 @@
   
           AttributesImpl attributes = new AttributesImpl();
   
  +        Collection collection = null;
           try {
  -            Collection collection = DatabaseManager.getCollection(url, user, password);
  +            collection = DatabaseManager.getCollection(url, user, password);
               if (collection == null) {
                   throw new ResourceNotFoundException("Collection " + url +
                           " not found");
  @@ -293,15 +250,15 @@
   
               if (query != null) {
                   // Query collection
  -                if (this.getLogger().isDebugEnabled()) {
  -                    this.getLogger().debug("Querying collection " + url + "; query= " + this.query);
  +                if (getLogger().isDebugEnabled()) {
  +                    getLogger().debug("Querying collection " + url + "; query= " + this.query);
                   }
   
                   queryToSAX(handler, collection, null);
               } else {
                   // List collection
  -                if (this.getLogger().isDebugEnabled()) {
  -                    this.getLogger().debug("Listing collection " + url);
  +                if (getLogger().isDebugEnabled()) {
  +                    getLogger().debug("Listing collection " + url);
                   }
   
                   final String ncollections = Integer.toString(collection.getChildCollectionCount());
  @@ -341,11 +298,16 @@
                   handler.endPrefixMapping(PREFIX);
                   handler.endDocument();
               }
  -
  -            collection.close();
           } catch (XMLDBException xde) {
               String error = "Collection listing failed. Error " + xde.errorCode + ": " + xde.getMessage();
               throw new SAXException(error, xde);
  +        } finally {
  +            if (collection != null) {
  +                try {
  +                    collection.close();
  +                } catch (XMLDBException ignored) {
  +                }
  +            }
           }
       }
   
  @@ -403,8 +365,9 @@
       }
   
       public void recycle() {
  -        this.driver = null;
           this.url = null;
  +        this.user = null;
  +        this.password = null;
           this.query = null;
       }
   
  @@ -465,10 +428,12 @@
           } catch (Exception e) {
               throw new CascadingIOException("Exception during processing of " + this.getURI(), e);
           } finally {
  -            if (serializer != null) serializerSelector.release(serializer);
  -            if (serializerSelector != null) this.manager.release(serializerSelector);
  +            if (serializer != null) {
  +                serializerSelector.release(serializer);
  +            }
  +            if (serializerSelector != null) {
  +                this.manager.release(serializerSelector);
  +            }
           }
       }
  -
  -
   }
  
  
  
  1.2       +40 -30    cocoon-2.1/src/blocks/xmldb/java/org/apache/cocoon/components/source/impl/XMLDBSourceFactory.java
  
  Index: XMLDBSourceFactory.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/xmldb/java/org/apache/cocoon/components/source/impl/XMLDBSourceFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLDBSourceFactory.java	12 Mar 2003 09:35:38 -0000	1.1
  +++ XMLDBSourceFactory.java	30 Apr 2003 01:50:02 -0000	1.2
  @@ -64,6 +64,9 @@
   import org.apache.cocoon.components.source.helpers.SourceCredential;
   import org.apache.excalibur.source.Source;
   import org.apache.excalibur.source.SourceFactory;
  +import org.xmldb.api.base.Database;
  +import org.xmldb.api.base.XMLDBException;
  +import org.xmldb.api.DatabaseManager;
   
   /**
    * This class implements the xmldb:// pseudo-protocol and allows to get XML
  @@ -72,46 +75,58 @@
    * @author <a href="mailto:gianugo@rabellino.it">Gianugo Rabellino</a>
    * @version CVS $Id$
    */
  -
   public final class XMLDBSourceFactory
           extends AbstractLogEnabled
           implements SourceFactory, Configurable, Composable {
   
  -    /** The driver implementation class */
  -    protected String driver;
  -
  -    /** The authentication info */
  -    protected SourceCredential credential;
  -
       /** The Component Manager class */
       protected ComponentManager m_manager;
   
  -    /** A Map containing the driver list */
  -    protected HashMap driverMap;
  -
       /** A Map containing the authentication credentials */
       protected HashMap credentialMap;
   
       /**
  -     * Configure the instance.
  +     * Configure the instance and initialize XML:DB connections (load and register the drivers).
        */
       public void configure(final Configuration conf)
               throws ConfigurationException {
   
  -        driverMap = new HashMap();
           credentialMap = new HashMap();
   
           Configuration[] xmldbConfigs = conf.getChildren("driver");
   
           for (int i = 0; i < xmldbConfigs.length; i++) {
  -            SourceCredential credential = new SourceCredential(null, null);
  -
  -            driverMap.put(xmldbConfigs[i].getAttribute("type"),
  -                          xmldbConfigs[i].getAttribute("class"));
  +            String type = xmldbConfigs[i].getAttribute("type");
  +            String driver = xmldbConfigs[i].getAttribute("class");
   
  +            SourceCredential credential = new SourceCredential(null, null);
               credential.setPrincipal(xmldbConfigs[i].getAttribute("user", null));
               credential.setPassword(xmldbConfigs[i].getAttribute("password", null));
  -            credentialMap.put(xmldbConfigs[i].getAttribute("type"), credential);
  +            credentialMap.put(type, credential);
  +
  +            if (getLogger().isDebugEnabled()) {
  +                getLogger().debug("Initializing XML:DB connection, using driver " + driver);
  +            }
  +
  +            try {
  +
  +                Class c = Class.forName(driver);
  +                DatabaseManager.registerDatabase((Database)c.newInstance());
  +
  +            } catch (XMLDBException xde) {
  +
  +                String error = "Unable to connect to the XMLDB database. Error "
  +                               + xde.errorCode + ": " + xde.getMessage();
  +                getLogger().debug(error, xde);
  +                throw new ConfigurationException(error, xde);
  +
  +            } catch (Exception e) {
  +
  +                getLogger().warn("There was a problem setting up the connection. "
  +                                 + "Make sure that your driver is available");
  +                throw new ConfigurationException("Problem setting up the connection to XML:DB: "
  +                                                 + e.getMessage(), e);
  +            }
           }
       }
   
  @@ -119,13 +134,16 @@
        * Compose this Composable object. We need to pass on the
        * ComponentManager to the actual Source.
        */
  -
       public void compose(ComponentManager cm) {
           this.m_manager = cm;
       }
   
  +    /**
  +     * Resolve the source
  +     */
       public Source getSource(String location, Map parameters)
  -    throws MalformedURLException, IOException {
  +            throws MalformedURLException, IOException {
  +
           int start = location.indexOf(':') + 1;
           int end = location.indexOf(':', start);
   
  @@ -135,17 +153,10 @@
           }
   
           String type = location.substring(start, end);
  -
  -        driver = (String)driverMap.get(type);
  -        credential = (SourceCredential)credentialMap.get(type);
  -
  -        if (driver == null) {
  -            throw new IOException("Unable to find a driver for the \"" +
  -                                          type + " \" database type, please check the configuration");
  -        }
  +        SourceCredential credential = (SourceCredential)credentialMap.get(type);
   
           return new XMLDBSource(this.getLogger(),
  -                               driver, credential, location, location.substring(0, start-1), 
  +                               credential, location,
                                  this.m_manager);
       }
   
  @@ -155,5 +166,4 @@
               ((XMLDBSource)source).recycle();
           }
       }
  -
   }