com.hazelcast.spi.merge.SplitBrainMergePolicyProvider Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2018, 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.spi.merge;
import com.hazelcast.config.InvalidConfigurationException;
import com.hazelcast.spi.NodeEngine;
import com.hazelcast.spi.annotation.Beta;
import com.hazelcast.util.ConstructorFunction;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import static com.hazelcast.nio.ClassLoaderUtil.newInstance;
import static com.hazelcast.util.ConcurrencyUtil.getOrPutIfAbsent;
/**
* A provider for {@link SplitBrainMergePolicy} instances.
*
* Registers out-of-the-box merge policies by their fully qualified and simple class name.
*
* @since 3.10
*/
@Beta
public final class SplitBrainMergePolicyProvider {
private static final Map OUT_OF_THE_BOX_MERGE_POLICIES;
static {
OUT_OF_THE_BOX_MERGE_POLICIES = new HashMap();
addPolicy(DiscardMergePolicy.class, new DiscardMergePolicy());
addPolicy(HigherHitsMergePolicy.class, new HigherHitsMergePolicy());
addPolicy(HyperLogLogMergePolicy.class, new HyperLogLogMergePolicy());
addPolicy(LatestAccessMergePolicy.class, new LatestAccessMergePolicy());
addPolicy(LatestUpdateMergePolicy.class, new LatestUpdateMergePolicy());
addPolicy(PassThroughMergePolicy.class, new PassThroughMergePolicy());
addPolicy(PutIfAbsentMergePolicy.class, new PutIfAbsentMergePolicy());
}
private final NodeEngine nodeEngine;
private final ConcurrentMap mergePolicyMap
= new ConcurrentHashMap();
private final ConstructorFunction policyConstructorFunction
= new ConstructorFunction() {
@Override
public SplitBrainMergePolicy createNew(String className) {
try {
return newInstance(nodeEngine.getConfigClassLoader(), className);
} catch (Exception e) {
throw new InvalidConfigurationException("Invalid SplitBrainMergePolicy: " + className, e);
}
}
};
/**
* Constructs a new provider for {@link SplitBrainMergePolicy} classes.
*
* @param nodeEngine the {@link NodeEngine} to retrieve the classloader from
*/
public SplitBrainMergePolicyProvider(NodeEngine nodeEngine) {
this.nodeEngine = nodeEngine;
this.mergePolicyMap.putAll(OUT_OF_THE_BOX_MERGE_POLICIES);
}
/**
* Resolves the {@link SplitBrainMergePolicy} class by its classname.
*
* @param className the merge policy classname to resolve
* @return the resolved {@link SplitBrainMergePolicy} class
* @throws InvalidConfigurationException when the classname could not be resolved
*/
public SplitBrainMergePolicy getMergePolicy(String className) {
if (className == null) {
throw new InvalidConfigurationException("Class name is mandatory!");
}
return getOrPutIfAbsent(mergePolicyMap, className, policyConstructorFunction);
}
private static void addPolicy(Class clazz, T policy) {
OUT_OF_THE_BOX_MERGE_POLICIES.put(clazz.getName(), policy);
OUT_OF_THE_BOX_MERGE_POLICIES.put(clazz.getSimpleName(), policy);
}
}