[jboss-cvs] JBossAS SVN: r68166 - trunk/ejb3/src/main/org/jboss/ejb3/cache/tree.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Dec 11 19:17:30 EST 2007


Author: bstansberry at jboss.com
Date: 2007-12-11 19:17:30 -0500 (Tue, 11 Dec 2007)
New Revision: 68166

Modified:
   trunk/ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulTreeCache.java
Log:
Avoid NPEs if stop gets called before start is done
[EJBTHREE-1136] Clean bean region before activating it

Modified: trunk/ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulTreeCache.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulTreeCache.java	2007-12-12 00:16:09 UTC (rev 68165)
+++ trunk/ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulTreeCache.java	2007-12-12 00:17:30 UTC (rev 68166)
@@ -126,7 +126,7 @@
          ctx.setInUse(true);
          ctx.lastUsed = System.currentTimeMillis();
          ++createCount;
-         beans.put(ctx.getId(), ctx.lastUsed);
+         beans.put(ctx.getId(), new Long(ctx.lastUsed));
       }
       catch (EJBException e)
       {
@@ -153,7 +153,7 @@
          ctx.setInUse(true);
          ctx.lastUsed = System.currentTimeMillis();
          ++createCount;
-         beans.put(ctx.getId(), ctx.lastUsed);
+         beans.put(ctx.getId(), new Long(ctx.lastUsed));
       }
       catch (EJBException e)
       {
@@ -214,7 +214,7 @@
          // Note the Fqn we use is relative to the region!
          region.markNodeCurrentlyInUse(new Fqn(key.toString()), MarkInUseWaitTime);
          entry.lastUsed = System.currentTimeMillis();
-         beans.put(key, entry.lastUsed);
+         beans.put(key, new Long(entry.lastUsed));
       }
       
       if(log.isTraceEnabled())
@@ -277,7 +277,7 @@
       {
          ctx.setInUse(false);
          ctx.lastUsed = System.currentTimeMillis();
-         beans.put(ctx.getId(), ctx.lastUsed);
+         beans.put(ctx.getId(), new Long(ctx.lastUsed));
          // OK, it is free to passivate now.
          // Note the Fqn we use is relative to the region!
          region.unmarkNodeCurrentlyInUse(getFqn(ctx.getId(), true));
@@ -333,6 +333,9 @@
             config.maxSize());
       region.setEvictionPolicy(epc);
       
+      // JBCACHE-1136.  There's no reason to have state in an inactive region
+      cleanBeanRegion();
+      
       // Transfer over the state for the region
       region.registerContextClassLoader(cl);
       region.activate();
@@ -374,31 +377,39 @@
    {
       running = false;
 
-      // Remove the listener
-      cache.removeCacheListener(listener);
-      
-      try {
+      if (cache != null)
+      {
+         // Remove the listener
+         if (listener != null)
+            cache.removeCacheListener(listener);
+   
          // Remove locally. We do this to clean up the persistent store,
-         // which is not affected by the region.deactivate call below.
-         InvocationContext ctx = cache.getInvocationContext();
-         ctx.setOptionOverrides(getLocalOnlyOption());
-         cache.removeNode(cacheNode);
-      } 
-      catch (CacheException e) 
-      {
-         log.error("stop(): can't remove bean from the underlying distributed cache");
-      }
-      
-      if (region != null)
-      {
-         region.deactivate();
-         region.unregisterContextClassLoader();      
+         // which is not affected by the inactivateRegion call below.
+         cleanBeanRegion();    
          
-         // FIXME this method needs to be in Cache
-         ((CacheSPI) cache).getRegionManager().removeRegion(region.getFqn());
-         // Clear any queues
-         region.resetEvictionQueues();
-         region = null;
+         try {
+            // Remove locally. We do this to clean up the persistent store,
+            // which is not affected by the region.deactivate call below.
+            InvocationContext ctx = cache.getInvocationContext();
+            ctx.setOptionOverrides(getLocalOnlyOption());
+            cache.removeNode(cacheNode);
+         } 
+         catch (CacheException e) 
+         {
+            log.error("stop(): can't remove bean from the underlying distributed cache");
+         }
+         
+         if (region != null)
+         {
+            region.deactivate();
+            region.unregisterContextClassLoader();      
+            
+            // FIXME this method needs to be in Cache
+            ((CacheSPI) cache).getRegionManager().removeRegion(region.getFqn());
+            // Clear any queues
+            region.resetEvictionQueues();
+            region = null;
+         }
       }
       
       classloader = null;
@@ -504,6 +515,19 @@
          return new Fqn(cacheNode, hashBuckets[index], beanId);
    }
    
+   private void cleanBeanRegion()
+   {
+      try {
+         // Remove locally.
+         cache.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
+         cache.removeNode(cacheNode);
+      } 
+      catch (CacheException e) 
+      {
+         log.error("Stop(): can't remove bean from the underlying distributed cache");
+      }       
+   }
+   
    /** 
     * Creates a RuntimeException, but doesn't pass CacheException as the cause
     * as it is a type that likely doesn't exist on a client.
@@ -708,7 +732,7 @@
                while (it.hasNext())
                {
                   Map.Entry<Object, Long> entry = it.next();
-                  long lastUsed = entry.getValue();
+                  long lastUsed = entry.getValue().longValue();
                   if (now - lastUsed >= removalTimeout * 1000)
                   {
                      remove(entry.getKey());




More information about the jboss-cvs-commits mailing list