From 3205db109f914dde844573352f30a8ef4a08c066 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Fri, 8 Jul 2011 17:53:15 +0300 Subject: [PATCH] + retrofitted client cache onto the master + the pool/client cache loop still exists --- .../data/gemfire/CacheFactoryBean.java | 48 +++++++++++++---- .../client/ClientCacheFactoryBean.java | 53 +++++++++---------- .../data/gemfire/client/PoolFactoryBean.java | 2 +- .../data/gemfire/client/client-cache.xml | 2 +- 4 files changed, 65 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java index ee0751cd..c6f5f4e9 100644 --- a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java @@ -38,6 +38,7 @@ import com.gemstone.gemfire.GemFireException; import com.gemstone.gemfire.cache.Cache; import com.gemstone.gemfire.cache.CacheClosedException; import com.gemstone.gemfire.cache.CacheFactory; +import com.gemstone.gemfire.cache.client.ClientCacheFactory; import com.gemstone.gemfire.distributed.DistributedMember; import com.gemstone.gemfire.distributed.DistributedSystem; import com.gemstone.gemfire.pdx.PdxSerializable; @@ -61,19 +62,33 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl private class PdxOptions implements Runnable { - private CacheFactory factory; + private Object factory; - PdxOptions(CacheFactory factory) { + PdxOptions(Object factory) { this.factory = factory; } public void run() { Assert.isAssignable(PdxSerializer.class, pdxSerializer.getClass(), "Invalid pdx serializer used"); - factory.setPdxSerializer((PdxSerializer) pdxSerializer); - factory.setPdxDiskStore(pdxDiskStoreName); - factory.setPdxIgnoreUnreadFields(pdxIgnoreUnreadFields); - factory.setPdxPersistent(pdxPersistent); - factory.setPdxReadSerialized(pdxReadSerialized); + + // ugly duplication to go around the different factory hierarchy + if (factory instanceof CacheFactory) { + CacheFactory cf = (CacheFactory) factory; + + cf.setPdxSerializer((PdxSerializer) pdxSerializer); + cf.setPdxDiskStore(pdxDiskStoreName); + cf.setPdxIgnoreUnreadFields(pdxIgnoreUnreadFields); + cf.setPdxPersistent(pdxPersistent); + cf.setPdxReadSerialized(pdxReadSerialized); + } + if (factory instanceof ClientCacheFactory) { + ClientCacheFactory cf = (ClientCacheFactory) factory; + cf.setPdxSerializer((PdxSerializer) pdxSerializer); + cf.setPdxDiskStore(pdxDiskStoreName); + cf.setPdxIgnoreUnreadFields(pdxIgnoreUnreadFields); + cf.setPdxPersistent(pdxPersistent); + cf.setPdxReadSerialized(pdxReadSerialized); + } } } @@ -104,7 +119,6 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl factoryLocator.afterPropertiesSet(); } Properties cfgProps = mergeProperties(); - CacheFactory factory = new CacheFactory(cfgProps); // use the bean class loader to load Declarable classes Thread th = Thread.currentThread(); @@ -115,9 +129,11 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl // first look for open caches String msg = null; try { - cache = CacheFactory.getAnyInstance(); + cache = fetchCache(); msg = "Retrieved existing"; } catch (CacheClosedException ex) { + Object factory = createFactory(cfgProps); + // GemFire 6.6 specific options if (pdxSerializer != null || pdxPersistent != null || pdxReadSerialized != null || pdxIgnoreUnreadFields != null || pdxDiskStoreName != null) { @@ -127,7 +143,7 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl } // fall back to cache creation - cache = factory.create(); + cache = createCache(factory); msg = "Created"; } @@ -151,6 +167,18 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl } } + protected Object createFactory(Properties props) { + return new CacheFactory(props); + } + + protected Cache fetchCache() { + return CacheFactory.getAnyInstance(); + } + + protected Cache createCache(Object factory) { + return ((CacheFactory) factory).create(); + } + private Properties mergeProperties() { Properties cfgProps = (properties != null ? (Properties) properties.clone() : new Properties()); return cfgProps; diff --git a/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java b/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java index f885290e..37a6f991 100644 --- a/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java @@ -16,55 +16,52 @@ package org.springframework.data.gemfire.client; -import java.util.concurrent.Callable; +import java.util.Properties; -import org.springframework.beans.factory.BeanFactory; import org.springframework.data.gemfire.CacheFactoryBean; import org.springframework.util.Assert; import com.gemstone.gemfire.cache.Cache; import com.gemstone.gemfire.cache.client.ClientCacheFactory; import com.gemstone.gemfire.cache.client.Pool; -import com.gemstone.gemfire.distributed.DistributedSystem; /** * FactoryBean dedicated to creating client caches (caches for client JVMs). - * Intended mainly for usage with GemFire 6.5 (while preserving 6.0 compatibility). + * Acts an utility class (as client caches are a subset with a particular configuration of the generic cache). * * @author Costin Leau */ public class ClientCacheFactoryBean extends CacheFactoryBean { private String poolName; - private boolean GEMFIRE65 = false; @Override - protected Cache createCache(final DistributedSystem system) throws Exception { - initPool(); - - if (GEMFIRE65) { - Callable call = new Callable() { - public Cache call() throws Exception { - return (Cache) new ClientCacheFactory(system.getProperties()).create(); - } - }; - return call.call(); - } - return super.createCache(system); + protected Cache createCache(Object factory) { + return (Cache) ((ClientCacheFactory) factory).create(); } - private void initPool() { - BeanFactory beanFactory = getBeanFactory(); - - // try to eagerly initialize the pool name, if defined as a bean - if (beanFactory.isTypeMatch(poolName, Pool.class)) { - if (log.isDebugEnabled()) { - log.debug("Found bean definition for pool '" + poolName + "'. Eagerly initializing it..."); - } - beanFactory.getBean(poolName, Pool.class); - } + @Override + protected Object createFactory(Properties props) { + return new ClientCacheFactory(props); } + @Override + protected Cache fetchCache() { + return (Cache) ClientCacheFactory.getAnyInstance(); + } + + // private void initPool() { + // BeanFactory beanFactory = getBeanFactory(); + // + // // try to eagerly initialize the pool name, if defined as a bean + // if (beanFactory.isTypeMatch(poolName, Pool.class)) { + // if (log.isDebugEnabled()) { + // log.debug("Found bean definition for pool '" + poolName + "'. Eagerly initializing it..."); + // } + // beanFactory.getBean(poolName, Pool.class); + // } + // } + /** * Sets the pool name used by this client. * @@ -84,4 +81,4 @@ public class ClientCacheFactoryBean extends CacheFactoryBean { Assert.notNull(pool, "pool cannot be null"); setPoolName(pool.getName()); } -} +} \ No newline at end of file diff --git a/src/main/java/org/springframework/data/gemfire/client/PoolFactoryBean.java b/src/main/java/org/springframework/data/gemfire/client/PoolFactoryBean.java index 9bab6b96..fcbdfc94 100644 --- a/src/main/java/org/springframework/data/gemfire/client/PoolFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/client/PoolFactoryBean.java @@ -55,7 +55,7 @@ public class PoolFactoryBean implements FactoryBean, InitializingBean, Dis private static final Log log = LogFactory.getLog(PoolFactoryBean.class); - // whether the pool has been created internaly or not + // whether the pool has been created internally or not private boolean internalPool = true; private Pool pool; diff --git a/src/test/resources/org/springframework/data/gemfire/client/client-cache.xml b/src/test/resources/org/springframework/data/gemfire/client/client-cache.xml index e4120de8..748ce520 100644 --- a/src/test/resources/org/springframework/data/gemfire/client/client-cache.xml +++ b/src/test/resources/org/springframework/data/gemfire/client/client-cache.xml @@ -5,7 +5,7 @@ xsi:schemaLocation="http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - +