vgritsenko 2002/08/03 09:44:13 Modified: . Tag: cocoon_2_0_3_branch changes.xml src/java/org/apache/cocoon/components/source Tag: cocoon_2_0_3_branch XMLDBSource.java XMLDBSourceFactory.java Log: optimize XML:DB source Revision Changes Path No revision No revision 1.138.2.42 +4 -1 xml-cocoon2/changes.xml Index: changes.xml =================================================================== RCS file: /home/cvs/xml-cocoon2/changes.xml,v retrieving revision 1.138.2.41 retrieving revision 1.138.2.42 diff -u -r1.138.2.41 -r1.138.2.42 --- changes.xml 3 Aug 2002 15:11:03 -0000 1.138.2.41 +++ changes.xml 3 Aug 2002 16:44:13 -0000 1.138.2.42 @@ -40,6 +40,9 @@ <release version="@version@" date="@date@"> <action dev="VG" type="fix"> + Conect to the XML:DB once, from the XMLDBSourceFactory. + </action> + <action dev="VG" type="fix"> <xsp-util:include-expr/> includes any object, converting it to string. </action> <action dev="VG" type="update"> No revision No revision 1.7.2.1 +35 -69 xml-cocoon2/src/java/org/apache/cocoon/components/source/XMLDBSource.java Index: XMLDBSource.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/source/XMLDBSource.java,v retrieving revision 1.7 retrieving revision 1.7.2.1 diff -u -r1.7 -r1.7.2.1 --- XMLDBSource.java 22 Feb 2002 07:00:13 -0000 1.7 +++ XMLDBSource.java 3 Aug 2002 16:44:13 -0000 1.7.2.1 @@ -63,7 +63,6 @@ import org.xmldb.api.DatabaseManager; import org.xmldb.api.base.Collection; -import org.xmldb.api.base.Database; import org.xmldb.api.base.ErrorCodes; import org.xmldb.api.base.Resource; import org.xmldb.api.base.Service; @@ -95,12 +94,6 @@ */ public class XMLDBSource extends AbstractSAXSource { - /** The driver implementation class */ - protected String driver; - - /** The connection status. */ - protected boolean connected = false; - /** The requested URL */ protected String url; @@ -156,14 +149,11 @@ public XMLDBSource(Environment environment, ComponentManager manager, Logger logger, - String driver, String url) { super(environment, manager, logger); int start; - this.driver = driver; - if ((start = url.indexOf('#')) != -1) { this.url = url.substring(0, start); this.query = url.substring(start + 1); @@ -173,65 +163,27 @@ } /** - * Initialize the XML:DB connection. - * - */ - public void connect() - throws ProcessingException { - - if (log.isDebugEnabled()) { - this.log.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.log.debug(error, xde); - throw new ProcessingException(error, xde); - - } catch (Exception e) { - - this.log.error("There was a problem setting up the connection"); - this.log.error("Make sure that your driver is available"); - throw new ProcessingException("Problem setting up the connection to XML:DB: " - + e.getMessage(), 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, ProcessingException { - - if (!connected) { - this.connect(); - } + public void toSAX(ContentHandler handler) + throws SAXException, ProcessingException + { if (url.endsWith("/")) this.collectionToSAX(handler); 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 collection = DatabaseManager.getCollection(col); + collection = DatabaseManager.getCollection(col); if (collection == null) { throw new ResourceNotFoundException("Document " + url + " not found"); } @@ -256,22 +208,29 @@ xmlResource.getContentAsSAX(handler); } - - collection.close(); } catch (XMLDBException xde) { String error = "Unable to fetch content. Error " + xde.errorCode + ": " + xde.getMessage(); this.log.debug(error, xde); throw new SAXException(error, xde); + } finally { + if (collection != null) { + try { + collection.close(); + } catch (XMLDBException ignored) { + } + } } } - 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 collection = DatabaseManager.getCollection(url); + collection = DatabaseManager.getCollection(url); if (collection == null) { throw new ResourceNotFoundException("Collection " + url + " not found"); @@ -327,17 +286,23 @@ handler.endPrefixMapping(PREFIX); handler.endDocument(); } - - collection.close(); } 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) { + try { + collection.close(); + } catch (XMLDBException ignored) { + } + } } } - 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 { @@ -390,15 +355,16 @@ } } - public void recycle() { - this.driver = null; + public void recycle() + { this.log = null; this.manager = null; this.url = null; this.query = null; } - public String getSystemId() { + public String getSystemId() + { return url; } } 1.4.2.1 +57 -34 xml-cocoon2/src/java/org/apache/cocoon/components/source/XMLDBSourceFactory.java Index: XMLDBSourceFactory.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/source/XMLDBSourceFactory.java,v retrieving revision 1.4 retrieving revision 1.4.2.1 diff -u -r1.4 -r1.4.2.1 --- XMLDBSourceFactory.java 22 Feb 2002 07:00:13 -0000 1.4 +++ XMLDBSourceFactory.java 3 Aug 2002 16:44:13 -0000 1.4.2.1 @@ -61,6 +61,10 @@ import org.apache.cocoon.environment.Source; import org.apache.cocoon.sitemap.Sitemap; +import org.xmldb.api.DatabaseManager; +import org.xmldb.api.base.Database; +import org.xmldb.api.base.XMLDBException; + import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; @@ -94,32 +98,53 @@ * Configure the instance. */ public void configure(final Configuration conf) - throws ConfigurationException { + throws ConfigurationException { - if (conf != null) { + if (conf != null) { + driverMap = new HashMap(); - driverMap = 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++) { - driverMap.put(xmldbConfigs[i].getAttribute("type"), - xmldbConfigs[i].getAttribute("class")); - } + 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); - } else { - throw new ConfigurationException("XMLDB configuration not found"); - } + } 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); + } + } + } else { + throw new ConfigurationException("XMLDB configuration not found"); + } } /** * Compose this Composable object. We need to pass on the * ComponentManager to the actual Source. */ - public void compose(ComponentManager cm) { - this.m_manager = cm; + this.m_manager = cm; } /** @@ -131,35 +156,33 @@ 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\""); + 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\""); + } } - } - - String type = location.substring(start, end); - driver = (String)driverMap.get(type); + 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"); - } + 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"); + } - return new XMLDBSource(environment, m_manager, this.getLogger(), - driver, location); + return new XMLDBSource(environment, m_manager, this.getLogger(), location); } /** * Resolve the source */ public Source getSource(Environment environment, URL base, String location) - throws ProcessingException, IOException, MalformedURLException { - return getSource(environment, base.toExternalForm() + location); + throws ProcessingException, IOException, MalformedURLException { + return getSource(environment, base.toExternalForm() + location); } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]