Code changes based on code review by David Turanski in PR #33 for JIRA issue SGF-203 concerning persistence.

This commit is contained in:
John Blum
2013-10-30 13:34:53 -07:00
parent 339393b794
commit 0f819f2e04
8 changed files with 151 additions and 66 deletions

View File

@@ -0,0 +1,68 @@
/*
* 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 java.util.concurrent.ConcurrentMap;
import org.springframework.util.ClassUtils;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;
/**
* The GemfireUtils class is a utility class encapsulating common functionality to access features and capabilities
* of GemFire based on version and other configuration meta-data.
* <p/>
* @author John Blum
* @since 1.3.3
*/
public abstract class GemfireUtils {
public final static String GEMFIRE_VERSION = CacheFactory.getVersion();
public static boolean isGemfireVersion65OrAbove() {
// expected 'major.minor'
try {
double version = Double.parseDouble(GEMFIRE_VERSION.substring(0, 3));
return version >= 6.5;
}
catch (NumberFormatException e) {
// NOTE based on logic from the PartitionedRegionFactoryBean class...
return ConcurrentMap.class.isAssignableFrom(Region.class);
}
}
public static boolean isGemfireVersion7OrAbove() {
try {
int version = Integer.parseInt(GEMFIRE_VERSION.substring(0, 1));
return version >= 7;
}
catch (NumberFormatException e) {
// NOTE the com.gemstone.gemfire.distributed.ServerLauncher class only exists in GemFire v 7.0.x or above...
return ClassUtils.isPresent("com.gemstone.gemfire.distributed.ServerLauncher",
Thread.currentThread().getContextClassLoader());
}
}
public static void main(final String... args) {
System.out.printf("GemFire Version %1$s%n", GEMFIRE_VERSION);
//System.out.printf("Is GemFire Version 6.5 of Above? %1$s%n", isGemfireVersion65OrAbove());
//System.out.printf("Is GemFire Version 7.0 of Above? %1$s%n", isGemfireVersion7OrAbove());
}
}

View File

@@ -55,7 +55,7 @@ public class LocalRegionFactoryBean<K, V> extends RegionFactoryBean<K, V> {
protected void resolveDataPolicy(RegionFactory<K, V> regionFactory, Boolean persistent, String dataPolicy) {
DataPolicy resolvedDataPolicy = new DataPolicyConverter().convert(dataPolicy);
Assert.isTrue(resolvedDataPolicy != null || (resolvedDataPolicy == null && !StringUtils.hasText(dataPolicy)),
Assert.isTrue(resolvedDataPolicy != null || !StringUtils.hasText(dataPolicy),
String.format("Data Policy '%1$s' is invalid.", dataPolicy));
if (resolvedDataPolicy == null || DataPolicy.NORMAL.equals(resolvedDataPolicy)) {

View File

@@ -15,13 +15,10 @@
*/
package org.springframework.data.gemfire;
import java.util.concurrent.ConcurrentMap;
import org.springframework.util.Assert;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionFactory;
/**
@@ -49,7 +46,7 @@ public class PartitionedRegionFactoryBean<K, V> extends RegionFactoryBean<K, V>
}
else if (isPersistent()) {
// first, check the presence of GemFire 6.5 or Higher
Assert.isTrue(isGemFireVersion65orHigher(), String.format(
Assert.isTrue(GemfireUtils.isGemfireVersion65OrAbove(), String.format(
"Can define Persistent Partitioned Regions only from GemFire 6.5 onwards; current version is [%1$s]",
CacheFactory.getVersion()));
regionFactory.setDataPolicy(DataPolicy.PERSISTENT_PARTITION);
@@ -59,8 +56,4 @@ public class PartitionedRegionFactoryBean<K, V> extends RegionFactoryBean<K, V>
}
}
private boolean isGemFireVersion65orHigher() {
return ConcurrentMap.class.isAssignableFrom(Region.class);
}
}

View File

@@ -28,6 +28,7 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedArray;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.data.gemfire.GemfireUtils;
import org.springframework.data.gemfire.SubRegionFactoryBean;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
@@ -100,14 +101,14 @@ abstract class AbstractRegionParser extends AbstractSingleBeanDefinitionParser {
String hubId = element.getAttribute("hub-id");
// Factory will enable gateway if it is not set and hub-id is set.
if (StringUtils.hasText(enableGateway)) {
if (ParsingUtils.isGemfireV7OrAbove()) {
if (GemfireUtils.isGemfireVersion7OrAbove()) {
log.warn("'enable-gateway' is deprecated since Gemfire 7.0");
}
}
ParsingUtils.setPropertyValue(element, builder, "enable-gateway");
if (StringUtils.hasText(hubId)) {
if (ParsingUtils.isGemfireV7OrAbove()) {
if (GemfireUtils.isGemfireVersion7OrAbove()) {
log.warn("'hub-id' is deprecated since Gemfire 7.0");
}
if (!CollectionUtils.isEmpty(DomUtils.getChildElementsByTagName(element, "gateway-sender"))) {

View File

@@ -18,6 +18,7 @@ package org.springframework.data.gemfire.config;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.data.gemfire.GemfireUtils;
import org.springframework.data.gemfire.wan.AsyncEventQueueFactoryBean;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
@@ -58,7 +59,7 @@ public class AsyncEventQueueParser extends AbstractSingleBeanDefinitionParser {
ParsingUtils.setPropertyValue(element, builder, "persistent");
ParsingUtils.setPropertyValue(element, builder, "parallel");
if (ParsingUtils.GEMFIRE_VERSION.compareTo("7.0.1") >= 0 ) {
if (GemfireUtils.GEMFIRE_VERSION.compareTo("7.0.1") >= 0 ) {
ParsingUtils.setPropertyValue(element, builder, "batch-conflation-enabled");
ParsingUtils.setPropertyValue(element, builder, "disk-synchronous");
ParsingUtils.setPropertyValue(element, builder, "batch-time-interval");

View File

@@ -20,22 +20,17 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanMetadataAttribute;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedArray;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.Conventions;
import org.springframework.data.gemfire.GemfireUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.ExpirationAction;
import com.gemstone.gemfire.cache.ExpirationAttributes;
import com.gemstone.gemfire.cache.LossAction;
@@ -49,16 +44,11 @@ import com.gemstone.gemfire.cache.Scope;
* @author Costin Leau
* @author David Turanski
* @author Lyndon Adams
* @author John Blum
*/
abstract class ParsingUtils {
private static Log log = LogFactory.getLog(ParsingUtils.class);
final static String GEMFIRE_VERSION = CacheFactory.getVersion();
private static final String ALIASES_KEY = ParsingUtils.class.getName() + ":aliases";
private static final Log log = LogFactory.getLog(ParsingUtils.class);
static void setPropertyValue(Element element, BeanDefinitionBuilder builder, String attributeName,
String propertyName, Object defaultValue) {
@@ -90,35 +80,6 @@ abstract class ParsingUtils {
}
}
/**
* Utility for parsing bean aliases. Normally parsed by
* AbstractBeanDefinitionParser however due to the attribute clash (bean
* uses 'name' for aliases while region use it to indicate their name), the
* parser needs to handle this differently by storing them as metadata which
* gets deleted just before registration.
*
* @param element
* @param builder
*/
static void addBeanAliasAsMetadata(Element element, BeanDefinitionBuilder builder) {
String[] aliases = new String[0];
String name = element.getAttributeNS(BeanDefinitionParserDelegate.BEANS_NAMESPACE_URI,
AbstractBeanDefinitionParser.NAME_ATTRIBUTE);
if (StringUtils.hasLength(name)) {
aliases = StringUtils.trimArrayElements(StringUtils.commaDelimitedListToStringArray(name));
}
BeanMetadataAttribute attr = new BeanMetadataAttribute(ALIASES_KEY, aliases);
attr.setSource(element);
builder.getRawBeanDefinition().addMetadataAttribute(attr);
}
static BeanDefinitionHolder replaceBeanAliasAsMetadata(BeanDefinitionHolder holder) {
BeanDefinition beanDefinition = holder.getBeanDefinition();
return new BeanDefinitionHolder(beanDefinition, holder.getBeanName(),
(String[]) beanDefinition.removeAttribute(ALIASES_KEY));
}
/**
* Utility method handling parsing of nested definition of the type:
*
@@ -349,7 +310,7 @@ abstract class ParsingUtils {
String concurrencyChecksEnabled = element.getAttribute("concurrency-checks-enabled");
if (StringUtils.hasText(concurrencyChecksEnabled)) {
if (!ParsingUtils.isGemfireV7OrAbove()) {
if (!GemfireUtils.isGemfireVersion7OrAbove()) {
log.warn("'concurrency-checks-enabled' is only available in Gemfire 7.0 or above");
} else {
ParsingUtils.setPropertyValue(element, attrBuilder, "concurrency-checks-enabled");
@@ -387,21 +348,14 @@ abstract class ParsingUtils {
}
static void throwExceptionIfNotGemfireV7(String elementName, String attributeName, ParserContext parserContext) {
if (!isGemfireV7OrAbove()) {
if (!GemfireUtils.isGemfireVersion7OrAbove()) {
String messagePrefix = (attributeName == null) ? "element '" + elementName + "'" : "attribute '"
+ attributeName + " of element '" + elementName + "'";
parserContext.getReaderContext().error(
messagePrefix + " requires Gemfire version 7 or later. The current version is " + GEMFIRE_VERSION,
messagePrefix + " requires Gemfire version 7 or later. The current version is " + GemfireUtils.GEMFIRE_VERSION,
null);
}
}
static boolean isGemfireV7OrAbove() {
int version = Integer.parseInt(GEMFIRE_VERSION.substring(0,1));
return version >= 7;
}
static void parseScope(Element element, BeanDefinitionBuilder builder) {
String scope = element.getAttribute("scope");
@@ -469,4 +423,4 @@ abstract class ParsingUtils {
return true;
}
}
}