Refactored the DataPolicyConverter for basic code cleanup and added a value to the Policy enum for PARTITION. Added additional test cases in the corresponding test class. Refactored the resolveDataPolicy method in the RegionFactoryBean making use of helper methods.
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.gemfire;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
@@ -24,54 +25,50 @@ import com.gemstone.gemfire.cache.DataPolicy;
|
||||
*
|
||||
*/
|
||||
public class DataPolicyConverter implements Converter<String, DataPolicy> {
|
||||
private static enum Policy {
|
||||
EMPTY, DEFAULT, NORMAL, PERSISTENT_PARTITION, PERSISTENT_REPLICATE, PRELOADED, REPLICATE;
|
||||
public DataPolicy toDataPolicy() {
|
||||
DataPolicy dataPolicy = null;
|
||||
switch (this) {
|
||||
case EMPTY:
|
||||
dataPolicy = DataPolicy.EMPTY;
|
||||
break;
|
||||
case DEFAULT:
|
||||
dataPolicy = DataPolicy.DEFAULT;
|
||||
break;
|
||||
case NORMAL:
|
||||
dataPolicy = DataPolicy.NORMAL;
|
||||
break;
|
||||
case PERSISTENT_PARTITION:
|
||||
dataPolicy = DataPolicy.PERSISTENT_PARTITION;
|
||||
break;
|
||||
case PERSISTENT_REPLICATE:
|
||||
dataPolicy = DataPolicy.PERSISTENT_REPLICATE;
|
||||
break;
|
||||
case PRELOADED:
|
||||
dataPolicy = DataPolicy.PRELOADED;
|
||||
break;
|
||||
case REPLICATE:
|
||||
dataPolicy = DataPolicy.REPLICATE;
|
||||
break;
|
||||
}
|
||||
return dataPolicy;
|
||||
|
||||
static enum Policy {
|
||||
DEFAULT, EMPTY, NORMAL, PRELOADED, PARTITION, PERSISTENT_PARTITION, REPLICATE, PERSISTENT_REPLICATE;
|
||||
|
||||
private static String toUpperCase(String value) {
|
||||
return (value == null ? null : value.toUpperCase());
|
||||
}
|
||||
|
||||
public static Policy getValue(String value) {
|
||||
Policy policy = null;
|
||||
try {
|
||||
policy = valueOf(value);
|
||||
return valueOf(toUpperCase(value));
|
||||
}
|
||||
catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
return policy;
|
||||
}
|
||||
};
|
||||
|
||||
public DataPolicy toDataPolicy() {
|
||||
switch (this) {
|
||||
case EMPTY:
|
||||
return DataPolicy.EMPTY;
|
||||
case NORMAL:
|
||||
return DataPolicy.NORMAL;
|
||||
case PRELOADED:
|
||||
return DataPolicy.PRELOADED;
|
||||
case PARTITION :
|
||||
return DataPolicy.PARTITION;
|
||||
case PERSISTENT_PARTITION:
|
||||
return DataPolicy.PERSISTENT_PARTITION;
|
||||
case REPLICATE:
|
||||
return DataPolicy.REPLICATE;
|
||||
case PERSISTENT_REPLICATE:
|
||||
return DataPolicy.PERSISTENT_REPLICATE;
|
||||
case DEFAULT:
|
||||
default:
|
||||
return DataPolicy.DEFAULT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataPolicy convert(String source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
source = source.toUpperCase();
|
||||
return Policy.getValue(source) == null ? null : Policy.getValue(source).toDataPolicy();
|
||||
public DataPolicy convert(String policyValue) {
|
||||
Policy policy = Policy.getValue(policyValue);
|
||||
return (policy == null ? null : policy.toDataPolicy());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -195,21 +195,27 @@ public class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V> imple
|
||||
* @param dataPolicy requested data policy
|
||||
*/
|
||||
protected void resolveDataPolicy(RegionFactory<K, V> regionFactory, Boolean persistent, String dataPolicy) {
|
||||
if (dataPolicy == null) {
|
||||
if (isPersistent()) {
|
||||
regionFactory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
|
||||
} else {
|
||||
regionFactory.setDataPolicy(DataPolicy.DEFAULT);
|
||||
}
|
||||
return;
|
||||
if (dataPolicy != null) {
|
||||
regionFactory.setDataPolicy(convertAndValidate(dataPolicy));
|
||||
}
|
||||
else {
|
||||
regionFactory.setDataPolicy(isPersistent() ? DataPolicy.PERSISTENT_REPLICATE : DataPolicy.DEFAULT);
|
||||
}
|
||||
}
|
||||
|
||||
private DataPolicy convertAndValidate(final String dataPolicy) {
|
||||
final DataPolicy dataPolicyType = new DataPolicyConverter().convert(dataPolicy);
|
||||
|
||||
Assert.notNull(dataPolicyType, String.format("Data policy %1$s is invalid", dataPolicy));
|
||||
|
||||
if (dataPolicyType.withPersistence()) {
|
||||
// NOTE isNotPersistent means the user explicitly set the persistent attribute for the Region to false,
|
||||
// and this conflicts with the Data Policy (via the that was set by the user, which indicates persistence.
|
||||
Assert.isTrue(!isNotPersistent(), String.format("Data policy %1$s is invalid when persistent is false",
|
||||
dataPolicy));
|
||||
}
|
||||
|
||||
DataPolicy dp = new DataPolicyConverter().convert(dataPolicy);
|
||||
Assert.notNull(dp, "Data policy " + dataPolicy + " is invalid");
|
||||
if (dp.withPersistence()) {
|
||||
Assert.isTrue(!isNotPersistent(), "Data policy " + dataPolicy + " is invalid when persistent is false");
|
||||
}
|
||||
regionFactory.setDataPolicy(dp);
|
||||
return dataPolicyType;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -343,7 +349,7 @@ public class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V> imple
|
||||
/**
|
||||
* Sets the dataPolicy as a String. Required to support property
|
||||
* placeholders
|
||||
* @param dataPolicy the dataPolicy name (NORMAL, PRELOADED, etc)
|
||||
* @param dataPolicyName the dataPolicy name (NORMAL, PRELOADED, etc)
|
||||
*/
|
||||
public void setDataPolicy(String dataPolicyName) {
|
||||
this.dataPolicy = dataPolicyName;
|
||||
|
||||
Reference in New Issue
Block a user