[jboss-cvs] JBossAS SVN: r77668 - in trunk: testsuite/src/main/org/jboss/test/cluster/defaultcfg/test and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Aug 29 18:35:10 EDT 2008


Author: bstansberry at jboss.com
Date: 2008-08-29 18:35:09 -0400 (Fri, 29 Aug 2008)
New Revision: 77668

Added:
   trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManagerManagedCache.java
   trunk/cluster/src/main/org/jboss/ha/cachemanager/PojoCacheManagerManagedPojoCache.java
Modified:
   trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManager.java
   trunk/testsuite/src/main/org/jboss/test/cluster/defaultcfg/test/CacheManagerUnitTestCase.java
Log:
[JBAS-5915] CacheManager to set TCCL before creating/starting caches

Modified: trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManager.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManager.java	2008-08-29 22:34:39 UTC (rev 77667)
+++ trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManager.java	2008-08-29 22:35:09 UTC (rev 77668)
@@ -176,7 +176,8 @@
          }
       }
 
-      return cache;
+      // Wrap the pojocache to control classloading and disable stop/destroy
+      return cache == null ? null : new PojoCacheManagerManagedPojoCache(cache);
    }
    
    /**
@@ -238,10 +239,10 @@
                       " the PojoCache's underlying plain cache");
             PojoCache pc = getPojoCache(configName, false);
             if (pc != null)
-               return pc.getCache();
+               return wrapCache(pc.getCache());
          }
          
-         return super.getCache(configName, create);
+         return wrapCache(super.getCache(configName, create));
       }
    }
     
@@ -540,6 +541,14 @@
    }
 
    // ----------------------------------------------------------------  Private
+   
+   /**
+    * Wrap the pojocache to control classloading and disable stop/destroy
+    */
+   private Cache wrapCache(Cache toWrap)
+   {
+      return toWrap == null ? null : new CacheManagerManagedCache(toWrap);
+   }
 
    private int incrementPojoCacheCheckout(String configName)
    {

Added: trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManagerManagedCache.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManagerManagedCache.java	                        (rev 0)
+++ trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManagerManagedCache.java	2008-08-29 22:35:09 UTC (rev 77668)
@@ -0,0 +1,322 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.ha.cachemanager;
+
+import java.security.AccessController;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.jboss.cache.Cache;
+import org.jboss.cache.CacheException;
+import org.jboss.cache.CacheStatus;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.InvocationContext;
+import org.jboss.cache.Node;
+import org.jboss.cache.NodeNotExistsException;
+import org.jboss.cache.Region;
+import org.jboss.cache.config.Configuration;
+import org.jboss.logging.Logger;
+import org.jboss.util.loading.ContextClassLoaderSwitcher;
+import org.jgroups.Address;
+
+/**
+ * Wrapper around a cache that 1) ensures the calling thread's TCCL doesn't
+ * leak to cache threads via calls to create/start; and 2) logs WARNS about calls
+ * to stop/destroy as these should be handled by the CacheManager.
+ * 
+ * TODO disable calls to stop/destroy once testsuite cleanup code is fixed
+ * to not call those methods.
+ * 
+ * @author Brian Stansberry
+ */
+class CacheManagerManagedCache implements Cache
+{
+   private static final Logger log = Logger.getLogger(CacheManagerManagedCache.class);
+   
+   private final Cache delegate;
+   private final ContextClassLoaderSwitcher switcher;
+   
+   CacheManagerManagedCache(Cache delegate)
+   {
+      assert delegate != null : "delegate is null";
+      this.delegate = delegate;
+      this.switcher = (ContextClassLoaderSwitcher) AccessController.doPrivileged(ContextClassLoaderSwitcher.INSTANTIATOR);
+   }
+
+   /**
+    * Switches the TCCL to the delegate's classloader before calling create()
+    * on the delegate.
+    */
+   public void create() throws CacheException
+   {
+      ContextClassLoaderSwitcher.SwitchContext switchContext = switcher.getSwitchContext();
+      try
+      {
+         switchContext.setClassLoader(delegate.getClass().getClassLoader());
+         delegate.create();
+      }
+      finally
+      {
+         switchContext.reset();
+      }
+   }
+
+   /**
+    * Switches the TCCL to the delegate's classloader before calling start()
+    * on the delegate.
+    */
+   public void start() throws CacheException
+   {
+      ContextClassLoaderSwitcher.SwitchContext switchContext = switcher.getSwitchContext();
+      try
+      {
+         switchContext.setClassLoader(delegate.getClass().getClassLoader());
+         delegate.start();
+      }
+      finally
+      {
+         switchContext.reset();
+      }
+   }
+
+   /**
+    * TODO: Log a WARN and do nothing else; currently logs and then calls 
+    * through to delegate.
+    */
+   public void stop()
+   {
+      log.warn("stop() should not be directly called on caches obtained from a CacheManager -- use CacheManager.releaseCache()", 
+            new UnsupportedOperationException("stop() is not supported"));
+      delegate.stop();
+   }
+
+   /**
+    * TODO: Log a WARN and do nothing else; currently logs and then calls 
+    * through to delegate.
+    */
+   public void destroy()
+   {
+      log.warn("destroy() should not be directly called on caches obtained from a CacheManager -- use CacheManager.releaseCache()", 
+            new UnsupportedOperationException("destroy() is not supported"));
+      delegate.destroy();
+   }
+   
+   // ------------------------------------------------------------------ Cache
+   
+   public void addCacheListener(Object arg0)
+   {
+      delegate.addCacheListener(arg0);
+   }
+
+   public void clearData(String arg0)
+   {
+      delegate.clearData(arg0);
+   }
+
+   public void clearData(Fqn arg0)
+   {
+      delegate.clearData(arg0);
+   }
+
+   public void evict(Fqn arg0)
+   {
+      delegate.evict(arg0);
+   }
+
+   public void evict(Fqn arg0, boolean arg1)
+   {
+      delegate.evict(arg0, arg1);
+   }
+
+   public Object get(Fqn arg0, Object arg1)
+   {
+      return delegate.get(arg0, arg1);
+   }
+
+   public Object get(String arg0, Object arg1)
+   {
+      return delegate.get(arg0, arg1);
+   }
+
+   public Set getCacheListeners()
+   {
+      return delegate.getCacheListeners();
+   }
+
+   public CacheStatus getCacheStatus()
+   {
+      return delegate.getCacheStatus();
+   }
+
+   public Configuration getConfiguration()
+   {
+      return delegate.getConfiguration();
+   }
+
+   public Map getData(Fqn arg0)
+   {
+      return delegate.getData(arg0);
+   }
+
+   public InvocationContext getInvocationContext()
+   {
+      return delegate.getInvocationContext();
+   }
+
+   public Set getKeys(String arg0)
+   {
+      return getKeys(arg0);
+   }
+
+   public Set getKeys(Fqn arg0)
+   {
+      return getKeys(arg0);
+   }
+
+   public Address getLocalAddress()
+   {
+      return delegate.getLocalAddress();
+   }
+
+   public List getMembers()
+   {
+      return delegate.getMembers();
+   }
+
+   public Node getNode(Fqn arg0)
+   {
+      return delegate.getNode(arg0);
+   }
+
+   public Node getNode(String arg0)
+   {
+      return delegate.getNode(arg0);
+   }
+
+   public Region getRegion(Fqn arg0, boolean arg1)
+   {
+      return delegate.getRegion(arg0, arg1);
+   }
+
+   public Node getRoot()
+   {
+      return delegate.getRoot();
+   }
+
+   public String getVersion()
+   {
+      return delegate.getVersion();
+   }
+
+   public void move(Fqn arg0, Fqn arg1) throws NodeNotExistsException
+   {
+      delegate.move(arg0, arg1);
+   }
+
+   public void move(String arg0, String arg1) throws NodeNotExistsException
+   {
+      delegate.move(arg0, arg1);
+   }
+
+   public void put(Fqn arg0, Map arg1)
+   {
+      delegate.put(arg0, arg1);
+   }
+
+   public void put(String arg0, Map arg1)
+   {
+      delegate.put(arg0, arg1);
+   }
+
+   public Object put(Fqn arg0, Object arg1, Object arg2)
+   {
+      return delegate.put(arg0, arg1, arg2);
+   }
+
+   public Object put(String arg0, Object arg1, Object arg2)
+   {
+      return delegate.put(arg0, arg1, arg2);
+   }
+
+   public void putForExternalRead(Fqn arg0, Object arg1, Object arg2)
+   {
+      delegate.putForExternalRead(arg0, arg1, arg2);
+   }
+
+   public Object remove(Fqn arg0, Object arg1)
+   {
+      return delegate.remove(arg0, arg1);
+   }
+
+   public Object remove(String arg0, Object arg1)
+   {
+      return delegate.remove(arg0, arg1);
+   }
+
+   public void removeCacheListener(Object arg0)
+   {
+      delegate.removeCacheListener(arg0);
+   }
+
+   public boolean removeNode(Fqn arg0)
+   {
+       return delegate.removeNode(arg0);
+   }
+
+   public boolean removeNode(String arg0)
+   {
+      return delegate.removeNode(arg0);
+   }
+
+   public boolean removeRegion(Fqn arg0)
+   {
+      return delegate.removeRegion(arg0);
+   }
+
+   public void setInvocationContext(InvocationContext arg0)
+   {
+      delegate.setInvocationContext(arg0);
+   }
+
+   // --------------------------------------------------------------  Overrides
+
+   @Override
+   public boolean equals(Object obj)
+   {
+      if (obj instanceof CacheManagerManagedCache)
+      {
+         CacheManagerManagedCache other = (CacheManagerManagedCache) obj;
+         return delegate.equals(other.delegate);
+      }
+      return false;
+   }
+
+   @Override
+   public int hashCode()
+   {
+      return delegate.hashCode();
+   }
+   
+   
+}

Added: trunk/cluster/src/main/org/jboss/ha/cachemanager/PojoCacheManagerManagedPojoCache.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/cachemanager/PojoCacheManagerManagedPojoCache.java	                        (rev 0)
+++ trunk/cluster/src/main/org/jboss/ha/cachemanager/PojoCacheManagerManagedPojoCache.java	2008-08-29 22:35:09 UTC (rev 77668)
@@ -0,0 +1,222 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.ha.cachemanager;
+
+import java.security.AccessController;
+import java.util.Collection;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+import org.jboss.cache.Cache;
+import org.jboss.cache.CacheException;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.pojo.PojoCache;
+import org.jboss.cache.pojo.PojoCacheException;
+import org.jboss.cache.pojo.PojoCacheThreadContext;
+import org.jboss.logging.Logger;
+import org.jboss.util.loading.ContextClassLoaderSwitcher;
+
+/**
+ * Wrapper around a PojoCache that 1) ensures the calling thread's TCCL doesn't
+ * leak to cache threads via calls to create/start; and 2) logs WARNS about calls
+ * to stop/destroy as these should be handled by the PojoCacheManager.
+ * 
+ * TODO disable calls to stop/destroy once testsuite cleanup code is fixed
+ * to not call those methods.
+ * 
+ * @author Brian Stansberry
+ *
+ */
+class PojoCacheManagerManagedPojoCache implements PojoCache
+{
+   private static final Logger log = Logger.getLogger(PojoCacheManagerManagedPojoCache.class);
+   
+   private final PojoCache delegate;
+   private final ContextClassLoaderSwitcher switcher;
+   
+   PojoCacheManagerManagedPojoCache(PojoCache delegate)
+   {
+      assert delegate != null : "delegate is null";
+      this.delegate = delegate;
+      this.switcher = (ContextClassLoaderSwitcher) AccessController.doPrivileged(ContextClassLoaderSwitcher.INSTANTIATOR);
+   }
+
+   /**
+    * Switches the TCCL to the delegate's classloader before calling create()
+    * on the delegate.
+    */
+   public void create() throws CacheException
+   {
+      ContextClassLoaderSwitcher.SwitchContext switchContext = switcher.getSwitchContext();
+      try
+      {
+         switchContext.setClassLoader(delegate.getClass().getClassLoader());
+         delegate.create();
+      }
+      finally
+      {
+         switchContext.reset();
+      }
+   }
+
+   /**
+    * Switches the TCCL to the delegate's classloader before calling start()
+    * on the delegate.
+    */
+   public void start() throws CacheException
+   {
+      ContextClassLoaderSwitcher.SwitchContext switchContext = switcher.getSwitchContext();
+      try
+      {
+         switchContext.setClassLoader(delegate.getClass().getClassLoader());
+         delegate.start();
+      }
+      finally
+      {
+         switchContext.reset();
+      }
+   }
+
+   /**
+    * TODO: Log a WARN and do nothing else; currently logs and then calls 
+    * through to delegate.
+    */
+   public void stop() throws PojoCacheException
+   {
+      log.warn("stop() should not be directly called on caches obtained from a PojoCacheManager -- use CacheManager.releaseCache()", 
+            new UnsupportedOperationException("stop() is not supported"));
+      delegate.stop();
+   }
+
+   /**
+    * TODO: Log a WARN and do nothing else; currently logs and then calls 
+    * through to delegate.
+    */
+   public void destroy() throws PojoCacheException
+   {
+      log.warn("destroy() should not be directly called on caches obtained from a PojoCacheManager -- use CacheManager.releaseCache()", 
+            new UnsupportedOperationException("destroy() is not supported"));
+      delegate.destroy();
+   }
+   
+   public void addListener(Object arg0)
+   {
+      delegate.addListener(arg0);
+   }
+
+   public void addListener(Object arg0, Pattern arg1)
+   {
+      delegate.addListener(arg0, arg1);
+   }
+
+   public Object attach(String arg0, Object arg1) throws PojoCacheException
+   {
+      return delegate.attach(arg0, arg1);
+   }
+
+   public Object attach(Fqn<?> arg0, Object arg1) throws PojoCacheException
+   {
+      return delegate.attach(arg0, arg1);
+   }
+
+   public Object detach(String arg0) throws PojoCacheException
+   {
+      return delegate.detach(arg0);
+   }
+
+   public Object detach(Fqn<?> arg0) throws PojoCacheException
+   {
+      return delegate.detach(arg0);
+   }
+
+   public boolean exists(Fqn<?> arg0)
+   {
+      return delegate.exists(arg0);
+   }
+
+   public Object find(String arg0) throws PojoCacheException
+   {
+      return delegate.find(arg0);
+   }
+
+   public Object find(Fqn<?> arg0) throws PojoCacheException
+   {
+      return delegate.find(arg0);
+   }
+
+   public Map<Fqn<?>, Object> findAll(String arg0) throws PojoCacheException
+   {
+      return delegate.findAll(arg0);
+   }
+
+   public Map<Fqn<?>, Object> findAll(Fqn<?> arg0) throws PojoCacheException
+   {
+      return delegate.findAll(arg0);
+   }
+
+   public Cache<Object, Object> getCache()
+   {
+      Cache c = delegate.getCache();
+      return c == null ? null : new CacheManagerManagedCache(c);
+   }
+
+   public Collection<Object> getListeners()
+   {
+      return delegate.getListeners();
+   }
+
+   public String getPojoID(Object arg0)
+   {
+      return delegate.getPojoID(arg0);
+   }
+
+   public PojoCacheThreadContext getThreadContext()
+   {
+      return delegate.getThreadContext();
+   }
+
+   public void removeListener(Object arg0)
+   {
+      delegate.removeListener(arg0);
+   }
+
+   // --------------------------------------------------------------  Overrides
+
+   @Override
+   public boolean equals(Object obj)
+   {
+      if (obj instanceof PojoCacheManagerManagedPojoCache)
+      {
+         PojoCacheManagerManagedPojoCache other = (PojoCacheManagerManagedPojoCache) obj;
+         return delegate.equals(other.delegate);
+      }
+      return false;
+   }
+
+   @Override
+   public int hashCode()
+   {
+      return delegate.hashCode();
+   }
+
+}

Modified: trunk/testsuite/src/main/org/jboss/test/cluster/defaultcfg/test/CacheManagerUnitTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/cluster/defaultcfg/test/CacheManagerUnitTestCase.java	2008-08-29 22:34:39 UTC (rev 77667)
+++ trunk/testsuite/src/main/org/jboss/test/cluster/defaultcfg/test/CacheManagerUnitTestCase.java	2008-08-29 22:35:09 UTC (rev 77668)
@@ -164,7 +164,7 @@
       
       // Test 2 checkouts of the same cache
       Cache<Object, Object> cache2 = registry.getCache(configName, true);      
-      assertTrue(cache == cache2);
+      assertEquals(cache, cache2);
       
       registry.releaseCache(configName);
       
@@ -257,7 +257,7 @@
       
       // Test 2 checkouts of the same cache
       PojoCache cache2 = registry.getPojoCache(configName, true);      
-      assertTrue(cache == cache2);
+      assertEquals(cache, cache2);
       
       registry.releaseCache(configName);
       
@@ -322,7 +322,7 @@
       Cache cache = registry.getCache("alias", true);
       assertNotNull(cache);
       Cache other = registry.getCache(DEFAULT_STACK, false);
-      assertSame(cache, other);
+      assertEquals(cache, other);
       
       assertEquals(1, registry.getCacheNames().size());
       
@@ -337,7 +337,7 @@
       PojoCache pcache = registry.getPojoCache("alias", true);
       assertNotNull(pcache);
       PojoCache otherPC = registry.getPojoCache(DEFAULT_STACK, false);
-      assertSame(pcache, otherPC);
+      assertEquals(pcache, otherPC);
       
       assertEquals(1, registry.getPojoCacheNames().size());
       




More information about the jboss-cvs-commits mailing list