com.ocadotechnology.config.ConfigSettingCollector Maven / Gradle / Ivy
/*
* Copyright © 2017-2023 Ocado (Ocava)
*
* 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.ocadotechnology.config;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Queue;
import java.util.Set;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.ocadotechnology.wrappers.Pair;
/**
* Collects config settings against the source of those settings.
*/
class ConfigSettingCollector {
private final Map> valueAndFileByKey = new LinkedHashMap<>();
private final Set secretKeys = new LinkedHashSet<>();
/**
* Adds enumerations that should be checked to determine whether settings should be collected.
*
* @param configEnums Enumerations that setting keys will be checked against before collecting them; any annotated
* with @SecretConfig will be excluded.
*/
void addSecrets(ImmutableSet>> configEnums) {
for (Class extends Enum>> clazz : configEnums) {
secretKeys.addAll(resolveSecretKeysForClass(clazz));
}
// Defensively remove any existing settings that we now know to be secrets.
valueAndFileByKey.keySet().removeAll(secretKeys);
}
private Set resolveSecretKeysForClass(Class extends Enum>> inputClazz) {
Set result = new LinkedHashSet<>();
Queue>>> qualifierAndClassQueue = new LinkedList<>();
qualifierAndClassQueue.offer(Pair.of(inputClazz.getSimpleName(), inputClazz));
while (!qualifierAndClassQueue.isEmpty()) {
Pair>> qualifierAndClass = qualifierAndClassQueue.poll();
String qualifier = qualifierAndClass.a();
Class extends Enum>> clazz = qualifierAndClass.b();
for (Enum> enumeration : clazz.getEnumConstants()) {
try {
if (clazz.getField(enumeration.name()).isAnnotationPresent(SecretConfig.class)) {
result.add(qualifier + "." + enumeration.name());
}
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
for (Class> subEnumeration : clazz.getDeclaredClasses()) {
String subQualifier = qualifier + "." + subEnumeration.getSimpleName();
Preconditions.checkState(subEnumeration.isEnum());
qualifierAndClassQueue.offer(Pair.of(subQualifier, (Class extends Enum>>) subEnumeration));
}
}
return result;
}
/**
* Accept the properties associated with the given file name, collecting those settings whose keys are not annotated
* with @SecretConfig.
*/
public void accept(String fileName, Properties properties) {
properties.forEach((objKey, objValue) -> {
String key = (String)objKey;
String value = (String) objValue;
if (!secretKeys.contains(key)) {
valueAndFileByKey.put(key, Pair.of(value, fileName));
}
});
}
@Override
public String toString() {
Map propertiesByFile = new LinkedHashMap<>();
for (Map.Entry> entry : valueAndFileByKey.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().a();
String fileName = entry.getValue().b();
propertiesByFile.computeIfAbsent(fileName, k -> new Properties()).put(key, value);
}
StringBuilder builder = new StringBuilder();
for (Map.Entry entry: propertiesByFile.entrySet()) {
builder.append(entry.getKey());
builder.append("\n");
builder.append(propertiesToString(entry.getValue()));
builder.append("\n");
}
return builder.toString();
}
private String propertiesToString(Properties properties) {
StringBuilder builder = new StringBuilder();
for (Entry
© 2015 - 2024 Weber Informatics LLC | Privacy Policy