From 9b951666b03e02a8d4553009aaa7341f7bae5e58 Mon Sep 17 00:00:00 2001 From: John Blum Date: Tue, 3 Feb 2015 10:01:07 -0800 Subject: [PATCH] SGF-289 - Enumeration restrictions (xsd:enumeration) should be avoided in the XML schema. Removed the XSD enumeration restriction on the 'scope' attribute of the 'baseReplicatedRegionType' complexType, Region element definition in the SDG XSD allowing the use of property placeholders to specify a GemFire Cache Region's Scope for different distribution patterns. In addition, completely removed the 'scopeType' simpleType element definition in the SDG XSD. --- .../data/gemfire/ScopeConverter.java | 56 ++++++++ .../data/gemfire/ScopeType.java | 124 ++++++++++++++++++ ...rRegistrationBeanFactoryPostProcessor.java | 3 + .../data/gemfire/config/ParsingUtils.java | 2 +- .../gemfire/config/spring-gemfire-1.6.xsd | 23 +--- .../data/gemfire/ScopeConverterTest.java | 88 +++++++++++++ .../data/gemfire/ScopeTypeTest.java | 102 ++++++++++++++ ...istrationBeanFactoryPostProcessorTest.java | 6 +- .../gemfire/config/IndexNamespaceTest.java | 18 +-- .../config/ReplicatedRegionNamespaceTest.java | 1 + .../data/gemfire/config/index-ns.xml | 8 +- .../data/gemfire/config/replicated-ns.xml | 3 +- 12 files changed, 396 insertions(+), 38 deletions(-) create mode 100644 src/main/java/org/springframework/data/gemfire/ScopeConverter.java create mode 100644 src/main/java/org/springframework/data/gemfire/ScopeType.java create mode 100644 src/test/java/org/springframework/data/gemfire/ScopeConverterTest.java create mode 100644 src/test/java/org/springframework/data/gemfire/ScopeTypeTest.java diff --git a/src/main/java/org/springframework/data/gemfire/ScopeConverter.java b/src/main/java/org/springframework/data/gemfire/ScopeConverter.java new file mode 100644 index 00000000..3820472d --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/ScopeConverter.java @@ -0,0 +1,56 @@ +/* + * 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 org.springframework.data.gemfire.support.AbstractPropertyEditorConverterSupport; + +import com.gemstone.gemfire.cache.Scope; + +/** + * The ScopeConverter class is a Spring Converter and JavaBeans PropertyEditor that converts Strings + * into GemFire Scope constant values. + * + * @author John Blum + * @see org.springframework.data.gemfire.support.AbstractPropertyEditorConverterSupport + * @see com.gemstone.gemfire.cache.Scope + * @since 1.6.0 + */ +@SuppressWarnings("unused") +public class ScopeConverter extends AbstractPropertyEditorConverterSupport { + + /** + * Converts the given String source into an instance of GemFire Scope. + * + * @param source the String to convert into a GemFire Scope. + * @return a GemFire Scope for the given String. + * @throws java.lang.IllegalArgumentException if the String is not a valid GemFire Scope. + * @see org.springframework.data.gemfire.ScopeType#getScope(ScopeType) + * @see org.springframework.data.gemfire.ScopeType#valueOfIgnoreCase(String) + * @see com.gemstone.gemfire.cache.Scope#fromString(String) + * @see #assertConverted(String, Object, Class) + */ + @Override + public Scope convert(final String source) { + try { + return Scope.fromString(source); + } + catch (IllegalArgumentException e) { + return assertConverted(source, ScopeType.getScope(ScopeType.valueOfIgnoreCase(source)), Scope.class); + } + } + +} diff --git a/src/main/java/org/springframework/data/gemfire/ScopeType.java b/src/main/java/org/springframework/data/gemfire/ScopeType.java new file mode 100644 index 00000000..df35e636 --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/ScopeType.java @@ -0,0 +1,124 @@ +/* + * 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 com.gemstone.gemfire.cache.Scope; +import com.gemstone.gemfire.management.internal.cli.util.spring.StringUtils; + +/** + * The ScopeType enum is an enumeration of GemFire Scopes. + * + * @author John Blum + * @see com.gemstone.gemfire.cache.Scope + * @since 1.6.0 + */ +@SuppressWarnings("unused") +public enum ScopeType { + DISTRIBUTED_ACK(Scope.DISTRIBUTED_ACK), + DISTRIBUTED_NO_ACK(Scope.DISTRIBUTED_NO_ACK), + GLOBAL(Scope.GLOBAL), + LOCAL(Scope.LOCAL); + + private final Scope gemfireScope; + + /** + * Constructs an instance of the ScopeType initialized with a matching GemFire Scope. + * + * @param gemfireScope the GemFire Scope paired with this enumerated value. + * @see com.gemstone.gemfire.cache.Scope + */ + ScopeType(final Scope gemfireScope) { + this.gemfireScope = gemfireScope; + } + + /** + * Null-safe operation to extract the GemFire Scope from the given ScopeType enum value, or null if the provided + * scopeType is null. + * + * @param scopeType the ScopeType enumerated value from which to extract the GemFire Scope. + * @return the paired GemFire Scope from the given ScopeType or null if scopeType is null. + * @see com.gemstone.gemfire.cache.Scope + * @see #getScope() + */ + public static Scope getScope(final ScopeType scopeType) { + return (scopeType != null ? scopeType.getScope() : null); + } + + /** + * Returns a ScopeType enumerated value for the given a GemFire Scope. + * + * @param scope the GemFire Scope used to lookup and match the appropriate ScopeType. + * @return a ScopeType for the given GemFire Scope or null if no match was found. + * @see com.gemstone.gemfire.cache.Scope + * @see #getScope() + * @see #values() + */ + public static ScopeType valueOf(final Scope scope) { + for (ScopeType scopeType : values()) { + if (scopeType.getScope().equals(scope)) { + return scopeType; + } + } + + return null; + } + + /** + * Returns a ScopeType enumerated value given the case-insensitive name of the GemFire Scope. + * + * @param name a String name describing the ScopeType enum value. + * @return a ScopeType for the given case-insensitive, named GemFire Scope. + * @see java.lang.String#equalsIgnoreCase(String) + * @see #values() + * @see #name() + * @see #transform(String) + */ + public static ScopeType valueOfIgnoreCase(String name) { + name = transform(name); + + for (ScopeType scopeType : values()) { + if (scopeType.name().equalsIgnoreCase(name)) { + return scopeType; + } + } + + return null; + } + + /** + * Null-safe operation that transforms a String name having hyphens and whitespace into a String with underscores + * and no whitespace. + * + * @param name the String to transform. + * @return a String value with underscores for hyphens and all leading/trailing whitespace trimmed, or null + * if the given String name is null. + */ + private static String transform(final String name) { + return (StringUtils.hasText(name) ? name.trim().replaceAll("-", "_") : name); + } + + /** + * Gets the matching GemFire Scope for this enumerated value. + * + * @return a GemFire Scope for this enumerated value. + * @see com.gemstone.gemfire.cache.Scope + */ + public Scope getScope() { + return gemfireScope; + } + +} diff --git a/src/main/java/org/springframework/data/gemfire/config/CustomEditorRegistrationBeanFactoryPostProcessor.java b/src/main/java/org/springframework/data/gemfire/config/CustomEditorRegistrationBeanFactoryPostProcessor.java index ec364ace..2bf207ec 100644 --- a/src/main/java/org/springframework/data/gemfire/config/CustomEditorRegistrationBeanFactoryPostProcessor.java +++ b/src/main/java/org/springframework/data/gemfire/config/CustomEditorRegistrationBeanFactoryPostProcessor.java @@ -28,6 +28,7 @@ import org.springframework.data.gemfire.IndexMaintenancePolicyType; import org.springframework.data.gemfire.IndexType; import org.springframework.data.gemfire.IndexTypeConverter; import org.springframework.data.gemfire.InterestPolicyConverter; +import org.springframework.data.gemfire.ScopeConverter; import org.springframework.data.gemfire.client.InterestResultPolicyConverter; import org.springframework.data.gemfire.server.SubscriptionEvictionPolicy; import org.springframework.data.gemfire.server.SubscriptionEvictionPolicyConverter; @@ -36,6 +37,7 @@ import com.gemstone.gemfire.cache.EvictionAction; import com.gemstone.gemfire.cache.ExpirationAction; import com.gemstone.gemfire.cache.InterestPolicy; import com.gemstone.gemfire.cache.InterestResultPolicy; +import com.gemstone.gemfire.cache.Scope; /** * The CustomEditorRegistrationBeanFactoryPostProcessor class is a Spring BeanFactoryPostProcessor used to register @@ -58,6 +60,7 @@ public class CustomEditorRegistrationBeanFactoryPostProcessor implements BeanFac beanFactory.registerCustomEditor(IndexType.class, IndexTypeConverter.class); beanFactory.registerCustomEditor(InterestPolicy.class, InterestPolicyConverter.class); beanFactory.registerCustomEditor(InterestResultPolicy.class, InterestResultPolicyConverter.class); + beanFactory.registerCustomEditor(Scope.class, ScopeConverter.class); beanFactory.registerCustomEditor(SubscriptionEvictionPolicy.class, SubscriptionEvictionPolicyConverter.class); } diff --git a/src/main/java/org/springframework/data/gemfire/config/ParsingUtils.java b/src/main/java/org/springframework/data/gemfire/config/ParsingUtils.java index 49245c7c..7f42a734 100644 --- a/src/main/java/org/springframework/data/gemfire/config/ParsingUtils.java +++ b/src/main/java/org/springframework/data/gemfire/config/ParsingUtils.java @@ -360,7 +360,7 @@ abstract class ParsingUtils { String scopeAttributeValue = element.getAttribute("scope"); if (StringUtils.hasText(scopeAttributeValue)) { - builder.addPropertyValue("scope", Scope.fromString(scopeAttributeValue.toUpperCase().replace("-", "_"))); + builder.addPropertyValue("scope", scopeAttributeValue); } } diff --git a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.6.xsd b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.6.xsd index 7f1c7ac6..922a3951 100644 --- a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.6.xsd +++ b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.6.xsd @@ -1090,10 +1090,14 @@ as only they allow locking. GemFire default is false. ]]> - + @@ -2842,21 +2846,6 @@ The id of the function service (optional) - - - - - - - - - - - +"> HASH @@ -28,9 +28,11 @@ - + - + + diff --git a/src/test/resources/org/springframework/data/gemfire/config/replicated-ns.xml b/src/test/resources/org/springframework/data/gemfire/config/replicated-ns.xml index 8036b27b..2a502ca9 100644 --- a/src/test/resources/org/springframework/data/gemfire/config/replicated-ns.xml +++ b/src/test/resources/org/springframework/data/gemfire/config/replicated-ns.xml @@ -55,13 +55,14 @@ synchronous + distributed-No_ACK - +