You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dm...@apache.org on 2003/08/24 03:52:09 UTC

cvs commit: jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri JXPathContextReferenceImpl.java

dmitri      2003/08/23 18:52:09

  Modified:    jxpath/src/java/org/apache/commons/jxpath/ri
                        JXPathContextReferenceImpl.java
  Log:
  Addressed cache synchronization issue
  
  Revision  Changes    Path
  1.33      +36 -34    jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java
  
  Index: JXPathContextReferenceImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- JXPathContextReferenceImpl.java	4 May 2003 23:51:59 -0000	1.32
  +++ JXPathContextReferenceImpl.java	24 Aug 2003 01:52:09 -0000	1.33
  @@ -71,6 +71,7 @@
   import java.util.Iterator;
   import java.util.Map;
   import java.util.Vector;
  +import java.util.Map.Entry;
   
   import org.apache.commons.jxpath.CompiledExpression;
   import org.apache.commons.jxpath.Function;
  @@ -238,45 +239,46 @@
   
       private Expression compileExpression(String xpath) {
           Expression expr;
  -        if (USE_SOFT_CACHE) {
  -            expr = null;
  -            SoftReference ref = (SoftReference) compiled.get(xpath);
  -            if (ref != null) {
  -                expr = (Expression) ref.get();
  -            }
  -            if (expr == null) {
  -                expr =
  -                    (Expression) Parser.parseExpression(xpath, getCompiler());
  -                synchronized (compiled) {
  -                    compiled.put(xpath, new SoftReference(expr));
  -                }
  -                if (cleanupCount++ >= CLEANUP_THRESHOLD) {
  -                    cleanupCache();
  +
  +        synchronized (compiled) {
  +            if (USE_SOFT_CACHE) {
  +                expr = null;
  +                SoftReference ref = (SoftReference) compiled.get(xpath);
  +                if (ref != null) {
  +                    expr = (Expression) ref.get();
                   }
               }
  -        }
  -        else {
  -            expr = (Expression) compiled.get(xpath);
  -            if (expr == null) {
  -                expr =
  -                    (Expression) Parser.parseExpression(xpath, getCompiler());
  -                compiled.put(xpath, expr);
  +            else {
  +                expr = (Expression) compiled.get(xpath);
               }
           }
  -        return expr;
  -    }
   
  -    private static void cleanupCache() {
  +        if (expr != null) {
  +            return expr;
  +        }
  +
  +        expr = (Expression) Parser.parseExpression(xpath, getCompiler());
  +
           synchronized (compiled) {
  -            Iterator it = compiled.entrySet().iterator();
  -            while (it.hasNext()) {
  -                Map.Entry me = (Map.Entry) it.next();
  -                if (((SoftReference) me.getValue()).get() == null) {
  -                    it.remove();
  +            if (USE_SOFT_CACHE) {
  +                if (cleanupCount++ >= CLEANUP_THRESHOLD) {
  +                    Iterator it = compiled.entrySet().iterator();
  +                    while (it.hasNext()) {
  +                        Entry me = (Entry) it.next();
  +                        if (((SoftReference) me.getValue()).get() == null) {
  +                            it.remove();
  +                        }
  +                    }
  +                    cleanupCount = 0;
                   }
  +                compiled.put(xpath, new SoftReference(expr));
  +            }
  +            else {
  +                compiled.put(xpath, expr);
               }
  -            cleanupCount = 0;
           }
  +
  +        return expr;
       }
   
       /**