Partial Fix and Refactoring for JIRA issue SGF-236 enabling the <gfe:lookup-region/> nested element syntax to function properly.

This commit is contained in:
John Blum
2013-12-11 16:04:52 -08:00
parent 5cbe2b6260
commit 8ee149e6e4
6 changed files with 186 additions and 17 deletions

View File

@@ -77,8 +77,6 @@ public class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V> imple
private Object[] asyncEventQueues;
private Object[] gatewaySenders;
private Region<?, ?> parent;
private RegionAttributes<K, V> attributes;
private Resource snapshot;
@@ -169,13 +167,13 @@ public class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V> imple
// get underlying AttributesFactory
postProcess(findAttributesFactory(regionFactory));
Region<K, V> region = (this.parent != null ? regionFactory.createSubregion(parent, regionName)
Region<K, V> region = (getParent() != null ? regionFactory.createSubregion(getParent(), regionName)
: regionFactory.create(regionName));
if (log.isInfoEnabled()) {
if (parent != null) {
if (getParent() != null) {
log.info(String.format("Created new Cache sub-Region [%1$s] under parent Region [%2$s].",
regionName, parent.getName()));
regionName, getParent().getName()));
}
else {
log.info(String.format("Created new Cache Region [%1$s].", regionName));
@@ -249,7 +247,6 @@ public class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V> imple
*/
@SuppressWarnings("unused")
protected void postProcess(Region<K, V> region) {
}
@Override
@@ -374,10 +371,6 @@ public class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V> imple
this.hubId = hubId;
}
public void setParent(Region<?, ?> parent) {
this.parent = parent;
}
public void setPersistent(boolean persistent) {
this.persistent = persistent;
}

View File

@@ -35,12 +35,14 @@ import com.gemstone.gemfire.cache.Region;
* @author Costin Leau
* @author John Blum
*/
@SuppressWarnings("unused")
public class RegionLookupFactoryBean<K, V> implements FactoryBean<Region<K, V>>, InitializingBean, BeanNameAware {
protected final Log log = LogFactory.getLog(getClass());
private GemFireCache cache;
private Region<?, ?> parent;
private Region<K, V> region;
private String beanName;
@@ -56,7 +58,13 @@ public class RegionLookupFactoryBean<K, V> implements FactoryBean<Region<K, V>>,
Assert.hasText(regionName, "The 'regionName', 'name' or 'beanName' property must be set.");
synchronized (cache) {
region = cache.getRegion(regionName);
//region = (getParent() != null ? getParent().getSubregion(regionName) : cache.getRegion(regionName));
if (getParent() != null) {
region = getParent().getSubregion(regionName);
}
else {
region = cache.getRegion(regionName);
}
if (region != null) {
log.info(String.format("Retrieved Region [%1$s] from Cache [%2$s].", regionName, cache.getName()));
@@ -126,6 +134,26 @@ public class RegionLookupFactoryBean<K, V> implements FactoryBean<Region<K, V>>,
this.name = name;
}
/**
* Sets a reference to the parent Region if this FactoryBean represents a GemFire Cache Sub-Region.
* <p/>
* @param parent a reference to the parent Region if this Region is a Sub-Region.
* @see com.gemstone.gemfire.cache.Region
*/
public void setParent(Region<?, ?> parent) {
this.parent = parent;
}
/**
* Gets a reference to the parent Region if this FactoryBean represents a GemFire Cache Sub-Region.
* <p/>
* @return a reference to the parent Region or null if this Region is not a Sub-Region.
* @see com.gemstone.gemfire.cache.Region
*/
protected Region<?, ?> getParent() {
return parent;
}
/**
* Sets the name of the Cache Region as expected by GemFire. If no Region is found with the given name, a new one
* will be created. If no name is given, the value of the 'name' property will be used.

View File

@@ -41,15 +41,13 @@ class LookupRegionParser extends AbstractRegionParser {
boolean subRegion) {
super.doParse(element, builder);
String resolvedCacheRef = ParsingUtils.resolveCacheReference(element.getAttribute("cache-ref"));
builder.addPropertyReference("cache", resolvedCacheRef);
ParsingUtils.setPropertyValue(element, builder, "name", "name");
if (!subRegion) {
String cacheRef = element.getAttribute("cache-ref");
builder.addPropertyReference("cache", (StringUtils.hasText(cacheRef) ? cacheRef
: GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME));
}
else {
builder.addPropertyValue("lookupOnly", true);
parseSubRegions(element, parserContext, resolvedCacheRef);
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gemstone.gemfire.cache.Region;
/**
* The LookupSubRegionTest class is a test suite of test cases testing the contract and functionality of Region lookups
* using Spring Data GemFire configuration and GemFire native cache.xml.
* <p/>
* @author John Blum
* @see org.junit.Test
* @see org.springframework.context.ApplicationContext
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
* @see com.gemstone.gemfire.cache.Region
* @since 1.3.3
* @since 7.0.1 (GemFire)
*/
@ContextConfiguration("lookupSubRegion.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@SuppressWarnings("unused")
public class LookupSubRegionTest {
@Autowired
private ApplicationContext context;
protected void assertRegionExists(final String expectedRegionName, final String expectedRegionPath, final Region region) {
assertNotNull(String.format("The Region with name (%1$s) at path (%2$s) was null!",
expectedRegionName, expectedRegionPath), region);
assertEquals(String.format("Expected Region name of %1$s; but was %2$s!", expectedRegionName, region.getName()),
expectedRegionName, region.getName());
assertEquals(String.format("Expected Region path of %1$s; but was %2$s!", expectedRegionPath, region.getFullPath()),
expectedRegionPath, region.getFullPath());
}
@Test
public void testDirectLookup() {
Region accounts = context.getBean("/Customers/Accounts", Region.class);
assertRegionExists("Accounts", "/Customers/Accounts", accounts);
assertFalse(context.containsBean("Customers/Accounts"));
Region items = context.getBean("Customers/Accounts/Orders/Items", Region.class);
assertRegionExists("Items", "/Customers/Accounts/Orders/Items", items);
assertFalse(context.containsBean("/Customers/Accounts/Orders/Items"));
}
@Test
public void testNestedLookup() {
Region parent = context.getBean("Parent", Region.class);
assertRegionExists("Parent", "/Parent", parent);
assertFalse(context.containsBean("/Parent"));
Region child = context.getBean("/Parent/Child", Region.class);
assertRegionExists("Child", "/Parent/Child", child);
assertFalse(context.containsBean("Parent/Child"));
Region grandchild = context.getBean("/Parent/Child/Grandchild", Region.class);
assertRegionExists("Grandchild", "/Parent/Child/Grandchild", grandchild);
assertFalse(context.containsBean("Parent/Child/Grandchild"));
}
}

View File

@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:gfe="http://www.springframework.org/schema/gemfire"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire-1.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<util:properties id="peerCacheConfigurationSettings">
<prop key="name">springGemFireLookupSubRegionTest</prop>
<prop key="locators">localhost[11235]</prop>
<prop key="log-level">config</prop>
<prop key="mcast-port">0</prop>
<prop key="start-locator">localhost[11235]</prop>
</util:properties>
<gfe:cache cache-xml-location="/subregion-cache.xml" properties-ref="peerCacheConfigurationSettings"/>
<gfe:lookup-region id="/Customers/Accounts"/>
<gfe:lookup-region id="Customers/Accounts/Orders/Items"/>
<gfe:lookup-region id="Parent">
<gfe:lookup-region name="Child">
<gfe:lookup-region name="Grandchild"/>
</gfe:lookup-region>
</gfe:lookup-region>
<!--
<gfe:lookup-region id="/Parent/Child"/>
-->
<!--
<gfe:lookup-region id="/Parent/Child/Grandchild"/>
-->
</beans>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<!DOCTYPE cache PUBLIC "-//GemStone Systems, Inc.//GemFire Declarative Caching 7.0//EN"
"http://www.gemstone.com/dtd/cache7_0.dtd">
<cache>
<region name="Parent" refid="REPLICATE">
<region name="Child" refid="REPLICATE">
<region name="Grandchild" refid="REPLICATE"/>
</region>
</region>
<region name="Customers" refid="REPLICATE">
<region name="Accounts" refid="REPLICATE">
<region name="Orders" refid="REPLICATE">
<region name="Items" refid="REPLICATE"/>
</region>
</region>
</region>
</cache>