commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r950212 - /commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/EvictionTimer.java
Date Tue, 01 Jun 2010 19:10:17 GMT
Author: markt
Date: Tue Jun  1 19:10:17 2010
New Revision: 950212

URL: http://svn.apache.org/viewvc?rev=950212&view=rev
Log:
Partial fix for POOL-161
The default context class loader for the eviction thread needs to be the same class loader
that loads the library else memory leaks can occur in multiple class loader environments 

Modified:
    commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/EvictionTimer.java

Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/EvictionTimer.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/EvictionTimer.java?rev=950212&r1=950211&r2=950212&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/EvictionTimer.java (original)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/EvictionTimer.java Tue
Jun  1 19:10:17 2010
@@ -17,6 +17,8 @@
 
 package org.apache.commons.pool.impl;
 
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.Timer;
 import java.util.TimerTask;
 
@@ -58,7 +60,17 @@ class EvictionTimer {
      */
     static synchronized void schedule(TimerTask task, long delay, long period) {
         if (null == _timer) {
-            _timer = new Timer(true);
+            // Force the new Timer thread to be created with a context class
+            // loader set to the class loader that loaded this library
+            ClassLoader ccl = (ClassLoader) AccessController.doPrivileged(
+                    new PrivilegedGetTccl());
+            try {
+                AccessController.doPrivileged(new PrivilegedSetTccl(
+                        EvictionTimer.class.getClassLoader()));
+                _timer = new Timer(true);
+            } finally {
+                AccessController.doPrivileged(new PrivilegedSetTccl(ccl));
+            }
         }
         _usageCount++;
         _timer.schedule(task, delay, period);
@@ -76,4 +88,26 @@ class EvictionTimer {
             _timer = null;
         }
     }
+    
+    private static class PrivilegedGetTccl implements PrivilegedAction {
+
+        public Object run() {
+            return Thread.currentThread().getContextClassLoader();
+        }
+    }
+
+    private static class PrivilegedSetTccl implements PrivilegedAction {
+
+        private ClassLoader cl;
+
+        PrivilegedSetTccl(ClassLoader cl) {
+            this.cl = cl;
+        }
+
+        public Object run() {
+            Thread.currentThread().setContextClassLoader(cl);
+            return null;
+        }
+    }
+
 }



Mime
View raw message