sanjiva     2002/10/28 08:37:02

  Modified:    java/src/org/apache/soap/util/xml XMLParserUtils.java
  Log:
  added logic to share instances of DocumentBuilders on a per-thread
  basis
  
  Revision  Changes    Path
  1.6       +16 -7     xml-soap/java/src/org/apache/soap/util/xml/XMLParserUtils.java
  
  Index: XMLParserUtils.java
  ===================================================================
  RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/util/xml/XMLParserUtils.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- XMLParserUtils.java       17 May 2002 18:11:57 -0000      1.5
  +++ XMLParserUtils.java       28 Oct 2002 16:37:02 -0000      1.6
  @@ -57,7 +57,7 @@
   
   package org.apache.soap.util.xml;
   
  -// JAXP packages
  +import java.util.HashMap;
   import javax.xml.parsers.*;
   import org.xml.sax.*;
   import org.xml.sax.helpers.*;
  @@ -73,6 +73,7 @@
    */
   public class XMLParserUtils {
     private static DocumentBuilderFactory dbf = null;
  +  private static HashMap docBuilderTable = new HashMap ();
   
     static {
       // Create a default instance.
  @@ -132,12 +133,20 @@
      */
     synchronized public static DocumentBuilder getXMLDocBuilder()
       throws IllegalArgumentException {
  -    // Step 2: create a DocumentBuilder that satisfies the constraints
  -    // specified by the DocumentBuilderFactory
  -    try {
  -      return dbf.newDocumentBuilder();
  -    } catch (ParserConfigurationException pce) {
  -      throw new IllegalArgumentException(pce.toString());
  +    // if a document builder has already been created for this thread 
  +    // then just reuse that. otherwise create a new one and remember it.
  +    Thread t = Thread.currentThread ();
  +    DocumentBuilder db = (DocumentBuilder) docBuilderTable.get (t);
  +    if (db != null) {
  +      return db;
  +    } else {
  +      try {
  +        db = dbf.newDocumentBuilder();
  +        docBuilderTable.put (t, db);
  +        return db;
  +      } catch (ParserConfigurationException pce) {
  +        throw new IllegalArgumentException(pce.toString());
  +      }
       }
     }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:soap-dev-unsubscribe@;xml.apache.org>
For additional commands, e-mail: <mailto:soap-dev-help@;xml.apache.org>

Reply via email to