xalan-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mk...@apache.org
Subject cvs commit: xml-xalan/java/src/org/apache/xalan/lib Extensions.java ExsltStrings.java
Date Mon, 25 Nov 2002 17:30:31 GMT
mkwan       2002/11/25 09:30:30

  Modified:    java/src/org/apache/xalan/lib Extensions.java
                        ExsltStrings.java
  Log:
  Fix possible thread contention issue by putting
  a lock on the Document object before using it.
  
  Revision  Changes    Path
  1.22      +16 -6     xml-xalan/java/src/org/apache/xalan/lib/Extensions.java
  
  Index: Extensions.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/lib/Extensions.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- Extensions.java	16 Sep 2002 15:54:45 -0000	1.21
  +++ Extensions.java	25 Nov 2002 17:30:30 -0000	1.22
  @@ -93,7 +93,8 @@
   {
   
     // Reuse the Document object to reduce memory usage.
  -  private static Document lDoc = null;
  +  private static Document m_doc = null;
  +  private static Extensions m_instance = new Extensions();
     
     /**
      * Constructor Extensions
  @@ -289,11 +290,17 @@
      */
     public static NodeList tokenize(String toTokenize, String delims)
     {
  -
       try
       {
  -      if (lDoc == null)
  -        lDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
  +      // Lock the instance to ensure thread safety
  +      if (m_doc == null)
  +      {
  +        synchronized (m_instance)
  +        {
  +          if (m_doc == null)
  +            m_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
  +        }
  +      }
       }
       catch(ParserConfigurationException pce)
       {
  @@ -303,9 +310,12 @@
       StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims);
       NodeSet resultSet = new NodeSet();
   
  -    while (lTokenizer.hasMoreTokens())
  +    synchronized (m_doc)
       {
  -      resultSet.addNode(lDoc.createTextNode(lTokenizer.nextToken()));
  +      while (lTokenizer.hasMoreTokens())
  +      {
  +        resultSet.addNode(m_doc.createTextNode(lTokenizer.nextToken()));
  +      }
       }
   
       return resultSet;
  
  
  
  1.9       +44 -20    xml-xalan/java/src/org/apache/xalan/lib/ExsltStrings.java
  
  Index: ExsltStrings.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/lib/ExsltStrings.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ExsltStrings.java	4 Nov 2002 15:18:20 -0000	1.8
  +++ ExsltStrings.java	25 Nov 2002 17:30:30 -0000	1.9
  @@ -78,7 +78,8 @@
   public class ExsltStrings extends ExsltBase
   {
     // Reuse the Document object to reduce memory usage.
  -  private static Document lDoc = null;
  +  private static Document m_doc = null;
  +  private static ExsltStrings m_instance = new ExsltStrings();
   
     /**
      * The str:align function aligns a string within another string. 
  @@ -231,8 +232,15 @@
     {
       try
       {
  -      if (lDoc == null)
  -        lDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
  +      // Lock the instance to ensure thread safety
  +      if (m_doc == null)
  +      {
  +        synchronized (m_instance)
  +        {
  +          if (m_doc == null)
  +            m_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
  +        }
  +      }
       }
       catch(ParserConfigurationException pce)
       {
  @@ -252,19 +260,22 @@
         matchIndex = str.indexOf(pattern, fromIndex);
         if (matchIndex >= 0)
         {
  -	    token = str.substring(fromIndex, matchIndex);
  -	    fromIndex = matchIndex + pattern.length();
  -	  }
  +	token = str.substring(fromIndex, matchIndex);
  +	fromIndex = matchIndex + pattern.length();
  +      }
         else
         {
           done = true;
           token = str.substring(fromIndex);
         }
           
  -      Element element = lDoc.createElement("token");
  -      Text text = lDoc.createTextNode(token);
  -      element.appendChild(text);
  -      resultSet.addNode(element);      
  +      synchronized (m_doc)
  +      {
  +        Element element = m_doc.createElement("token");
  +        Text text = m_doc.createTextNode(token);
  +        element.appendChild(text);
  +        resultSet.addNode(element);      
  +      }
       }
       
       return resultSet;
  @@ -315,8 +326,15 @@
     {
       try
       {
  -      if (lDoc == null)
  -        lDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
  +      // Lock the instance to ensure thread safety
  +      if (m_doc == null)
  +      {
  +        synchronized (m_instance)
  +        {
  +          if (m_doc == null)
  +            m_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
  +        }
  +      }
       }
       catch(ParserConfigurationException pce)
       {
  @@ -329,22 +347,28 @@
       {
         StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims);
   
  -      while (lTokenizer.hasMoreTokens())
  +      synchronized (m_doc)
         {
  -        Element element = lDoc.createElement("token");
  -        element.appendChild(lDoc.createTextNode(lTokenizer.nextToken()));
  -        resultSet.addNode(element);      
  +        while (lTokenizer.hasMoreTokens())
  +        {
  +          Element element = m_doc.createElement("token");
  +          element.appendChild(m_doc.createTextNode(lTokenizer.nextToken()));
  +          resultSet.addNode(element);      
  +        }
         }
       }
       // If the delimiter is an empty string, create one token Element for 
       // every single character.
       else
       {
  -      for (int i = 0; i < toTokenize.length(); i++)
  +      synchronized (m_doc)
         {
  -        Element element = lDoc.createElement("token");
  -        element.appendChild(lDoc.createTextNode(toTokenize.substring(i, i+1)));
  -        resultSet.addNode(element);              
  +        for (int i = 0; i < toTokenize.length(); i++)
  +        {
  +          Element element = m_doc.createElement("token");
  +          element.appendChild(m_doc.createTextNode(toTokenize.substring(i, i+1)));
  +          resultSet.addNode(element);              
  +        }
         }
       }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org


Mime
View raw message