From 1f0ae2b2ef14dbd06dab9bf71cf26bdba191e79d Mon Sep 17 00:00:00 2001 From: David Turanski Date: Thu, 30 Aug 2012 13:54:54 -0400 Subject: [PATCH] SGF-122 - Added namespace support for FixedPartitionAttributes --- .../FixedPartitionAttributesFactoryBean.java | 98 +++++++++++++++++++ .../PartitionAttributesFactoryBean.java | 7 ++ .../config/PartitionedRegionParser.java | 22 ++++- .../gemfire/config/spring-gemfire-1.2.xsd | 30 ++++++ .../PartitionedRegionNamespaceTest.java | 33 ++++++- .../data/gemfire/config/partitioned-ns.xml | 6 ++ 6 files changed, 190 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/springframework/data/gemfire/FixedPartitionAttributesFactoryBean.java diff --git a/src/main/java/org/springframework/data/gemfire/FixedPartitionAttributesFactoryBean.java b/src/main/java/org/springframework/data/gemfire/FixedPartitionAttributesFactoryBean.java new file mode 100644 index 00000000..996d0f31 --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/FixedPartitionAttributesFactoryBean.java @@ -0,0 +1,98 @@ +/* + * 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; + +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.Assert; + +import com.gemstone.gemfire.cache.FixedPartitionAttributes; + +/** + * @author David Turanski + * + */ +public class FixedPartitionAttributesFactoryBean implements FactoryBean, InitializingBean { + private Boolean primary; + private String partitionName; + private Integer numBuckets; + private FixedPartitionAttributes fixedPartitionAttributes; + /** + * @param primary + */ + public void setPrimary(boolean primary) { + this.primary = primary; + } + + /** + * @param partitionName the partitionName to set + */ + public void setPartitionName(String partitionName) { + this.partitionName = partitionName; + } + + /** + * @param numBuckets the numBuckets to set + */ + public void setNumBuckets(Integer numBuckets) { + this.numBuckets = numBuckets; + } + + + /* (non-Javadoc) + * @see org.springframework.beans.factory.FactoryBean#getObject() + */ + @Override + public FixedPartitionAttributes getObject() throws Exception { + return fixedPartitionAttributes; + } + + /* (non-Javadoc) + * @see org.springframework.beans.factory.FactoryBean#getObjectType() + */ + @Override + public Class getObjectType() { + return FixedPartitionAttributes.class; + } + + /* (non-Javadoc) + * @see org.springframework.beans.factory.FactoryBean#isSingleton() + */ + @Override + public boolean isSingleton() { + return true; + } + + /* (non-Javadoc) + * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() + */ + @Override + public void afterPropertiesSet() throws Exception { + Assert.hasText(partitionName,"partitionName cannot be empty or null"); + + fixedPartitionAttributes = null; + if (primary == null && numBuckets == null){ + fixedPartitionAttributes = FixedPartitionAttributes.createFixedPartition(partitionName); + } else if (primary == null && numBuckets != null){ + fixedPartitionAttributes = FixedPartitionAttributes.createFixedPartition(partitionName, numBuckets); + } else if (primary != null && numBuckets == null) { + fixedPartitionAttributes = FixedPartitionAttributes.createFixedPartition(partitionName, primary); + } else { + fixedPartitionAttributes = FixedPartitionAttributes.createFixedPartition(partitionName,primary,numBuckets); + } + + } +} diff --git a/src/main/java/org/springframework/data/gemfire/PartitionAttributesFactoryBean.java b/src/main/java/org/springframework/data/gemfire/PartitionAttributesFactoryBean.java index 9ec74b56..caa8d7e2 100644 --- a/src/main/java/org/springframework/data/gemfire/PartitionAttributesFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/PartitionAttributesFactoryBean.java @@ -21,6 +21,7 @@ import java.util.List; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; +import com.gemstone.gemfire.cache.FixedPartitionAttributes; import com.gemstone.gemfire.cache.PartitionAttributes; import com.gemstone.gemfire.cache.PartitionResolver; import com.gemstone.gemfire.cache.partition.PartitionListener; @@ -58,6 +59,12 @@ public class PartitionAttributesFactoryBean implements FactoryBean fixedPartitionAttributes) { + for (FixedPartitionAttributes fpa: fixedPartitionAttributes) { + paf.addFixedPartitionAttributes(fpa); + } + } public void setLocalMaxMemory(int mb) { paf.setLocalMaxMemory(mb); 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 6066f245..047cccc5 100644 --- a/src/main/java/org/springframework/data/gemfire/config/PartitionedRegionParser.java +++ b/src/main/java/org/springframework/data/gemfire/config/PartitionedRegionParser.java @@ -16,12 +16,17 @@ package org.springframework.data.gemfire.config; +import java.util.List; + 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.FixedPartitionAttributesFactoryBean; import org.springframework.data.gemfire.PartitionAttributesFactoryBean; import org.springframework.data.gemfire.PartitionedRegionFactoryBean; import org.springframework.data.gemfire.RegionAttributesFactoryBean; import org.springframework.util.StringUtils; +import org.springframework.util.CollectionUtils; import org.springframework.util.xml.DomUtils; import org.w3c.dom.Element; @@ -40,6 +45,7 @@ class PartitionedRegionParser extends AbstractRegionParser { return PartitionedRegionFactoryBean.class; } + @SuppressWarnings("unchecked") @Override protected void doParseRegion(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, boolean subRegion) { @@ -104,8 +110,22 @@ class PartitionedRegionParser extends AbstractRegionParser { parAttrBuilder.addPropertyValue("partitionListeners", parsePartitionListeners(parserContext, subElement, builder)); } + + List fixedPartitions = DomUtils.getChildElementsByTagName(element, "fixed-partition"); + if (! CollectionUtils.isEmpty(fixedPartitions)){ + + @SuppressWarnings("rawtypes") + ManagedList fixedPartitionAttributes = new ManagedList(); + for (Element fp: fixedPartitions) { + BeanDefinitionBuilder fpaBuilder = BeanDefinitionBuilder.genericBeanDefinition(FixedPartitionAttributesFactoryBean.class); + ParsingUtils.setPropertyValue(fp, fpaBuilder, "partition-name"); + ParsingUtils.setPropertyValue(fp, fpaBuilder, "num-buckets"); + ParsingUtils.setPropertyValue(fp, fpaBuilder, "primary"); + fixedPartitionAttributes.add(fpaBuilder.getBeanDefinition()); + } + parAttrBuilder.addPropertyValue("fixedPartitionAttributes", fixedPartitionAttributes); + } - // // // add partition attributes attributes attrBuilder.addPropertyValue("partitionAttributes", parAttrBuilder.getBeanDefinition()); // add partition/overflow settings as attributes 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 c4b74613..e8d7ebdd 100755 --- 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 @@ -1072,6 +1072,36 @@ is created or any bucket in a partitioned region becomes primary + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/org/springframework/data/gemfire/config/PartitionedRegionNamespaceTest.java b/src/test/java/org/springframework/data/gemfire/config/PartitionedRegionNamespaceTest.java index 519dc5fe..9b8ee461 100644 --- a/src/test/java/org/springframework/data/gemfire/config/PartitionedRegionNamespaceTest.java +++ b/src/test/java/org/springframework/data/gemfire/config/PartitionedRegionNamespaceTest.java @@ -35,6 +35,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.util.ObjectUtils; import com.gemstone.gemfire.cache.CacheListener; +import com.gemstone.gemfire.cache.FixedPartitionAttributes; import com.gemstone.gemfire.cache.PartitionAttributes; import com.gemstone.gemfire.cache.Region; import com.gemstone.gemfire.cache.RegionAttributes; @@ -55,6 +56,7 @@ public class PartitionedRegionNamespaceTest { testBasicPartition(); testComplexPartition(); testPartitionOptions(); + testFixedPartition(); } private void testBasicPartition() throws Exception { @@ -64,7 +66,8 @@ public class PartitionedRegionNamespaceTest { @SuppressWarnings("rawtypes") private void testPartitionOptions() throws Exception { assertTrue(context.containsBean("options")); - RegionFactoryBean fb = context.getBean("&options", RegionFactoryBean.class); + RegionFactoryBean fb = context.getBean("&options", + RegionFactoryBean.class); assertTrue(fb instanceof PartitionedRegionFactoryBean); assertEquals(null, TestUtils.readField("scope", fb)); assertEquals("redundant", TestUtils.readField("name", fb)); @@ -76,20 +79,24 @@ public class PartitionedRegionNamespaceTest { assertEquals(1, pAttr.getRedundantCopies()); assertEquals(4, pAttr.getTotalNumBuckets()); - assertSame(SimplePartitionResolver.class, pAttr.getPartitionResolver().getClass()); + assertSame(SimplePartitionResolver.class, pAttr.getPartitionResolver() + .getClass()); } @SuppressWarnings("rawtypes") private void testComplexPartition() throws Exception { assertTrue(context.containsBean("complex")); - RegionFactoryBean fb = context.getBean("&complex", RegionFactoryBean.class); + RegionFactoryBean fb = context.getBean("&complex", + RegionFactoryBean.class); CacheListener[] listeners = TestUtils.readField("cacheListeners", fb); assertFalse(ObjectUtils.isEmpty(listeners)); assertEquals(2, listeners.length); assertSame(listeners[0], context.getBean("c-listener")); - assertSame(context.getBean("c-loader"), TestUtils.readField("cacheLoader", fb)); - assertSame(context.getBean("c-writer"), TestUtils.readField("cacheWriter", fb)); + assertSame(context.getBean("c-loader"), + TestUtils.readField("cacheLoader", fb)); + assertSame(context.getBean("c-writer"), + TestUtils.readField("cacheWriter", fb)); RegionAttributes attrs = TestUtils.readField("attributes", fb); PartitionAttributes pAttr = attrs.getPartitionAttributes(); @@ -100,6 +107,22 @@ public class PartitionedRegionNamespaceTest { assertTrue(pAttr.getPartitionListeners()[0] instanceof TestPartitionListener); } + @SuppressWarnings("rawtypes") + public void testFixedPartition() throws Exception { + RegionFactoryBean fb = context.getBean("&fixed", + RegionFactoryBean.class); + RegionAttributes attrs = TestUtils.readField("attributes", fb); + PartitionAttributes pAttr = attrs.getPartitionAttributes(); + assertNotNull(pAttr.getFixedPartitionAttributes()); + assertEquals(3, pAttr.getFixedPartitionAttributes().size()); + + FixedPartitionAttributes fpa = (FixedPartitionAttributes) pAttr + .getFixedPartitionAttributes().get(0); + assertEquals(3, fpa.getNumBuckets()); + assertTrue(fpa.isPrimary()); + + } + public static class TestPartitionListener implements PartitionListener { @Override diff --git a/src/test/resources/org/springframework/data/gemfire/config/partitioned-ns.xml b/src/test/resources/org/springframework/data/gemfire/config/partitioned-ns.xml index 00a0bf8d..30665ce0 100644 --- a/src/test/resources/org/springframework/data/gemfire/config/partitioned-ns.xml +++ b/src/test/resources/org/springframework/data/gemfire/config/partitioned-ns.xml @@ -30,6 +30,12 @@ + + + + + +