[jboss-cvs] JBossAS SVN: r108440 - in projects/cluster/ha-server-ispn/trunk/src: test/java/org/jboss/ha/ispn and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Oct 4 15:58:49 EDT 2010
Author: pferraro
Date: 2010-10-04 15:58:49 -0400 (Mon, 04 Oct 2010)
New Revision: 108440
Added:
projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainer.java
projects/cluster/ha-server-ispn/trunk/src/test/java/org/jboss/ha/ispn/DefaultCacheContainerTest.java
Modified:
projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainerFactory.java
Log:
Workaround for ISPN-658
Added: 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 (rev 0)
+++ projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainer.java 2010-10-04 19:58:49 UTC (rev 108440)
@@ -0,0 +1,211 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, 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.ispn;
+
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import org.infinispan.Cache;
+import org.infinispan.config.Configuration;
+import org.infinispan.config.GlobalConfiguration;
+import org.infinispan.lifecycle.ComponentStatus;
+import org.infinispan.manager.DefaultCacheManager;
+import org.infinispan.manager.EmbeddedCacheManager;
+import org.infinispan.notifications.Listener;
+import org.infinispan.notifications.cachemanagerlistener.annotation.CacheStopped;
+import org.infinispan.notifications.cachemanagerlistener.event.CacheStoppedEvent;
+import org.infinispan.remoting.transport.Address;
+
+/**
+ * Cache manager wrapper to work around ISPN-658
+ * @author Paul Ferraro
+ */
+ at Listener
+public class DefaultCacheContainer implements EmbeddedCacheManager
+{
+ private final EmbeddedCacheManager container;
+ private final ConcurrentMap<String, EmbeddedCacheManager> containers = new ConcurrentHashMap<String, EmbeddedCacheManager>();
+
+ public DefaultCacheContainer(EmbeddedCacheManager manager)
+ {
+ this.container = manager;
+ }
+
+ @Override
+ public <K, V> Cache<K, V> getCache()
+ {
+ return this.container.getCache();
+ }
+
+ @Override
+ public <K, V> Cache<K, V> getCache(String cacheName)
+ {
+ // Until ISPN-658 is fixed, we need to create a separate adhoc cache manager if requested cache uses DIST mode.
+ Configuration config = this.container.defineConfiguration(cacheName, new Configuration());
+
+ if (config.getCacheMode().isDistributed())
+ {
+ 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)
+ {
+ GlobalConfiguration global = globalConfig.clone();
+ global.setClusterName(clusterName);
+ Properties properties = new Properties(globalConfig.getTransportProperties());
+ properties.setProperty("clusterId", clusterName);
+ global.setTransportProperties(properties);
+
+ // Create single use cache manager
+ container = new DefaultCacheManager(global, config, false);
+
+ EmbeddedCacheManager existing = this.containers.putIfAbsent(clusterName, container);
+
+ if (existing == null)
+ {
+ for (Object listener: this.container.getListeners())
+ {
+ container.addListener(listener);
+ }
+
+ // The cache manager should stop when the cache stops
+ container.addListener(this);
+ container.start();
+ }
+ else
+ {
+ container = existing;
+ }
+ }
+
+ return container.getCache();
+ }
+
+ return this.container.getCache(cacheName);
+ }
+
+ @Override
+ public void start()
+ {
+ this.container.start();
+ }
+
+ @Override
+ public void stop()
+ {
+ this.container.stop();
+ }
+
+ @Override
+ public void addListener(Object listener)
+ {
+ this.container.addListener(listener);
+ }
+
+ @Override
+ public void removeListener(Object listener)
+ {
+ this.container.removeListener(listener);
+ }
+
+ @Override
+ public Set<Object> getListeners()
+ {
+ return this.container.getListeners();
+ }
+
+ @Override
+ public Configuration defineConfiguration(String cacheName, Configuration configurationOverride)
+ {
+ return this.container.defineConfiguration(cacheName, configurationOverride);
+ }
+
+ @Override
+ public Configuration defineConfiguration(String cacheName, String templateCacheName, Configuration configurationOverride)
+ {
+ return this.container.defineConfiguration(cacheName, templateCacheName, configurationOverride);
+ }
+
+ @Override
+ public String getClusterName()
+ {
+ return this.container.getClusterName();
+ }
+
+ @Override
+ public List<Address> getMembers()
+ {
+ return this.container.getMembers();
+ }
+
+ @Override
+ public Address getAddress()
+ {
+ return this.container.getAddress();
+ }
+
+ @Override
+ public boolean isCoordinator()
+ {
+ return this.container.isCoordinator();
+ }
+
+ @Override
+ public ComponentStatus getStatus()
+ {
+ return this.container.getStatus();
+ }
+
+ @Override
+ public GlobalConfiguration getGlobalConfiguration()
+ {
+ return this.container.getGlobalConfiguration();
+ }
+
+ @Override
+ public Configuration getDefaultConfiguration()
+ {
+ return this.container.getDefaultConfiguration();
+ }
+
+ @Override
+ public Set<String> getCacheNames()
+ {
+ return this.container.getCacheNames();
+ }
+
+ @CacheStopped
+ public void cacheStopped(CacheStoppedEvent event)
+ {
+ EmbeddedCacheManager manager = event.getCacheManager();
+ this.containers.remove(manager.getClusterName());
+ // Stop the cache manager when its cache stops
+ manager.stop();
+ }
+}
Modified: projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainerFactory.java
===================================================================
--- projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainerFactory.java 2010-10-04 18:52:27 UTC (rev 108439)
+++ projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/DefaultCacheContainerFactory.java 2010-10-04 19:58:49 UTC (rev 108440)
@@ -83,7 +83,7 @@
manager.defineConfiguration(config.getName(), config);
}
- return manager;
+ return new DefaultCacheContainer(manager);
}
/**
Added: projects/cluster/ha-server-ispn/trunk/src/test/java/org/jboss/ha/ispn/DefaultCacheContainerTest.java
===================================================================
--- projects/cluster/ha-server-ispn/trunk/src/test/java/org/jboss/ha/ispn/DefaultCacheContainerTest.java (rev 0)
+++ projects/cluster/ha-server-ispn/trunk/src/test/java/org/jboss/ha/ispn/DefaultCacheContainerTest.java 2010-10-04 19:58:49 UTC (rev 108440)
@@ -0,0 +1,430 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, 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.ispn;
+
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
+import junit.framework.Assert;
+
+import org.easymock.EasyMock;
+import org.easymock.IMocksControl;
+import org.infinispan.Cache;
+import org.infinispan.config.Configuration;
+import org.infinispan.config.Configuration.CacheMode;
+import org.infinispan.config.GlobalConfiguration;
+import org.infinispan.lifecycle.ComponentStatus;
+import org.infinispan.manager.CacheContainer;
+import org.infinispan.manager.EmbeddedCacheManager;
+import org.infinispan.notifications.Listener;
+import org.infinispan.notifications.cachemanagerlistener.annotation.CacheStarted;
+import org.infinispan.notifications.cachemanagerlistener.event.CacheStartedEvent;
+import org.infinispan.remoting.transport.Address;
+import org.junit.Test;
+
+public class DefaultCacheContainerTest
+{
+ private final IMocksControl control = EasyMock.createStrictControl();
+ private final EmbeddedCacheManager manager = this.control.createMock(EmbeddedCacheManager.class);
+
+ private final EmbeddedCacheManager container = new DefaultCacheContainer(this.manager);
+
+ @Test
+ public void getDefaultCache()
+ {
+ @SuppressWarnings("unchecked")
+ Cache<Object, Object> cache = this.control.createMock(Cache.class);
+
+ EasyMock.expect(this.manager.<Object, Object>getCache()).andReturn(cache);
+
+ this.control.replay();
+
+ Cache<Object, Object> result = this.container.getCache();
+
+ this.control.verify();
+
+ Assert.assertSame(cache, result);
+
+ this.control.reset();
+ }
+
+ @Test
+ public void getCache()
+ {
+ this.getCache(CacheMode.DIST_ASYNC, true);
+ this.getCache(CacheMode.DIST_SYNC, true);
+ this.getCache(CacheMode.INVALIDATION_ASYNC, false);
+ this.getCache(CacheMode.INVALIDATION_SYNC, false);
+ this.getCache(CacheMode.LOCAL, false);
+ this.getCache(CacheMode.REPL_ASYNC, false);
+ this.getCache(CacheMode.REPL_SYNC, false);
+ }
+
+ private void getCache(CacheMode mode, boolean singleton)
+ {
+ String cacheName = "test";
+ Configuration config = new Configuration();
+ config.setCacheMode(mode);
+ @SuppressWarnings("unchecked")
+ Cache<Object, Object> cache = this.control.createMock(Cache.class);
+ GlobalConfiguration global = GlobalConfiguration.getClusteredDefault();
+ global.setClusterName("cluster");
+ Object listener = new DummyListener();
+
+ EasyMock.expect(this.manager.defineConfiguration(cacheName, new Configuration())).andReturn(config);
+
+ if (singleton)
+ {
+ EasyMock.expect(this.manager.getGlobalConfiguration()).andReturn(global);
+ EasyMock.expect(this.manager.getListeners()).andReturn(Collections.singleton(listener));
+ }
+ else
+ {
+ EasyMock.expect(this.manager.getCache(cacheName)).andReturn(cache);
+ }
+
+ this.control.replay();
+
+ Cache<Object, Object> result = this.container.getCache(cacheName);
+
+ this.control.verify();
+
+ if (singleton)
+ {
+ Assert.assertNotSame(cache, result);
+
+ EmbeddedCacheManager manager = (EmbeddedCacheManager) result.getCacheManager();
+ Assert.assertEquals(config, manager.getDefaultConfiguration());
+ GlobalConfiguration globalConfig = manager.getGlobalConfiguration();
+ Assert.assertEquals("cluster/test", globalConfig.getClusterName());
+ Assert.assertEquals("cluster/test", globalConfig.getTransportProperties().getProperty("clusterId"));
+ Assert.assertEquals(CacheContainer.DEFAULT_CACHE_NAME, result.getName());
+ Set<Object> listeners = manager.getListeners();
+ Assert.assertTrue(listeners.contains(this.container));
+ Assert.assertTrue(listeners.contains(listener));
+ }
+ else
+ {
+ Assert.assertSame(cache, result);
+ }
+
+ this.control.reset();
+
+ // Validate 2nd attempt
+
+ EasyMock.expect(this.manager.defineConfiguration(cacheName, new Configuration())).andReturn(config);
+
+ if (singleton)
+ {
+ EasyMock.expect(this.manager.getGlobalConfiguration()).andReturn(global);
+ }
+ else
+ {
+ EasyMock.expect(this.manager.getCache(cacheName)).andReturn(cache);
+ }
+
+ this.control.replay();
+
+ Cache<Object, Object> result2 = this.container.getCache(cacheName);
+
+ this.control.verify();
+
+ Assert.assertSame(result, result2);
+
+ this.control.reset();
+
+ if (singleton)
+ {
+ result.stop();
+ }
+ }
+
+ @Test
+ public void start()
+ {
+ this.manager.start();
+
+ this.control.replay();
+
+ this.container.start();
+
+ this.control.verify();
+ this.control.reset();
+ }
+
+ @Test
+ public void stop()
+ {
+ this.manager.stop();
+
+ this.control.replay();
+
+ this.container.stop();
+
+ this.control.verify();
+ this.control.reset();
+ }
+
+ @Test
+ public void addListener()
+ {
+ Object listener = new Object();
+
+ this.manager.addListener(EasyMock.same(listener));
+
+ this.control.replay();
+
+ this.container.addListener(listener);
+
+ this.control.verify();
+ this.control.reset();
+ }
+
+ @Test
+ public void removeListener()
+ {
+ Object listener = new Object();
+
+ this.manager.removeListener(EasyMock.same(listener));
+
+ this.control.replay();
+
+ this.container.removeListener(listener);
+
+ this.control.verify();
+ this.control.reset();
+ }
+
+ @Test
+ public void getListeners()
+ {
+ Set<Object> expected = new TreeSet<Object>();
+
+ EasyMock.expect(this.manager.getListeners()).andReturn(expected);
+
+ this.control.replay();
+
+ Set<Object> result = this.container.getListeners();
+
+ this.control.verify();
+
+ Assert.assertSame(expected, result);
+
+ this.control.reset();
+ }
+
+ @Test
+ public void defineConfiguration()
+ {
+ String cacheName = "test";
+ Configuration config = new Configuration();
+ Configuration expected = new Configuration();
+
+ EasyMock.expect(this.manager.defineConfiguration(EasyMock.same(cacheName), EasyMock.same(config))).andReturn(expected);
+
+ this.control.replay();
+
+ Configuration result = this.container.defineConfiguration(cacheName, config);
+
+ this.control.verify();
+
+ Assert.assertSame(expected, result);
+
+ this.control.reset();
+ }
+
+ @Test
+ public void defineConfigurationFromTemplate()
+ {
+ String cacheName = "test";
+ String templateCacheName = "test";
+ Configuration config = new Configuration();
+ Configuration expected = new Configuration();
+
+ EasyMock.expect(this.manager.defineConfiguration(EasyMock.same(cacheName), EasyMock.same(templateCacheName), EasyMock.same(config))).andReturn(expected);
+
+ this.control.replay();
+
+ Configuration result = this.container.defineConfiguration(cacheName, templateCacheName, config);
+
+ this.control.verify();
+
+ Assert.assertSame(expected, result);
+
+ this.control.reset();
+ }
+
+ @Test
+ public void getClusterName()
+ {
+ String expected = "cluster";
+
+ EasyMock.expect(this.manager.getClusterName()).andReturn(expected);
+
+ this.control.replay();
+
+ String result = this.container.getClusterName();
+
+ this.control.verify();
+
+ Assert.assertSame(expected, result);
+
+ this.control.reset();
+ }
+
+ @Test
+ public void getMembers()
+ {
+ List<Address> expected = new LinkedList<Address>();
+
+ EasyMock.expect(this.manager.getMembers()).andReturn(expected);
+
+ this.control.replay();
+
+ List<Address> result = this.container.getMembers();
+
+ this.control.verify();
+
+ Assert.assertSame(expected, result);
+
+ this.control.reset();
+ }
+
+ @Test
+ public void getAddress()
+ {
+ Address expected = this.control.createMock(Address.class);
+
+ EasyMock.expect(this.manager.getAddress()).andReturn(expected);
+
+ this.control.replay();
+
+ Address result = this.container.getAddress();
+
+ this.control.verify();
+
+ Assert.assertSame(expected, result);
+
+ this.control.reset();
+ }
+
+ @Test
+ public void isCoordinator()
+ {
+ EasyMock.expect(this.manager.isCoordinator()).andReturn(true);
+
+ this.control.replay();
+
+ boolean result = this.container.isCoordinator();
+
+ this.control.verify();
+
+ Assert.assertTrue(result);
+
+ this.control.reset();
+ }
+
+ @Test
+ public void getStatus()
+ {
+ ComponentStatus expected = ComponentStatus.FAILED;
+
+ EasyMock.expect(this.manager.getStatus()).andReturn(expected);
+
+ this.control.replay();
+
+ ComponentStatus result = this.container.getStatus();
+
+ this.control.verify();
+
+ Assert.assertSame(expected, result);
+
+ this.control.reset();
+ }
+
+ @Test
+ public void getGlobalConfiguration()
+ {
+ GlobalConfiguration expected = new GlobalConfiguration();
+
+ EasyMock.expect(this.manager.getGlobalConfiguration()).andReturn(expected);
+
+ this.control.replay();
+
+ GlobalConfiguration result = this.container.getGlobalConfiguration();
+
+ this.control.verify();
+
+ Assert.assertSame(expected, result);
+
+ this.control.reset();
+ }
+
+ @Test
+ public void getDefaultConfiguration()
+ {
+ Configuration expected = new Configuration();
+
+ EasyMock.expect(this.manager.getDefaultConfiguration()).andReturn(expected);
+
+ this.control.replay();
+
+ Configuration result = this.container.getDefaultConfiguration();
+
+ this.control.verify();
+
+ Assert.assertSame(expected, result);
+
+ this.control.reset();
+ }
+
+ @Test
+ public void getCacheNames()
+ {
+ Set<String> expected = new TreeSet<String>();
+
+ EasyMock.expect(this.manager.getCacheNames()).andReturn(expected);
+
+ this.control.replay();
+
+ Set<String> result = this.container.getCacheNames();
+
+ this.control.verify();
+
+ Assert.assertSame(expected, result);
+
+ this.control.reset();
+ }
+
+ @Listener
+ public static class DummyListener
+ {
+ @CacheStarted
+ public void cacheStarted(CacheStartedEvent event)
+ {
+ // Do nothing
+ }
+ }
+}
More information about the jboss-cvs-commits
mailing list