Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
*
* 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 com.hazelcast.internal.config;
import com.hazelcast.config.InvalidConfigurationException;
import com.hazelcast.config.MapConfig;
import com.hazelcast.spi.merge.MergingExpirationTime;
import com.hazelcast.spi.merge.MergingLastStoredTime;
import com.hazelcast.spi.merge.MergingValue;
import com.hazelcast.spi.merge.MergingView;
import com.hazelcast.spi.merge.SplitBrainMergePolicy;
import com.hazelcast.spi.merge.SplitBrainMergePolicyProvider;
import com.hazelcast.spi.merge.SplitBrainMergeTypes;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.List;
/**
* Validates a merge policy instance.
*/
public final class MergePolicyValidator {
private MergePolicyValidator() {
}
/**
* Checks the merge policy configuration
* of the given {@link MapConfig}.
*
* @param mapConfig the {@link MapConfig}
* @param mergePolicyProvider the {@link
* SplitBrainMergePolicyProvider} to resolve merge policy classes
*/
static void checkMapMergePolicy(MapConfig mapConfig,
SplitBrainMergePolicyProvider mergePolicyProvider) {
String mergePolicyClassName = mapConfig.getMergePolicyConfig().getPolicy();
SplitBrainMergePolicy mergePolicyInstance = mergePolicyProvider.getMergePolicy(mergePolicyClassName);
List requiredMergeTypes =
checkSplitBrainMergePolicy(SplitBrainMergeTypes.MapMergeTypes.class, mergePolicyInstance);
if (!mapConfig.isStatisticsEnabled() && requiredMergeTypes != null) {
checkMapMergePolicyWhenStatisticsAreDisabled(mergePolicyClassName, requiredMergeTypes);
}
}
/**
* Checks if the configured merge policy requires merge types,
* which are just available if map statistics are enabled.
*
* @param mergePolicyClass the name of the configured merge policy class
* @param requiredMergeTypes the required merge types of the configured merge policy
*/
private static void checkMapMergePolicyWhenStatisticsAreDisabled(String mergePolicyClass,
List requiredMergeTypes) {
for (Class> requiredMergeType : requiredMergeTypes) {
if (MergingLastStoredTime.class.isAssignableFrom(requiredMergeType)
|| MergingExpirationTime.class.isAssignableFrom(requiredMergeType)) {
throw new InvalidConfigurationException("The merge policy " + mergePolicyClass
+ " requires the merge type " + requiredMergeType.getName()
+ ", which is just provided if the map statistics are enabled.");
}
}
}
/**
* Checks if the provided {@code mergeTypes} satisfy the requirements of a
* given merge policy.
*
* @param mergeTypes the provided merge types
* @param mergePolicyProvider the {@link SplitBrainMergePolicyProvider} to resolve merge policy classes
* @param mergePolicyClassName the merge policy class name
* @throws InvalidConfigurationException if the given merge policy is no {@link SplitBrainMergePolicy}
*/
static void checkMergeTypeProviderHasRequiredTypes(Class extends MergingValue> mergeTypes,
SplitBrainMergePolicyProvider mergePolicyProvider,
String mergePolicyClassName) {
if (mergePolicyProvider == null) {
return;
}
SplitBrainMergePolicy mergePolicy = getMergePolicyInstance(mergePolicyProvider,
mergePolicyClassName);
checkSplitBrainMergePolicy(mergeTypes, mergePolicy);
}
private static SplitBrainMergePolicy getMergePolicyInstance(SplitBrainMergePolicyProvider mergePolicyProvider,
String mergePolicyClassName) {
try {
return mergePolicyProvider.getMergePolicy(mergePolicyClassName);
} catch (InvalidConfigurationException e) {
throw new InvalidConfigurationException("Merge policy must be an instance of SplitBrainMergePolicy,"
+ " but was " + mergePolicyClassName, e.getCause());
}
}
/**
* Checks if the provided {@code mergeTypes} satisfy the requirements of a
* given merge policy.
*
* @param mergeTypes the provided merge types
* @param mergePolicyInstance the {@link SplitBrainMergePolicy} instance
* @return a list of the required merge types
*/
private static List checkSplitBrainMergePolicy(Class extends MergingValue> mergeTypes,
SplitBrainMergePolicy mergePolicyInstance) {
List requiredMergeTypes = new ArrayList<>();
Class> mergePolicyClass = mergePolicyInstance.getClass();
String mergePolicyClassName = mergePolicyClass.getName();
// iterate over the complete class hierarchy of a merge policy, to check all its generics
do {
checkSplitBrainMergePolicyGenerics(requiredMergeTypes, mergeTypes, mergePolicyClassName, mergePolicyClass);
mergePolicyClass = mergePolicyClass.getSuperclass();
} while (mergePolicyClass != null);
return requiredMergeTypes;
}
private static void checkSplitBrainMergePolicyGenerics(List requiredMergeTypes, Class providedMergeTypes,
String mergePolicyClassName, Class> mergePolicyClass) {
for (TypeVariable extends Class>> classTypeVariable : mergePolicyClass.getTypeParameters()) {
// checks merge policies like
// CustomMergePolicy> implements SplitBrainMergePolicy
for (Type requireMergeType : classTypeVariable.getBounds()) {
checkRequiredMergeType(requiredMergeTypes, providedMergeTypes, mergePolicyClassName, requireMergeType);
}
}
for (Type type : mergePolicyClass.getGenericInterfaces()) {
// checks merge policies like
// CustomMergePolicy implements SplitBrainMergePolicy