diff --git a/src/main/java/org/springframework/data/gemfire/SubRegion.java b/src/main/java/org/springframework/data/gemfire/SubRegion.java deleted file mode 100644 index dbb1b68b..00000000 --- a/src/main/java/org/springframework/data/gemfire/SubRegion.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.springframework.data.gemfire; - -import java.util.List; - -import com.gemstone.gemfire.cache.RegionAttributes; -/** - * - * @author David Turanski - * - * @param - * @param - */ -public class SubRegion { - private final RegionAttributes regionAttributes; - private final String regionName; - private List> subRegions; - - public SubRegion(String regionName, RegionAttributes regionAttributes) { - this.regionAttributes = regionAttributes; - this.regionName = regionName; - } - - public RegionAttributes getRegionAttributes() { - return regionAttributes; - } - - public String getRegionName() { - return regionName; - } - - public List> getSubRegions() { - return subRegions; - } - public void setSubRegions(List> subRegions) { - this.subRegions = subRegions; - } -} diff --git a/src/main/java/org/springframework/data/gemfire/SubRegionFactoryBean.java b/src/main/java/org/springframework/data/gemfire/SubRegionFactoryBean.java index 5c1104dc..dd033209 100644 --- a/src/main/java/org/springframework/data/gemfire/SubRegionFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/SubRegionFactoryBean.java @@ -1,42 +1,79 @@ +/* + * Copyright 2010-2012 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 java.util.List; - -import org.springframework.beans.factory.BeanNameAware; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.Assert; import com.gemstone.gemfire.cache.AttributesFactory; +import com.gemstone.gemfire.cache.Region; -public class SubRegionFactoryBean extends AttributesFactory implements FactoryBean>, InitializingBean { +@SuppressWarnings("deprecation") +/** + * FactoryBean for creating a Gemfire Region as a subregion + * @author David Turanski + * + * @param - Region Key Type + * @param - Region Value Type + */ +public class SubRegionFactoryBean extends AttributesFactory implements FactoryBean>, + InitializingBean { + + protected final Log log = LogFactory.getLog(getClass()); + + @SuppressWarnings("unused") private String name; - private SubRegion subRegion; + private String regionName; - private List> subRegions; - - public void setName(String name) { - this.name = name; - } + private Region subRegion; - public void setSubRegions(List> subRegions) { - this.subRegions = subRegions; + private Region parent; + + private boolean lookupOnly; + + @Override + public void afterPropertiesSet() throws Exception { + Assert.notNull(parent, "parent region must not be null"); + + this.subRegion = parent.getSubregion(regionName); + if (this.subRegion == null) { + if (lookupOnly) { + throw new BeanInitializationException("Cannot find region [" + regionName + "] in cache " + + parent.getRegionService()); + } + else { + log.debug("creating subregion of [" + parent.getFullPath() + "] with name " + regionName); + this.subRegion = this.parent.createSubregion(regionName, create()); + } + } } @Override - public void afterPropertiesSet() throws Exception { - this.subRegion = new SubRegion(name,create()); - this.subRegion.setSubRegions(this.subRegions); - } - - @Override - public SubRegion getObject() throws Exception { + public Region getObject() throws Exception { return this.subRegion; } @Override public Class getObjectType() { - return SubRegion.class; + return Region.class; } @Override @@ -44,4 +81,37 @@ public class SubRegionFactoryBean extends AttributesFactory implements return true; } + /** + * Set the bean name - the same as the subregion full path + * @param name + */ + public void setName(String name) { + this.name = name; + } + + /** + * Set the simple name of the region + * @param regionName + */ + public void setRegionName(String regionName) { + this.regionName = regionName; + } + + /** + * Set the parent Region + * @param parent + */ + public void setParent(Region parent) { + this.parent = parent; + } + + /** + * Set to true if the subregion should already exist, e.g., specified by + * <lookup-region> + * @param lookupOnly + */ + public void setLookupOnly(boolean lookupOnly) { + this.lookupOnly = lookupOnly; + } + } diff --git a/src/main/java/org/springframework/data/gemfire/config/AbstractRegionParser.java b/src/main/java/org/springframework/data/gemfire/config/AbstractRegionParser.java new file mode 100644 index 00000000..55930f14 --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/config/AbstractRegionParser.java @@ -0,0 +1,116 @@ +/* + * Copyright 2012 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.config; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +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.xml.ParserContext; +import org.springframework.data.gemfire.RegionFactoryBean; +import org.springframework.data.gemfire.SubRegionFactoryBean; +import org.springframework.util.StringUtils; +import org.w3c.dom.Element; + +/** + * Base class for all Region Parsers + * + * @author David Turanski + */ +abstract class AbstractRegionParser extends AliasReplacingBeanDefinitionParser { + protected final Log log = LogFactory.getLog(getClass()); + + protected Class getBeanClass(Element element) { + if (element.hasAttribute("subregion")) { + return SubRegionFactoryBean.class; + } + else { + return RegionFactoryBean.class; + } + } + + @Override + protected void doParseInternal(Element element, ParserContext parserContext, BeanDefinitionBuilder builder){ + super.doParse(element, builder); + boolean subRegion = element.hasAttribute("subregion"); + + doParseRegion(element, parserContext, builder, subRegion); + + if (subRegion) { + builder.addPropertyValue("parent", parserContext.getContainingBeanDefinition().getAttribute("parent")); + builder.addPropertyValue("regionName", element.getAttribute(NAME_ATTRIBUTE)); + } + } + + protected abstract void doParseRegion(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, + boolean subRegion); + + protected void doParseSubRegion(Element element, Element subElement, ParserContext parserContext, + BeanDefinitionBuilder builder, boolean subRegion) { + + String regionPath = null; + String parentBeanName = null; + if (subRegion) { + parentBeanName = parserContext.getContainingBeanDefinition().getAttribute("regionPath").toString(); + } + else { + parentBeanName = getRegionNameFromElement(element); + } + regionPath = StringUtils + .arrayToDelimitedString( + new String[] { parentBeanName, getRegionNameFromElement(subElement) }, "/"); + if (!regionPath.startsWith("/")) { + regionPath = "/" + regionPath; + } + + /* + * The Region parser needs some context to handle recursion correctly + */ + builder.getBeanDefinition().setAttribute("parent", + new BeanDefinitionHolder(builder.getBeanDefinition(), parentBeanName)); + builder.getBeanDefinition().setAttribute("regionPath",regionPath); + + // Make recursive call + BeanDefinition subRegionDef = this.parseSubRegion(subElement, parserContext, builder); + //TODO: Is there a better work-around? + /* + * This setting prevents the BF from generating a name for this been + */ + subRegionDef.setScope(BeanDefinition.SCOPE_PROTOTYPE); + + if (log.isDebugEnabled()) { + log.debug("registering subregion as " + regionPath); + } + this.registerBeanDefinition(new BeanDefinitionHolder(subRegionDef, regionPath), parserContext.getRegistry()); + } + + private BeanDefinition parseSubRegion(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + /* + * Easy way to mark this element as a subregion + */ + element.setAttribute("subregion", "true"); + BeanDefinition beanDefinition = parserContext.getDelegate().parseCustomElement(element, + builder.getBeanDefinition()); + return beanDefinition; + } + + private String getRegionNameFromElement(Element element){ + String name = element.getAttribute(NAME_ATTRIBUTE); + return StringUtils.hasText(name)? name: element.getAttribute(ID_ATTRIBUTE); + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/data/gemfire/config/LookupRegionParser.java b/src/main/java/org/springframework/data/gemfire/config/LookupRegionParser.java index 8c4c925e..0c32d54b 100644 --- a/src/main/java/org/springframework/data/gemfire/config/LookupRegionParser.java +++ b/src/main/java/org/springframework/data/gemfire/config/LookupRegionParser.java @@ -16,10 +16,14 @@ package org.springframework.data.gemfire.config; +import java.util.List; + import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.data.gemfire.RegionLookupFactoryBean; +import org.springframework.data.gemfire.SubRegionFactoryBean; import org.springframework.util.StringUtils; +import org.springframework.util.xml.DomUtils; import org.w3c.dom.Element; /** @@ -27,21 +31,41 @@ import org.w3c.dom.Element; * * @author Costin Leau */ -class LookupRegionParser extends AliasReplacingBeanDefinitionParser { +class LookupRegionParser extends AbstractRegionParser { + @Override protected Class getBeanClass(Element element) { - return RegionLookupFactoryBean.class; + if (element.hasAttribute("subregion")) { + return SubRegionFactoryBean.class; + } + else { + return RegionLookupFactoryBean.class; + } } @Override - protected void doParseInternal(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + protected void doParseRegion(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, + boolean subRegion) { super.doParse(element, builder); ParsingUtils.setPropertyValue(element, builder, "name", "name"); - String attr = element.getAttribute("cache-ref"); - // add cache reference (fallback to default if nothing is specified) - builder.addPropertyReference("cache", (StringUtils.hasText(attr) ? attr : "gemfire-cache")); + if (!subRegion) { + String attr = element.getAttribute("cache-ref"); + // add cache reference (fallback to default if nothing is specified) + builder.addPropertyReference("cache", (StringUtils.hasText(attr) ? attr : "gemfire-cache")); + } + else { + builder.addPropertyValue("lookupOnly", true); + } + // parse nested elements + List subElements = DomUtils.getChildElements(element); + for (Element subElement : subElements) { + String name = subElement.getLocalName(); + if (name.endsWith("region")) { + doParseSubRegion(element, subElement, parserContext, builder, subRegion); + } + } } } diff --git a/src/main/java/org/springframework/data/gemfire/config/PartitionedRegionParser.java b/src/main/java/org/springframework/data/gemfire/config/PartitionedRegionParser.java index 5d046db3..aec50710 100644 --- a/src/main/java/org/springframework/data/gemfire/config/PartitionedRegionParser.java +++ b/src/main/java/org/springframework/data/gemfire/config/PartitionedRegionParser.java @@ -23,7 +23,6 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.data.gemfire.PartitionAttributesFactoryBean; import org.springframework.data.gemfire.RegionAttributesFactoryBean; -import org.springframework.data.gemfire.RegionFactoryBean; import org.springframework.util.StringUtils; import org.springframework.util.xml.DomUtils; import org.w3c.dom.Element; @@ -35,18 +34,17 @@ import com.gemstone.gemfire.cache.Region; /** * Parser for <partitioned-region;gt; definitions. * - * To avoid eager evaluations, the region attributes are declared as a nested definition. + * To avoid eager evaluations, the region attributes are declared as a nested + * definition. * * @author Costin Leau + * @author David Turanski */ -class PartitionedRegionParser extends AliasReplacingBeanDefinitionParser { - - protected Class getBeanClass(Element element) { - return RegionFactoryBean.class; - } +class PartitionedRegionParser extends AbstractRegionParser { @Override - protected void doParseInternal(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + protected void doParseRegion(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, + boolean subRegion) { super.doParse(element, builder); // set the data policy @@ -67,23 +65,24 @@ class PartitionedRegionParser extends AliasReplacingBeanDefinitionParser { builder.addPropertyValue("dataPolicy", DataPolicy.PARTITION); } + 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); + } 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")); - - // region attributes - BeanDefinitionBuilder attrBuilder = BeanDefinitionBuilder.genericBeanDefinition(RegionAttributesFactoryBean.class); - ParsingUtils.parseStatistics(element, attrBuilder); ParsingUtils.parseExpiration(parserContext, element, attrBuilder); ParsingUtils.parseEviction(parserContext, element, attrBuilder); ParsingUtils.parseDiskStorage(element, attrBuilder); // partition attributes - BeanDefinitionBuilder parAttrBuilder = BeanDefinitionBuilder.genericBeanDefinition(PartitionAttributesFactoryBean.class); - + BeanDefinitionBuilder parAttrBuilder = BeanDefinitionBuilder + .genericBeanDefinition(PartitionAttributesFactoryBean.class); attr = element.getAttribute("colocated-with"); @@ -122,7 +121,6 @@ class PartitionedRegionParser extends AliasReplacingBeanDefinitionParser { parAttrBuilder.addPropertyValue("totalNumBuckets", Integer.valueOf(attr)); } - List subElements = DomUtils.getChildElements(element); // parse nested cache-listener elements @@ -142,15 +140,21 @@ class PartitionedRegionParser extends AliasReplacingBeanDefinitionParser { } else if ("partition-resolver".equals(name)) { - parAttrBuilder.addPropertyValue("partitionResolver", parsePartitionResolver(parserContext, subElement, - builder)); + parAttrBuilder.addPropertyValue("partitionResolver", + parsePartitionResolver(parserContext, subElement, builder)); + } + // subregion + else if (name.endsWith("region")) { + doParseSubRegion(element, subElement, parserContext, builder, subRegion); } } // add partition attributes attributes attrBuilder.addPropertyValue("partitionAttributes", parAttrBuilder.getBeanDefinition()); // add partition/overflow settings as attributes - builder.addPropertyValue("attributes", attrBuilder.getBeanDefinition()); + if (!subRegion) { + builder.addPropertyValue("attributes", attrBuilder.getBeanDefinition()); + } } private Object parseCacheListener(ParserContext parserContext, Element subElement, BeanDefinitionBuilder builder) { diff --git a/src/main/java/org/springframework/data/gemfire/config/ReplicatedRegionParser.java b/src/main/java/org/springframework/data/gemfire/config/ReplicatedRegionParser.java index d1e85e3d..830f85ea 100644 --- a/src/main/java/org/springframework/data/gemfire/config/ReplicatedRegionParser.java +++ b/src/main/java/org/springframework/data/gemfire/config/ReplicatedRegionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2011 the original author or authors. + * Copyright 2010-2012 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. @@ -18,15 +18,9 @@ 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.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; @@ -38,18 +32,13 @@ import com.gemstone.gemfire.cache.Scope; * Parser for <replicated-region;gt; definitions. * * @author Costin Leau + * @author David Turanski */ -class ReplicatedRegionParser extends AliasReplacingBeanDefinitionParser { - - +class ReplicatedRegionParser extends AbstractRegionParser { @Override - protected void doParseInternal(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { - super.doParse(element, builder); - System.out.println("building class " + - builder.getBeanDefinition().getBeanClassName()); + protected void doParseRegion(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, + boolean subRegion) { - boolean subRegion = element.hasAttribute("subregion"); - // set the data policy String attr = element.getAttribute("persistent"); if (Boolean.parseBoolean(attr)) { @@ -64,8 +53,8 @@ class ReplicatedRegionParser extends AliasReplacingBeanDefinitionParser { ParsingUtils.setPropertyValue(element, builder, "name", "name"); BeanDefinitionBuilder attrBuilder = builder; - - if (!subRegion){ + + 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")); @@ -84,13 +73,12 @@ class ReplicatedRegionParser extends AliasReplacingBeanDefinitionParser { ParsingUtils.parseEviction(parserContext, element, attrBuilder); ParsingUtils.parseDiskStorage(element, attrBuilder); - builder.addPropertyValue("attributes", attrBuilder.getBeanDefinition()); + if (!subRegion) { + builder.addPropertyValue("attributes", attrBuilder.getBeanDefinition()); + } List subElements = DomUtils.getChildElements(element); - ManagedList subRegions = new ManagedList(); - subRegions.setElementTypeName(SubRegionFactoryBean.class.getName()); - // parse nested elements for (Element subElement : subElements) { String name = subElement.getLocalName(); @@ -106,23 +94,11 @@ 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); + // subregion + else if (name.endsWith("region")) { + doParseSubRegion(element, subElement, parserContext, builder, subRegion); } } - - if (!CollectionUtils.isEmpty(subRegions)) { - builder.addPropertyValue("subRegions", subRegions); - } } private Object parseCacheListener(ParserContext parserContext, Element subElement, BeanDefinitionBuilder builder) { @@ -137,10 +113,4 @@ class ReplicatedRegionParser extends AliasReplacingBeanDefinitionParser { 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; - } } \ No newline at end of file diff --git a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.2.xsd b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.2.xsd index a7f4fd02..f301c186 100644 --- a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.2.xsd +++ b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.2.xsd @@ -1,75 +1,90 @@ - - - - - - - + + + + + + - - - - - - + + + + + - - - - - + + + + - - - - - + + + + namespace and its 'properties' element. ]]> - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - - - - - - + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - + + + + + + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - - - - - - + + + + + + + + + - - - - - - + + + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + - - - - - - + + + + + - - - - - - - + + + + + + - - - - - - + + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - + + + + + + + + + - - - - - - - - - - + + + + + + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - - - + + + + - - - - - - - - - + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + - - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - + + + + + + + + + - - - - - - - + + + + + + - - - - - - - - - - + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - - - - - - + + + + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - + + + + - - - - - + + + + - - - - - - - - + + + + + + + - - - - - - - - - + + + + + + + + - - - - - + + + + - - - - - - - - - - - + + + + + + + + + + - - - - - + + + + - - - - - - - - + + + + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - - - - + + + + - - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - + + + + + + + - - - - - - + + + + + - - - - - - - - - + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - + + + + + + - - - - - - - - - - + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + - - - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + + - - - - - - - - - - - + + + + + + + + + + - - - - - + + + + - - - - - - - - - - - - + + + + + + + + + + + - - - - - - - + + + + + + - - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - - - - + + + + + + + + + - - - - - - - - - - + + + + + + + + + - - - - - + + + + - - - - - - - - - + + + + + + + + - - - - - - - - + + + + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - - - + + + + + + - - - - - - - - - - + + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - - + + + + + + + - - - - - + + + + - - - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The reference to a GemfireTemplate. Will default to 'gemfireTemplate'. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/org/springframework/data/gemfire/SubRegionFactoryBeanTest.java b/src/test/java/org/springframework/data/gemfire/SubRegionFactoryBeanTest.java deleted file mode 100644 index 0e387e00..00000000 --- a/src/test/java/org/springframework/data/gemfire/SubRegionFactoryBeanTest.java +++ /dev/null @@ -1,13 +0,0 @@ -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(); - } -} diff --git a/src/test/java/org/springframework/data/gemfire/SubRegionTest.java b/src/test/java/org/springframework/data/gemfire/SubRegionTest.java new file mode 100644 index 00000000..86177257 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/SubRegionTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2010-2012 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.assertNotNull; +import static org.junit.Assert.assertSame; + +import org.junit.Test; + +import com.gemstone.gemfire.cache.GemFireCache; +import com.gemstone.gemfire.cache.Region; + +/** + * + * @author David Turanski + * + */ +public class SubRegionTest extends RecreatingContextTest { + @Override + protected String location() { + return "org/springframework/data/gemfire/basic-subregion.xml"; + } + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Test + public void testBasic() throws Exception { + CacheFactoryBean cfb = new CacheFactoryBean(); + cfb.setUseBeanFactoryLocator(false); + cfb.afterPropertiesSet(); + GemFireCache cache = cfb.getObject(); + RegionFactoryBean rfb = new RegionFactoryBean(); + rfb.setCache(cache); + rfb.setName("parent"); + rfb.afterPropertiesSet(); + Region parent = rfb.getObject(); + + SubRegionFactoryBean srfb = new SubRegionFactoryBean(); + srfb.setParent(parent); + srfb.setName("/parent/child"); + srfb.setRegionName("child"); + srfb.afterPropertiesSet(); + Region child = srfb.getObject(); + + assertNotNull(parent.getSubregion("child")); + assertSame(child,parent.getSubregion("child")); + + cache.close(); + } + @SuppressWarnings("rawtypes") + @Test + public void testContext() throws Exception { + Region parent = ctx.getBean("parent", Region.class); + Region child = ctx.getBean("/parent/child", Region.class); + assertNotNull(parent.getSubregion("child")); + assertSame(child,parent.getSubregion("child")); + assertEquals("/parent/child",child.getFullPath()); + } + + @SuppressWarnings("rawtypes") + @Test + public void testChildOnly() throws Exception { + Region child = ctx.getBean("/parent/child", Region.class); + assertEquals("/parent/child",child.getFullPath()); + } +} diff --git a/src/test/java/org/springframework/data/gemfire/config/ReplicatedRegionNamespaceTest.java b/src/test/java/org/springframework/data/gemfire/config/ReplicatedRegionNamespaceTest.java index a943bfc0..415dc7ea 100644 --- a/src/test/java/org/springframework/data/gemfire/config/ReplicatedRegionNamespaceTest.java +++ b/src/test/java/org/springframework/data/gemfire/config/ReplicatedRegionNamespaceTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2011 the original author or authors. + * Copyright 2010-2012 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. @@ -18,18 +18,15 @@ 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; @@ -90,10 +87,4 @@ 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); - } } \ No newline at end of file diff --git a/src/test/java/org/springframework/data/gemfire/config/SubRegionNamespaceTest.java b/src/test/java/org/springframework/data/gemfire/config/SubRegionNamespaceTest.java new file mode 100644 index 00000000..dc7b8c67 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/config/SubRegionNamespaceTest.java @@ -0,0 +1,114 @@ +/* + * Copyright 2010-2012 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.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.data.gemfire.SubRegionFactoryBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.gemstone.gemfire.cache.AttributesFactory; +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.cache.CacheLoader; +import com.gemstone.gemfire.cache.Region; +import com.gemstone.gemfire.cache.RegionAttributes; + +/** + * @author David Turanski + */ +@SuppressWarnings("deprecation") +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("subregion-ns.xml") +public class SubRegionNamespaceTest { + + @Autowired + private ApplicationContext context; + + + + @SuppressWarnings("rawtypes") + @Test + public void testNestedReplicatedRegions() { + Region parent = context.getBean("parent",Region.class); + Region child = context.getBean("/parent/child", Region.class); + Region grandchild = context.getBean("/parent/child/grandchild", Region.class); + assertNotNull(child); + assertEquals("/parent/child",child.getFullPath()); + assertSame(child,parent.getSubregion("child")); + + assertEquals("/parent/child/grandchild",grandchild.getFullPath()); + assertSame(grandchild,child.getSubregion("grandchild")); + + } + + @SuppressWarnings({ "unused", "rawtypes", "unchecked" }) + @Test + public void testMixedNestedRegions() { + Cache cache = context.getBean(Cache.class); + + Region parent = context.getBean("replicatedParent",Region.class); + parent.createSubregion("lookupChild",new AttributesFactory().create()); + + Region child = context.getBean("/replicatedParent/lookupChild", Region.class); + Region grandchild = context.getBean("/replicatedParent/lookupChild/partitionedGrandchild", Region.class); + assertNotNull(child); + assertEquals("/replicatedParent/lookupChild",child.getFullPath()); + assertSame(child,parent.getSubregion("lookupChild")); + + assertEquals("/replicatedParent/lookupChild/partitionedGrandchild",grandchild.getFullPath()); + assertSame(grandchild,child.getSubregion("partitionedGrandchild")); + + } + + @SuppressWarnings("rawtypes") + @Test + public void testNestedRegionsWithSiblings() { + Region parent = context.getBean("parentWithSiblings",Region.class); + Region child1 = context.getBean("/parentWithSiblings/child1",Region.class); + assertEquals("/parentWithSiblings/child1",child1.getFullPath()); + Region child2 = context.getBean("/parentWithSiblings/child2",Region.class); + assertEquals("/parentWithSiblings/child2",child2.getFullPath()); + assertSame(child1,parent.getSubregion("child1")); + assertSame(child2,parent.getSubregion("child2")); + + Region grandchild1 = context.getBean("/parentWithSiblings/child1/grandChild11",Region.class); + assertEquals("/parentWithSiblings/child1/grandChild11",grandchild1.getFullPath()); + } + + @SuppressWarnings({ "unused", "rawtypes" }) + @Test + public void testComplexNestedRegions() throws Exception { + Region parent = context.getBean("complexNested",Region.class); + Region child1 = context.getBean("/complexNested/child1",Region.class); + Region child2 = context.getBean("/complexNested/child2",Region.class); + Region grandchild1 = context.getBean("/complexNested/child1/grandChild11",Region.class); + + SubRegionFactoryBean grandchild1fb = context.getBean("&/complexNested/child1/grandChild11",SubRegionFactoryBean.class); + assertNotNull(grandchild1fb); + RegionAttributes attr = grandchild1fb.create(); + assertNotNull(attr); + CacheLoader cl = attr.getCacheLoader(); + assertNotNull(cl); + } +} \ No newline at end of file diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties index 61dc0074..bdaf64ef 100644 --- a/src/test/resources/log4j.properties +++ b/src/test/resources/log4j.properties @@ -5,8 +5,8 @@ log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 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 +log4j.category.org.springframework.data.gemfire=DEBUG +log4j.category.org.springframework.beans.=DEBUG # for debugging datasource initialization # log4j.category.test.jdbc=DEBUG diff --git a/src/test/resources/org/springframework/data/gemfire/basic-subregion.xml b/src/test/resources/org/springframework/data/gemfire/basic-subregion.xml new file mode 100644 index 00000000..ad750a24 --- /dev/null +++ b/src/test/resources/org/springframework/data/gemfire/basic-subregion.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + diff --git a/src/test/resources/org/springframework/data/gemfire/config/subregion-ns.xml b/src/test/resources/org/springframework/data/gemfire/config/subregion-ns.xml new file mode 100644 index 00000000..d8cf4560 --- /dev/null +++ b/src/test/resources/org/springframework/data/gemfire/config/subregion-ns.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file