Put a cap on the maximum number of paths that can be cached.


git-svn-id: https://svn.apache.org/repos/asf/xmlbeans/branches/1.x@408791 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/xmlstore/org/apache/xmlbeans/impl/store/Path.java b/src/xmlstore/org/apache/xmlbeans/impl/store/Path.java
index ac54b1a..ac32242 100644
--- a/src/xmlstore/org/apache/xmlbeans/impl/store/Path.java
+++ b/src/xmlstore/org/apache/xmlbeans/impl/store/Path.java
@@ -23,8 +23,9 @@
 import org.apache.xmlbeans.impl.store.Cursor.PathEngine;
 import org.apache.xmlbeans.impl.store.Cursor.Selections;
 
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * Represents a precompiled path expression
@@ -559,7 +560,32 @@
         }
     }
 
-    private static HashMap _xqrlPathCache = new HashMap();
-    private static HashMap _xbeanPathCache = new HashMap();
-    private static HashMap _xqrlQueryCache = new HashMap();
+    private static Map _xqrlPathCache = new CacheMap();
+    private static Map _xbeanPathCache = new CacheMap();
+    private static Map _xqrlQueryCache = new CacheMap();
+
+    private static class CacheMap extends LinkedHashMap
+    {
+        private static final String XPATH_CACHE_SIZE = "xmlbean.xpathCacheSize";
+
+        private static int MAX_ENTRIES = -1;
+
+        CacheMap()
+        {
+            String size = null;
+            if ((size = System.getProperty(XPATH_CACHE_SIZE)) != null)
+                try 
+                {
+                    MAX_ENTRIES = Integer.parseInt(size);
+                }
+                catch (Exception e)
+                {
+                }
+        }
+
+        protected boolean removeEldestEntry(Map.Entry eldest)
+        {
+            return MAX_ENTRIES > 0 && size() > MAX_ENTRIES;
+        }
+    }
 }