querqy.elasticsearch.ConfigUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of querqy-elasticsearch Show documentation
Show all versions of querqy-elasticsearch Show documentation
Querqy library for query rewriting: Querqy for Elasticsearch
package querqy.elasticsearch;
import org.elasticsearch.SpecialPermission;
import querqy.trie.TrieMap;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
public interface ConfigUtils {
static String getStringArg(final Map config, final String name, final String defaultValue) {
final String value = (String) config.get(name);
return value == null ? defaultValue : value;
}
static Optional getStringArg(final Map config, final String name) {
return Optional.ofNullable((String) config.get(name));
}
static > Optional getEnumArg(final Map config, final String name,
final Class enumClass) {
final String value = (String) config.get(name);
return (value == null) ? Optional.empty() : Optional.of(Enum.valueOf(enumClass, value));
}
static T getArg(final Map config, final String name, final T defaultValue) {
return (T) config.getOrDefault(name, defaultValue);
}
static TrieMap getTrieSetArg(final Map config, final String name) {
final TrieMap result = new TrieMap<>();
final Collection collectionArg = (Collection) config.get(name);
if (collectionArg != null) {
for (final String word : new HashSet<>(collectionArg)) {
result.put(word, Boolean.TRUE);
}
}
return result;
}
static V getInstanceFromArg(final Map config, final String name, final V defaultValue) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new SpecialPermission());
}
return AccessController.doPrivileged(
(PrivilegedAction) () -> {
final String classField = (String) config.get(name);
if (classField == null) {
return defaultValue;
}
final String className = classField.trim();
if (className.isEmpty()) {
return defaultValue;
}
try {
return (V) Class.forName(className).getDeclaredConstructor().newInstance();
} catch (final Exception e) {
throw new RuntimeException(e);
}
});
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy