/*
 * Copyright  1999-2004 The Apache Software Foundation.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.xml.security.utils;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.WeakHashMap;

import org.apache.xpath.CachedXPathAPI;


/**
 * @author Raul Benito
 */
public class CachedXPathAPIHolder {
    //static ThreadLocal  local=new ThreadLocal();
    static WeakHashMap local=new WeakHashMap();
    	           
    CachedXPathAPI cx;
	/**
	 * 
	 */
	public CachedXPathAPIHolder() {
		cx=null;
		// TODO Auto-generated constructor stub
	}
    /**
     * @return
     */
    public CachedXPathAPI getCachedXPathAPI() {
        if (cx==null) { 
                  SoftReference sr=(SoftReference)local.get(Thread.currentThread());
                  if (sr!=null) {
                    cx=(CachedXPathAPI)sr.get();
                  }
                  if (cx==null) {
                     cx=new CachedXPathAPI();
                     local.put(Thread.currentThread(),new SoftReference(cx));
                  }                 
                  //cx.getXPathContext().reset();//
                  //cx=new CachedXPathAPI();
        }
    	return cx;
    }
}

@@ -670,111 +670,8 @@
 s;%0A%0A
-import java.lang.ref.SoftReference;%0Aimport java.lang.ref.WeakReference;%0Aimport java.util.WeakHashMap;%0A%0A
 impo
@@ -779,18 +779,16 @@
 r %7B%0A    
-//
 static T
@@ -832,68 +832,122 @@
     
-static WeakHashMap local=new WeakHashMap();%0A  
+//Just to call reset after a time.%0A
   
-%09
   
-         
+static final int RESET_NUMBER=10;%0A    static int resetCounter=RESET_NUMBER;
 %0A   
@@ -1196,72 +1196,37 @@
     
-SoftReference sr=(SoftReference)local.get(Thread.currentThread()
+cx=(CachedXPathAPI)local.get(
 );%0A 
@@ -1250,11 +1250,11 @@
 if (
-sr!
+cx=
 =nul
@@ -1270,36 +1270,40 @@
                 
+ 
 cx=
-(
+new 
 CachedXPathAPI)s
@@ -1304,16 +1304,45 @@
 hAPI
-)sr.g
+();%0A                     local.s
 et(
+cx
 );%0A 
@@ -1355,24 +1355,31 @@
            %7D
+ else %7B
 %0A           
@@ -1381,36 +1381,79 @@
             
-if (cx==null
+%09resetCounter--;%0A                    if (resetCounter%3C0
 ) %7B%0A        
@@ -1464,38 +1464,43 @@
         
- cx=new CachedXPathAPI
+%09cx.getXPathContext().reset
 ();%0A    
@@ -1520,65 +1520,40 @@
     
-local.put(Thread.currentThread(),new SoftReference(cx));%0A
+   resetCounter=RESET_NUMBER;%0A  
     
@@ -1567,16 +1567,17 @@
        %7D
+%0A
         
@@ -1585,16 +1585,18 @@
         
+ %7D
 %0A       

Better fix to the memory retention.