From 29e094ea558522ce276b2b4a49063a1ecbe1339c Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Fri, 26 Aug 2011 13:05:01 +0300 Subject: [PATCH] SGF-63 + introduce RegionFactory in RegionFactoryBean --- .../data/gemfire/RegionFactoryBean.java | 34 ++++++++++++++----- .../data/gemfire/client/client-cache.xml | 6 +++- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/springframework/data/gemfire/RegionFactoryBean.java b/src/main/java/org/springframework/data/gemfire/RegionFactoryBean.java index dcfebb18..83f1adf4 100644 --- a/src/main/java/org/springframework/data/gemfire/RegionFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/RegionFactoryBean.java @@ -16,11 +16,14 @@ package org.springframework.data.gemfire; +import java.lang.reflect.Field; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.DisposableBean; import org.springframework.core.io.Resource; import org.springframework.util.ObjectUtils; +import org.springframework.util.ReflectionUtils; import com.gemstone.gemfire.cache.AttributesFactory; import com.gemstone.gemfire.cache.Cache; @@ -31,6 +34,7 @@ import com.gemstone.gemfire.cache.CacheWriter; import com.gemstone.gemfire.cache.DataPolicy; import com.gemstone.gemfire.cache.Region; import com.gemstone.gemfire.cache.RegionAttributes; +import com.gemstone.gemfire.cache.RegionFactory; import com.gemstone.gemfire.cache.Scope; /** @@ -68,33 +72,35 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple if (attributes != null) AttributesFactory.validateAttributes(attributes); - AttributesFactory attrFactory = (attributes != null ? new AttributesFactory(attributes) - : new AttributesFactory()); + RegionFactory regionFactory = (attributes != null ? cache.createRegionFactory(attributes) + : cache. createRegionFactory()); + if (!ObjectUtils.isEmpty(cacheListeners)) { for (CacheListener listener : cacheListeners) { - attrFactory.addCacheListener(listener); + regionFactory.addCacheListener(listener); } } if (cacheLoader != null) { - attrFactory.setCacheLoader(cacheLoader); + regionFactory.setCacheLoader(cacheLoader); } if (cacheWriter != null) { - attrFactory.setCacheWriter(cacheWriter); + regionFactory.setCacheWriter(cacheWriter); } if (dataPolicy != null) { - attrFactory.setDataPolicy(dataPolicy); + regionFactory.setDataPolicy(dataPolicy); } if (scope != null) { - attrFactory.setScope(scope); + regionFactory.setScope(scope); } - postProcess(attrFactory); + // get underlying AttributesFactory + postProcess(findAttrFactory(regionFactory)); - Region reg = cache.createRegion(regionName, attrFactory.create()); + Region reg = regionFactory.create(regionName); log.info("Created new cache region [" + regionName + "]"); if (snapshot != null) { reg.loadSnapshot(snapshot.getInputStream()); @@ -103,13 +109,23 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple return reg; } + @SuppressWarnings("unchecked") + private AttributesFactory findAttrFactory(RegionFactory regionFactory) { + Field attrField = ReflectionUtils.findField(RegionFactory.class, "attrFactory", AttributesFactory.class); + ReflectionUtils.makeAccessible(attrField); + return (AttributesFactory) ReflectionUtils.getField(attrField, regionFactory); + } + + /** * Post-process the attribute factory object used for configuring the region of this factory bean during the initialization process. * The object is already initialized and configured by the factory bean before this method * is invoked. * * @param attrFactory attribute factory + * @deprecated as of GemFire 6.5, the use of {@link AttributesFactory} has been deprecated */ + @Deprecated protected void postProcess(AttributesFactory attrFactory) { } 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 a9372eaf..4534f6a7 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 @@ -4,10 +4,14 @@ xmlns:gfe="http://www.springframework.org/schema/gemfire" xsi:schemaLocation="http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire-1.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - + + + +