[jboss-cvs] JBossAS SVN: r109219 - projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Nov 10 00:01:07 EST 2010


Author: pferraro
Date: 2010-11-10 00:01:07 -0500 (Wed, 10 Nov 2010)
New Revision: 109219

Modified:
   projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainer.java
Log:
Don't create/start cache using thread context class loader, in case this is 1st cache started within container which starts container components, including channel.

Modified: projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainer.java
===================================================================
--- projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainer.java	2010-11-09 23:58:08 UTC (rev 109218)
+++ projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainer.java	2010-11-10 05:01:07 UTC (rev 109219)
@@ -21,6 +21,7 @@
  */
 package org.jboss.ha.ispn;
 
+import java.security.AccessController;
 import java.util.List;
 import java.util.Properties;
 import java.util.Set;
@@ -38,6 +39,7 @@
 import org.infinispan.notifications.cachemanagerlistener.annotation.CacheStopped;
 import org.infinispan.notifications.cachemanagerlistener.event.CacheStoppedEvent;
 import org.infinispan.remoting.transport.Address;
+import org.jboss.util.loading.ContextClassLoaderSwitcher;
 
 /**
  * Cache manager wrapper to work around ISPN-658
@@ -46,6 +48,10 @@
 @Listener
 public class DefaultCacheContainer implements EmbeddedCacheManager
 {
+   // Need to cast since ContextClassLoaderSwitcher.NewInstance does not generically implement PrivilegedAction<ContextClassLoaderSwitcher>
+   @SuppressWarnings("unchecked")
+   private final ContextClassLoaderSwitcher switcher = (ContextClassLoaderSwitcher) AccessController.doPrivileged(ContextClassLoaderSwitcher.INSTANTIATOR);
+   
    private final EmbeddedCacheManager container;
    private final ConcurrentMap<String, EmbeddedCacheManager> containers = new ConcurrentHashMap<String, EmbeddedCacheManager>();
 
@@ -69,75 +75,88 @@
    // Until ISPN-658 is fixed, we need to create a separate adhoc cache manager if requested cache uses DIST mode.
    private <K, V> Cache<K, V> getCache(String cacheName, Configuration config)
    {
-      if (config.getCacheMode().isDistributed())
+      // Since infinispan doesn't init the shared cache container until the 1st cache is started
+      // we need to make sure any classes are loaded with this object's classloader
+      // not the thread context class loader, which is likely application specific
+      // and may otherwise cause a class loader leak when the application is undeployed
+      ContextClassLoaderSwitcher.SwitchContext context = this.switcher.getSwitchContext(this.getClass().getClassLoader());
+
+      try
       {
-         GlobalConfiguration globalConfig = this.container.getGlobalConfiguration();
-         
-         // Create unique cluster name using cache name
-         String clusterName = globalConfig.getClusterName() + "/" + cacheName;
-         
-         EmbeddedCacheManager container = this.containers.get(clusterName);
-         
-         if (container == null)
+         if (config.getCacheMode().isDistributed())
          {
-            // Workaround for ISPN-744
-            GlobalConfiguration global = new GlobalConfiguration();
-            global.setAllowDuplicateDomains(globalConfig.isAllowDuplicateDomains());
-            global.setAsyncListenerExecutorFactoryClass(globalConfig.getAsyncListenerExecutorFactoryClass());
-            global.setAsyncListenerExecutorProperties(globalConfig.getAsyncListenerExecutorProperties());
-            global.setAsyncTransportExecutorFactoryClass(globalConfig.getAsyncTransportExecutorFactoryClass());
-            global.setAsyncTransportExecutorProperties(globalConfig.getAsyncTransportExecutorProperties());
-            global.setCacheManagerName(globalConfig.getCacheManagerName() + "/" + cacheName);
-            global.setClusterName(clusterName);
-            global.setDistributedSyncTimeout(globalConfig.getDistributedSyncTimeout());
-            global.setEvictionScheduledExecutorFactoryClass(globalConfig.getEvictionScheduledExecutorFactoryClass());
-            global.setEvictionScheduledExecutorProperties(globalConfig.getEvictionScheduledExecutorProperties());
-            global.setExposeGlobalJmxStatistics(globalConfig.isExposeGlobalJmxStatistics());
-            global.setJmxDomain(globalConfig.getJmxDomain());
-            global.setMachineId(globalConfig.getMachineId());
-            global.setMarshallerClass(globalConfig.getMarshallerClass());
-            global.setMarshallVersion(globalConfig.getMarshallVersion());
-            global.setMBeanServerLookup(globalConfig.getMBeanServerLookup());
-            global.setRackId(globalConfig.getRackId());
-            global.setReplicationQueueScheduledExecutorFactoryClass(globalConfig.getReplicationQueueScheduledExecutorFactoryClass());
-            global.setReplicationQueueScheduledExecutorProperties(globalConfig.getReplicationQueueScheduledExecutorProperties());
-            global.setShutdownHookBehavior(globalConfig.getShutdownHookBehavior());
-            global.setSiteId(globalConfig.getSiteId());
-            global.setStrictPeerToPeer(globalConfig.isStrictPeerToPeer());
-            global.setTransportClass(globalConfig.getTransportClass());
-            global.setTransportNodeName(globalConfig.getTransportNodeName());
-
-            Properties properties = new Properties(globalConfig.getTransportProperties());
-            properties.setProperty(DefaultCacheContainerFactory.CHANNEL_ID, clusterName);
-            global.setTransportProperties(properties);
+            GlobalConfiguration globalConfig = this.container.getGlobalConfiguration();
             
-            // Create single use cache manager
-            container = new SingletonCacheManager(this, cacheName, global, config);
+            // Create unique cluster name using cache name
+            String clusterName = globalConfig.getClusterName() + "/" + cacheName;
             
-            EmbeddedCacheManager existing = this.containers.putIfAbsent(clusterName, container);
+            EmbeddedCacheManager container = this.containers.get(clusterName);
             
-            if (existing == null)
+            if (container == null)
             {
-               container.addListener(this);
+               // Workaround for ISPN-744
+               GlobalConfiguration global = new GlobalConfiguration();
+               global.setAllowDuplicateDomains(globalConfig.isAllowDuplicateDomains());
+               global.setAsyncListenerExecutorFactoryClass(globalConfig.getAsyncListenerExecutorFactoryClass());
+               global.setAsyncListenerExecutorProperties(globalConfig.getAsyncListenerExecutorProperties());
+               global.setAsyncTransportExecutorFactoryClass(globalConfig.getAsyncTransportExecutorFactoryClass());
+               global.setAsyncTransportExecutorProperties(globalConfig.getAsyncTransportExecutorProperties());
+               global.setCacheManagerName(globalConfig.getCacheManagerName() + "/" + cacheName);
+               global.setClusterName(clusterName);
+               global.setDistributedSyncTimeout(globalConfig.getDistributedSyncTimeout());
+               global.setEvictionScheduledExecutorFactoryClass(globalConfig.getEvictionScheduledExecutorFactoryClass());
+               global.setEvictionScheduledExecutorProperties(globalConfig.getEvictionScheduledExecutorProperties());
+               global.setExposeGlobalJmxStatistics(globalConfig.isExposeGlobalJmxStatistics());
+               global.setJmxDomain(globalConfig.getJmxDomain());
+               global.setMachineId(globalConfig.getMachineId());
+               global.setMarshallerClass(globalConfig.getMarshallerClass());
+               global.setMarshallVersion(globalConfig.getMarshallVersion());
+               global.setMBeanServerLookup(globalConfig.getMBeanServerLookup());
+               global.setRackId(globalConfig.getRackId());
+               global.setReplicationQueueScheduledExecutorFactoryClass(globalConfig.getReplicationQueueScheduledExecutorFactoryClass());
+               global.setReplicationQueueScheduledExecutorProperties(globalConfig.getReplicationQueueScheduledExecutorProperties());
+               global.setShutdownHookBehavior(globalConfig.getShutdownHookBehavior());
+               global.setSiteId(globalConfig.getSiteId());
+               global.setStrictPeerToPeer(globalConfig.isStrictPeerToPeer());
+               global.setTransportClass(globalConfig.getTransportClass());
+               global.setTransportNodeName(globalConfig.getTransportNodeName());
+   
+               Properties properties = new Properties(globalConfig.getTransportProperties());
+               properties.setProperty(DefaultCacheContainerFactory.CHANNEL_ID, clusterName);
+               global.setTransportProperties(properties);
                
-               for (Object listener: this.container.getListeners())
+               // Create single use cache manager
+               container = new SingletonCacheManager(this, cacheName, global, config);
+               
+               EmbeddedCacheManager existing = this.containers.putIfAbsent(clusterName, container);
+               
+               if (existing == null)
                {
-                  container.addListener(listener);
+                  container.addListener(this);
+                  
+                  for (Object listener: this.container.getListeners())
+                  {
+                     container.addListener(listener);
+                  }
+                  
+                  // The cache manager should stop when the cache stops
+                  container.start();
                }
-               
-               // The cache manager should stop when the cache stops
-               container.start();
+               else
+               {
+                  container = existing;
+               }
             }
-            else
-            {
-               container = existing;
-            }
+            
+            return container.getCache(cacheName);
          }
          
-         return container.getCache(cacheName);
+         return this.container.getCache(cacheName);
       }
-      
-      return this.container.getCache(cacheName);
+      finally
+      {
+         context.reset();
+      }
    }
 
    @Override



More information about the jboss-cvs-commits mailing list