demonstrate problem trying to create subregions
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.springframework.data.gemfire;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -28,6 +29,7 @@ import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import com.gemstone.gemfire.cache.AttributesFactory;
|
||||
import com.gemstone.gemfire.cache.AttributesMutator;
|
||||
import com.gemstone.gemfire.cache.Cache;
|
||||
import com.gemstone.gemfire.cache.CacheClosedException;
|
||||
import com.gemstone.gemfire.cache.CacheListener;
|
||||
@@ -64,6 +66,7 @@ public class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V> imple
|
||||
private Scope scope;
|
||||
private DataPolicy dataPolicy;
|
||||
private Region<K, V> region;
|
||||
private List<Region<?, ?>> subRegions;
|
||||
|
||||
|
||||
@Override
|
||||
@@ -73,7 +76,6 @@ public class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V> imple
|
||||
postProcess(region);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Region<K, V> lookupFallback(GemFireCache cache, String regionName) throws Exception {
|
||||
Assert.isTrue(cache instanceof Cache, "Unable to create regions from " + cache);
|
||||
@@ -85,6 +87,7 @@ public class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V> imple
|
||||
|
||||
final RegionFactory<K, V> regionFactory = (attributes != null ? c.createRegionFactory(attributes)
|
||||
: c.<K, V> createRegionFactory());
|
||||
|
||||
|
||||
if (!ObjectUtils.isEmpty(cacheListeners)) {
|
||||
for (CacheListener<K, V> listener : cacheListeners) {
|
||||
@@ -110,12 +113,16 @@ public class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V> imple
|
||||
|
||||
// get underlying AttributesFactory
|
||||
postProcess(findAttrFactory(regionFactory));
|
||||
|
||||
|
||||
Region<K, V> reg = regionFactory.create(regionName);
|
||||
log.info("Created new cache region [" + regionName + "]");
|
||||
if (snapshot != null) {
|
||||
reg.loadSnapshot(snapshot.getInputStream());
|
||||
}
|
||||
|
||||
if (subRegions != null) {
|
||||
System.out.println("**********************************************" + subRegions.get(0));
|
||||
}
|
||||
|
||||
return reg;
|
||||
}
|
||||
@@ -225,6 +232,12 @@ public class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V> imple
|
||||
this.cacheListeners = cacheListeners;
|
||||
}
|
||||
|
||||
public void setSubRegions(List<Region<?, ?>> subRegions) {
|
||||
log.info("setting subRegions");
|
||||
this.subRegions = subRegions;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the cache loader used for the region used by this factory.
|
||||
* Used only when a new region is created.Overrides the settings
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.springframework.data.gemfire;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.gemstone.gemfire.cache.RegionAttributes;
|
||||
/**
|
||||
*
|
||||
* @author David Turanski
|
||||
*
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
*/
|
||||
public class SubRegion<K, V> {
|
||||
private final RegionAttributes<K, V> regionAttributes;
|
||||
private final String regionName;
|
||||
private List<SubRegion<?,?>> subRegions;
|
||||
|
||||
public SubRegion(String regionName, RegionAttributes<K, V> regionAttributes) {
|
||||
this.regionAttributes = regionAttributes;
|
||||
this.regionName = regionName;
|
||||
}
|
||||
|
||||
public RegionAttributes<K, V> getRegionAttributes() {
|
||||
return regionAttributes;
|
||||
}
|
||||
|
||||
public String getRegionName() {
|
||||
return regionName;
|
||||
}
|
||||
|
||||
public List<SubRegion<?, ?>> getSubRegions() {
|
||||
return subRegions;
|
||||
}
|
||||
public void setSubRegions(List<SubRegion<?, ?>> subRegions) {
|
||||
this.subRegions = subRegions;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.springframework.data.gemfire;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
import com.gemstone.gemfire.cache.AttributesFactory;
|
||||
|
||||
public class SubRegionFactoryBean<K,V> extends AttributesFactory<K,V> implements FactoryBean<SubRegion<K, V>>, InitializingBean {
|
||||
private String name;
|
||||
private SubRegion<K,V> subRegion;
|
||||
private String regionName;
|
||||
private List<SubRegion<?,?>> subRegions;
|
||||
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setSubRegions(List<SubRegion<?, ?>> subRegions) {
|
||||
this.subRegions = subRegions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.subRegion = new SubRegion<K,V>(name,create());
|
||||
this.subRegion.setSubRegions(this.subRegions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SubRegion<K, V> getObject() throws Exception {
|
||||
return this.subRegion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return SubRegion.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,6 +21,8 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.data.gemfire.RegionFactoryBean;
|
||||
import org.springframework.data.gemfire.SubRegionFactoryBean;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
@@ -32,10 +34,20 @@ import org.w3c.dom.Element;
|
||||
*/
|
||||
abstract class AliasReplacingBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
|
||||
|
||||
protected Class<?> getBeanClass(Element element) {
|
||||
if (element.hasAttribute("subregion")){
|
||||
System.out.println("building subregion " + element.getAttribute(NAME_ATTRIBUTE));
|
||||
return SubRegionFactoryBean.class;
|
||||
} else {
|
||||
return RegionFactoryBean.class;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
|
||||
ParsingUtils.addBeanAliasAsMetadata(element, builder);
|
||||
|
||||
|
||||
doParseInternal(element, parserContext, builder);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,10 +18,15 @@ package org.springframework.data.gemfire.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.ManagedList;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.data.gemfire.RegionAttributesFactoryBean;
|
||||
import org.springframework.data.gemfire.RegionFactoryBean;
|
||||
import org.springframework.data.gemfire.SubRegion;
|
||||
import org.springframework.data.gemfire.SubRegionFactoryBean;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.xml.DomUtils;
|
||||
import org.w3c.dom.Element;
|
||||
@@ -36,14 +41,15 @@ import com.gemstone.gemfire.cache.Scope;
|
||||
*/
|
||||
class ReplicatedRegionParser extends AliasReplacingBeanDefinitionParser {
|
||||
|
||||
protected Class<?> getBeanClass(Element element) {
|
||||
return RegionFactoryBean.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doParseInternal(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
super.doParse(element, builder);
|
||||
System.out.println("building class " +
|
||||
builder.getBeanDefinition().getBeanClassName());
|
||||
|
||||
boolean subRegion = element.hasAttribute("subregion");
|
||||
|
||||
// set the data policy
|
||||
String attr = element.getAttribute("persistent");
|
||||
if (Boolean.parseBoolean(attr)) {
|
||||
@@ -57,12 +63,15 @@ class ReplicatedRegionParser extends AliasReplacingBeanDefinitionParser {
|
||||
|
||||
ParsingUtils.setPropertyValue(element, builder, "name", "name");
|
||||
|
||||
attr = element.getAttribute("cache-ref");
|
||||
// add cache reference (fallback to default if nothing is specified)
|
||||
builder.addPropertyReference("cache", (StringUtils.hasText(attr) ? attr : "gemfire-cache"));
|
||||
|
||||
BeanDefinitionBuilder attrBuilder = builder;
|
||||
|
||||
if (!subRegion){
|
||||
attr = element.getAttribute("cache-ref");
|
||||
// add cache reference (fallback to default if nothing is specified)
|
||||
builder.addPropertyReference("cache", (StringUtils.hasText(attr) ? attr : "gemfire-cache"));
|
||||
attrBuilder = BeanDefinitionBuilder.genericBeanDefinition(RegionAttributesFactoryBean.class);
|
||||
}
|
||||
// add attributes
|
||||
BeanDefinitionBuilder attrBuilder = BeanDefinitionBuilder.genericBeanDefinition(RegionAttributesFactoryBean.class);
|
||||
|
||||
ParsingUtils.parseStatistics(element, attrBuilder);
|
||||
|
||||
@@ -79,7 +88,10 @@ class ReplicatedRegionParser extends AliasReplacingBeanDefinitionParser {
|
||||
|
||||
List<Element> subElements = DomUtils.getChildElements(element);
|
||||
|
||||
// parse nested cache-listener elements
|
||||
ManagedList subRegions = new ManagedList();
|
||||
subRegions.setElementTypeName(SubRegionFactoryBean.class.getName());
|
||||
|
||||
// parse nested elements
|
||||
for (Element subElement : subElements) {
|
||||
String name = subElement.getLocalName();
|
||||
|
||||
@@ -94,6 +106,22 @@ class ReplicatedRegionParser extends AliasReplacingBeanDefinitionParser {
|
||||
else if ("cache-writer".equals(name)) {
|
||||
builder.addPropertyValue("cacheWriter", parseCacheWriter(parserContext, subElement, builder));
|
||||
}
|
||||
//subregion
|
||||
else if (name.endsWith("region")){
|
||||
System.out.println("element type:" + subElement.getSchemaTypeInfo().getTypeName());
|
||||
String parentRegionName = StringUtils.hasLength(element.getAttribute(NAME_ATTRIBUTE))? element.getAttribute(NAME_ATTRIBUTE):
|
||||
element.getAttribute(ID_ATTRIBUTE);
|
||||
|
||||
|
||||
|
||||
BeanDefinition subRegionDef = this.parseSubRegion(subElement, parserContext, parentRegionName,builder.getBeanDefinition());
|
||||
subRegions.add(subRegionDef);
|
||||
System.out.println("parsed subregion " + subRegionDef);
|
||||
}
|
||||
}
|
||||
|
||||
if (!CollectionUtils.isEmpty(subRegions)) {
|
||||
builder.addPropertyValue("subRegions", subRegions);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,4 +136,11 @@ class ReplicatedRegionParser extends AliasReplacingBeanDefinitionParser {
|
||||
private Object parseCacheWriter(ParserContext parserContext, Element subElement, BeanDefinitionBuilder builder) {
|
||||
return ParsingUtils.parseRefOrNestedBeanDeclaration(parserContext, subElement, builder);
|
||||
}
|
||||
|
||||
private BeanDefinition parseSubRegion(Element element, ParserContext parserContext, String parentRegionName,
|
||||
BeanDefinition parentDefinition) {
|
||||
element.setAttribute("subregion", "true");
|
||||
BeanDefinition beanDefinition = parserContext.getDelegate().parseCustomElement(element, parentDefinition);
|
||||
return beanDefinition;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,13 @@
|
||||
package org.springframework.data.gemfire;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class SubRegionFactoryBeanTest {
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
SubRegionFactoryBean srfb = new SubRegionFactoryBean();
|
||||
srfb.setName("child");
|
||||
srfb.afterPropertiesSet();
|
||||
SubRegion sr = srfb.getObject();
|
||||
}
|
||||
}
|
||||
@@ -18,15 +18,18 @@ package org.springframework.data.gemfire.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.gemfire.RegionFactoryBean;
|
||||
import org.springframework.data.gemfire.RegionLookupFactoryBean;
|
||||
import org.springframework.data.gemfire.SubRegion;
|
||||
import org.springframework.data.gemfire.TestUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -87,4 +90,10 @@ public class ReplicatedRegionNamespaceTest {
|
||||
assertEquals("existing", TestUtils.readField("name", lfb));
|
||||
assertEquals(existing, context.getBean("lookup"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedRegions() {
|
||||
Object parent = context.getBean("parent");
|
||||
//SubRegion child = context.getBean("/parent/child", SubRegion.class);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
|
||||
|
||||
log4j.category.org.springframework.data.gemfire.listener=TRACE
|
||||
log4j.category.org.springframework.data.gemfire.repository=DEBUG
|
||||
#log4j.category.org.springframework=DEBUG
|
||||
|
||||
# for debugging datasource initialization
|
||||
# log4j.category.test.jdbc=DEBUG
|
||||
|
||||
@@ -28,4 +28,8 @@
|
||||
<bean id="c-writer" class="org.springframework.data.gemfire.SimpleCacheWriter"/>
|
||||
|
||||
<gfe:lookup-region id="lookup" name="existing"/>
|
||||
|
||||
<gfe:replicated-region id="parent">
|
||||
<gfe:replicated-region name="child"/>
|
||||
</gfe:replicated-region>
|
||||
</beans>
|
||||
Reference in New Issue
Block a user