[jbosscache-commits] JBoss Cache SVN: r5172 - in pojo/trunk/src: main/java/org/jboss/cache/pojo/impl and 1 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Mon Jan 21 17:31:27 EST 2008


Author: jason.greene at jboss.com
Date: 2008-01-21 17:31:27 -0500 (Mon, 21 Jan 2008)
New Revision: 5172

Modified:
   pojo/trunk/src/main/java/org/jboss/cache/pojo/PojoCacheThreadContext.java
   pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/PojoCacheThreadContextImpl.java
   pojo/trunk/src/test/java/org/jboss/cache/pojo/PojoCacheThreadContextTest.java
Log:
Merge 5170 from branches/2.1


Modified: pojo/trunk/src/main/java/org/jboss/cache/pojo/PojoCacheThreadContext.java
===================================================================
--- pojo/trunk/src/main/java/org/jboss/cache/pojo/PojoCacheThreadContext.java	2008-01-21 22:31:12 UTC (rev 5171)
+++ pojo/trunk/src/main/java/org/jboss/cache/pojo/PojoCacheThreadContext.java	2008-01-21 22:31:27 UTC (rev 5172)
@@ -47,4 +47,10 @@
     * @param gravitate  true if gravitation should be triggered on cache-miss, false if gravitation should not be triggered
     */
    public void setGravitationEnabled(boolean gravitate);
+   
+   /**
+    * Clears all thread settings stored on this context. After invoked, defaults will be returned. This will also reclaim
+    * any memory used to store this thread's settings.
+    */
+   public void clear();
 }
\ No newline at end of file

Modified: pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/PojoCacheThreadContextImpl.java
===================================================================
--- pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/PojoCacheThreadContextImpl.java	2008-01-21 22:31:12 UTC (rev 5171)
+++ pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/PojoCacheThreadContextImpl.java	2008-01-21 22:31:27 UTC (rev 5172)
@@ -9,6 +9,7 @@
    
    // Every cache instance gets it's own configuration
    // An array is used to conserve memory usage since reclamation is slow with TLs, and prevent CL leaks
+   // To further reduce leaks, allocation should be lazily initialized
    // In the future, if we get multiple booleans, use bitwise operations on an integer 
    // as the first entry
    private final ThreadLocal<Object[]> state = new ThreadLocal<Object[]>()
@@ -16,7 +17,7 @@
       @Override
       protected Object[] initialValue()
       {
-         return new Object[] {GRAVITATE_DEFAULT};
+         return null;
       }
    };
    
@@ -31,7 +32,8 @@
     */
    public boolean isGravitationEnabled()
    {
-      return (Boolean) state.get()[GRAVITATE];
+      Object[] values = state.get();
+      return values == null ? GRAVITATE_DEFAULT : (Boolean) values[GRAVITATE];
    }
    
    /**
@@ -40,7 +42,24 @@
     * @param gravitate  true if gravitation should be triggered on cache-miss, false if gravitation should not be triggered
     */
    public void setGravitationEnabled(boolean gravitate)
+   {  
+      Object[] values = state.get();
+      if (values == null)
+      {
+         // Don't initialize if this is the default
+         if (gravitate == GRAVITATE_DEFAULT)
+            return;
+      
+         state.set(new Object[] {gravitate});
+      }
+      else
+      {
+         values[GRAVITATE] = gravitate;
+      }
+   }
+   
+   public void clear()
    {
-      state.get()[GRAVITATE] = gravitate;
+      state.remove();
    }
-}
+}
\ No newline at end of file

Modified: pojo/trunk/src/test/java/org/jboss/cache/pojo/PojoCacheThreadContextTest.java
===================================================================
--- pojo/trunk/src/test/java/org/jboss/cache/pojo/PojoCacheThreadContextTest.java	2008-01-21 22:31:12 UTC (rev 5171)
+++ pojo/trunk/src/test/java/org/jboss/cache/pojo/PojoCacheThreadContextTest.java	2008-01-21 22:31:27 UTC (rev 5172)
@@ -23,28 +23,20 @@
 package org.jboss.cache.pojo;
 
 import static org.testng.AssertJUnit.assertEquals;
-import static org.testng.AssertJUnit.assertNotNull;
-import static org.testng.AssertJUnit.assertNull;
 
-import java.util.List;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.jboss.cache.buddyreplication.NextMemberBuddyLocatorConfig;
-import org.jboss.cache.config.BuddyReplicationConfig;
 import org.jboss.cache.config.Configuration;
 import org.jboss.cache.config.Configuration.CacheMode;
 import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
-import org.jboss.cache.pojo.test.Person;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 /**
- * A BuddyReplicatedTest.
+ * Tests the usage and behavior of PojoCacheThreadContext
  *
- * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
- * @version $Revision: 4717 $
+ * @author Jason T. Greene
  */
 @Test(groups = {"functional"})
 public class PojoCacheThreadContextTest 
@@ -78,6 +70,20 @@
       assertEquals(cache1.getThreadContext().isGravitationEnabled(), false);
    }
    
+   public void testClear()
+   {
+      cache1.getThreadContext().setGravitationEnabled(true);
+      cache.getThreadContext().setGravitationEnabled(true);
+      
+      assertEquals(cache.getThreadContext().isGravitationEnabled(), true);
+      assertEquals(cache1.getThreadContext().isGravitationEnabled(), true);
+      
+      cache1.getThreadContext().clear();
+      
+      assertEquals(cache.getThreadContext().isGravitationEnabled(), true);
+      assertEquals(cache1.getThreadContext().isGravitationEnabled(), false);
+   }
+   
    private class TestThread extends Thread
    {
       public volatile Throwable t;




More information about the jbosscache-commits mailing list