Update configme (#1631)

* Upgrade to ConfigMe 1.0.1
* Use ConfigMe reader whenever possible, minor simplifications
This commit is contained in:
ljacqu
2018-09-09 15:45:00 +02:00
committed by GitHub
parent 6676a39447
commit ee764c0a6e
42 changed files with 516 additions and 555 deletions
@@ -39,7 +39,7 @@ public class JarMessageSource {
}
private static String getString(String path, PropertyReader reader) {
return reader == null ? null : reader.getTypedObject(path, String.class);
return reader == null ? null : reader.getString(path);
}
private static MessageMigraterPropertyReader loadJarFile(String jarPath) {
@@ -0,0 +1,43 @@
package fr.xephi.authme.message.updater;
import ch.jalu.configme.configurationdata.ConfigurationDataImpl;
import ch.jalu.configme.properties.Property;
import ch.jalu.configme.resource.PropertyReader;
import fr.xephi.authme.message.MessageKey;
import java.util.List;
import java.util.Map;
public class MessageKeyConfigurationData extends ConfigurationDataImpl {
/**
* Constructor.
*
* @param propertyListBuilder property list builder for message key properties
* @param allComments registered comments
*/
public MessageKeyConfigurationData(MessageUpdater.MessageKeyPropertyListBuilder propertyListBuilder,
Map<String, List<String>> allComments) {
super(propertyListBuilder.getAllProperties(), allComments);
}
@Override
public void initializeValues(PropertyReader reader) {
getAllMessageProperties().stream()
.filter(prop -> prop.isPresent(reader))
.forEach(prop -> setValue(prop, prop.determineValue(reader)));
}
@SuppressWarnings("unchecked")
public List<Property<String>> getAllMessageProperties() {
return (List) getProperties();
}
public String getMessage(MessageKey messageKey) {
return getValue(new MessageUpdater.MessageKeyProperty(messageKey));
}
public void setMessage(MessageKey messageKey, String message) {
setValue(new MessageUpdater.MessageKeyProperty(messageKey), message);
}
}
@@ -12,20 +12,19 @@ import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* Implementation of {@link PropertyReader} which can read a file or a stream with
* a specified charset.
*/
public final class MessageMigraterPropertyReader implements PropertyReader {
final class MessageMigraterPropertyReader implements PropertyReader {
public static final Charset CHARSET = StandardCharsets.UTF_8;
private static final Charset CHARSET = StandardCharsets.UTF_8;
private Map<String, Object> root;
/** See same field in {@link ch.jalu.configme.resource.YamlFileReader} for details. */
private boolean hasObjectAsRoot = false;
private MessageMigraterPropertyReader(Map<String, Object> valuesMap) {
root = valuesMap;
@@ -38,14 +37,11 @@ public final class MessageMigraterPropertyReader implements PropertyReader {
* @return the created property reader
*/
public static MessageMigraterPropertyReader loadFromFile(File file) {
Map<String, Object> valuesMap;
try (InputStream is = new FileInputStream(file)) {
valuesMap = readStreamToMap(is);
return loadFromStream(is);
} catch (IOException e) {
throw new IllegalStateException("Error while reading file '" + file + "'", e);
}
return new MessageMigraterPropertyReader(valuesMap);
}
public static MessageMigraterPropertyReader loadFromStream(InputStream inputStream) {
@@ -53,10 +49,20 @@ public final class MessageMigraterPropertyReader implements PropertyReader {
return new MessageMigraterPropertyReader(valuesMap);
}
@Override
public boolean contains(String path) {
return getObject(path) != null;
}
@Override
public Set<String> getKeys(boolean b) {
throw new UnsupportedOperationException();
}
@Override
public Object getObject(String path) {
if (path.isEmpty()) {
return hasObjectAsRoot ? root.get("") : root;
return root.get("");
}
Object node = root;
String[] keys = path.split("\\.");
@@ -70,66 +76,29 @@ public final class MessageMigraterPropertyReader implements PropertyReader {
}
@Override
public <T> T getTypedObject(String path, Class<T> clazz) {
Object value = getObject(path);
if (clazz.isInstance(value)) {
return clazz.cast(value);
}
return null;
public String getString(String path) {
Object o = getObject(path);
return o instanceof String ? (String) o : null;
}
@Override
public void set(String path, Object value) {
Objects.requireNonNull(path);
if (path.isEmpty()) {
root.clear();
root.put("", value);
hasObjectAsRoot = true;
} else if (hasObjectAsRoot) {
throw new ConfigMeException("The root path is a bean property; you cannot set values to any subpath. "
+ "Modify the bean at the root or set a new one instead.");
} else {
setValueInChildPath(path, value);
}
}
/**
* Sets the value at the given path. This method is used when the root is a map and not a specific object.
*
* @param path the path to set the value at
* @param value the value to set
*/
@SuppressWarnings("unchecked")
private void setValueInChildPath(String path, Object value) {
Map<String, Object> node = root;
String[] keys = path.split("\\.");
for (int i = 0; i < keys.length - 1; ++i) {
Object child = node.get(keys[i]);
if (child instanceof Map<?, ?>) {
node = (Map<String, Object>) child;
} else { // child is null or some other value - replace with map
Map<String, Object> newEntry = new HashMap<>();
node.put(keys[i], newEntry);
if (value == null) {
// For consistency, replace whatever value/null here with an empty map,
// but if the value is null our work here is done.
return;
}
node = newEntry;
}
}
// node now contains the parent map (existing or newly created)
if (value == null) {
node.remove(keys[keys.length - 1]);
} else {
node.put(keys[keys.length - 1], value);
}
public Integer getInt(String path) {
throw new UnsupportedOperationException();
}
@Override
public void reload() {
throw new UnsupportedOperationException("Reload not supported by this implementation");
public Double getDouble(String path) {
throw new UnsupportedOperationException();
}
@Override
public Boolean getBoolean(String path) {
throw new UnsupportedOperationException();
}
@Override
public List<?> getList(String path) {
throw new UnsupportedOperationException();
}
private static Map<String, Object> readStreamToMap(InputStream inputStream) {
@@ -1,10 +1,10 @@
package fr.xephi.authme.message.updater;
import ch.jalu.configme.SettingsManager;
import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.configurationdata.PropertyListBuilder;
import ch.jalu.configme.properties.Property;
import ch.jalu.configme.properties.StringProperty;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.PropertyResource;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
@@ -20,21 +20,15 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static java.util.Collections.singletonList;
/**
* Migrates the used messages file to a complete, up-to-date version when necessary.
*/
public class MessageUpdater {
/**
* Configuration data object for all message keys incl. comments associated to sections.
*/
private static final ConfigurationData CONFIGURATION_DATA = buildConfigurationData();
public static ConfigurationData getConfigurationData() {
return CONFIGURATION_DATA;
}
/**
* Applies any necessary migrations to the user's messages file and saves it if it has been modified.
*
@@ -57,52 +51,57 @@ public class MessageUpdater {
*/
private boolean migrateAndSave(File userFile, JarMessageSource jarMessageSource) {
// YamlConfiguration escapes all special characters when saving, making the file hard to use, so use ConfigMe
MessageKeyConfigurationData configurationData = createConfigurationData();
PropertyResource userResource = new MigraterYamlFileResource(userFile);
PropertyReader reader = userResource.createReader();
configurationData.initializeValues(reader);
// Step 1: Migrate any old keys in the file to the new paths
boolean movedOldKeys = migrateOldKeys(userResource);
boolean movedOldKeys = migrateOldKeys(reader, configurationData);
// Step 2: Perform newer migrations
boolean movedNewerKeys = migrateKeys(userResource);
boolean movedNewerKeys = migrateKeys(reader, configurationData);
// Step 3: Take any missing messages from the message files shipped in the AuthMe JAR
boolean addedMissingKeys = addMissingKeys(jarMessageSource, userResource);
boolean addedMissingKeys = addMissingKeys(jarMessageSource, configurationData);
if (movedOldKeys || movedNewerKeys || addedMissingKeys) {
backupMessagesFile(userFile);
SettingsManager settingsManager = new SettingsManager(userResource, null, CONFIGURATION_DATA);
settingsManager.save();
userResource.exportProperties(configurationData);
ConsoleLogger.debug("Successfully saved {0}", userFile);
return true;
}
return false;
}
private boolean migrateKeys(PropertyResource userResource) {
return moveIfApplicable(userResource, "misc.two_factor_create", MessageKey.TWO_FACTOR_CREATE.getKey());
private boolean migrateKeys(PropertyReader propertyReader, MessageKeyConfigurationData configurationData) {
return moveIfApplicable(propertyReader, configurationData,
"misc.two_factor_create", MessageKey.TWO_FACTOR_CREATE);
}
private static boolean moveIfApplicable(PropertyResource resource, String oldPath, String newPath) {
if (resource.getString(newPath) == null && resource.getString(oldPath) != null) {
resource.setValue(newPath, resource.getString(oldPath));
private static boolean moveIfApplicable(PropertyReader reader, MessageKeyConfigurationData configurationData,
String oldPath, MessageKey messageKey) {
if (configurationData.getMessage(messageKey) == null && reader.getString(oldPath) != null) {
configurationData.setMessage(messageKey, reader.getString(oldPath));
return true;
}
return false;
}
private boolean migrateOldKeys(PropertyResource userResource) {
boolean hasChange = OldMessageKeysMigrater.migrateOldPaths(userResource);
private boolean migrateOldKeys(PropertyReader propertyReader, MessageKeyConfigurationData configurationData) {
boolean hasChange = OldMessageKeysMigrater.migrateOldPaths(propertyReader, configurationData);
if (hasChange) {
ConsoleLogger.info("Old keys have been moved to the new ones in your messages_xx.yml file");
}
return hasChange;
}
private boolean addMissingKeys(JarMessageSource jarMessageSource, PropertyResource userResource) {
private boolean addMissingKeys(JarMessageSource jarMessageSource, MessageKeyConfigurationData configurationData) {
List<String> addedKeys = new ArrayList<>();
for (Property<?> property : CONFIGURATION_DATA.getProperties()) {
for (Property<String> property : configurationData.getAllMessageProperties()) {
final String key = property.getPath();
if (userResource.getString(key) == null) {
userResource.setValue(key, jarMessageSource.getMessageFromJar(property));
if (configurationData.getValue(property) == null) {
configurationData.setValue(property, jarMessageSource.getMessageFromJar(property));
addedKeys.add(key);
}
}
@@ -129,40 +128,68 @@ public class MessageUpdater {
*
* @return the configuration data to export with
*/
private static ConfigurationData buildConfigurationData() {
Map<String, String[]> comments = ImmutableMap.<String, String[]>builder()
.put("registration", new String[]{"Registration"})
.put("password", new String[]{"Password errors on registration"})
.put("login", new String[]{"Login"})
.put("error", new String[]{"Errors"})
.put("antibot", new String[]{"AntiBot"})
.put("unregister", new String[]{"Unregister"})
.put("misc", new String[]{"Other messages"})
.put("session", new String[]{"Session messages"})
.put("on_join_validation", new String[]{"Error messages when joining"})
.put("email", new String[]{"Email"})
.put("recovery", new String[]{"Password recovery by email"})
.put("captcha", new String[]{"Captcha"})
.put("verification", new String[]{"Verification code"})
.put("time", new String[]{"Time units"})
.put("two_factor", new String[]{"Two-factor authentication"})
public static MessageKeyConfigurationData createConfigurationData() {
Map<String, String> comments = ImmutableMap.<String, String>builder()
.put("registration", "Registration")
.put("password", "Password errors on registration")
.put("login", "Login")
.put("error", "Errors")
.put("antibot", "AntiBot")
.put("unregister", "Unregister")
.put("misc", "Other messages")
.put("session", "Session messages")
.put("on_join_validation", "Error messages when joining")
.put("email", "Email")
.put("recovery", "Password recovery by email")
.put("captcha", "Captcha")
.put("verification", "Verification code")
.put("time", "Time units")
.put("two_factor", "Two-factor authentication")
.build();
Set<String> addedKeys = new HashSet<>();
PropertyListBuilder builder = new PropertyListBuilder();
MessageKeyPropertyListBuilder builder = new MessageKeyPropertyListBuilder();
// Add one key per section based on the comments map above so that the order is clear
for (String path : comments.keySet()) {
MessageKey key = Arrays.stream(MessageKey.values()).filter(p -> p.getKey().startsWith(path + "."))
.findFirst().orElseThrow(() -> new IllegalStateException(path));
builder.add(new StringProperty(key.getKey(), ""));
builder.addMessageKey(key);
addedKeys.add(key.getKey());
}
// Add all remaining keys to the property list builder
Arrays.stream(MessageKey.values())
.filter(key -> !addedKeys.contains(key.getKey()))
.forEach(key -> builder.add(new StringProperty(key.getKey(), "")));
.forEach(builder::addMessageKey);
return new ConfigurationData(builder.create(), comments);
// Create ConfigurationData instance
Map<String, List<String>> commentsMap = comments.entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> singletonList(e.getValue())));
return new MessageKeyConfigurationData(builder, commentsMap);
}
static final class MessageKeyProperty extends StringProperty {
MessageKeyProperty(MessageKey messageKey) {
super(messageKey.getKey(), "");
}
@Override
protected String getFromReader(PropertyReader reader) {
return reader.getString(getPath());
}
}
static final class MessageKeyPropertyListBuilder {
private PropertyListBuilder propertyListBuilder = new PropertyListBuilder();
void addMessageKey(MessageKey key) {
propertyListBuilder.add(new MessageKeyProperty(key));
}
@SuppressWarnings("unchecked")
List<MessageKeyProperty> getAllProperties() {
return (List) propertyListBuilder.create();
}
}
}
@@ -1,41 +1,30 @@
package fr.xephi.authme.message.updater;
import ch.jalu.configme.beanmapper.leafproperties.LeafPropertiesGenerator;
import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.exception.ConfigMeException;
import ch.jalu.configme.properties.Property;
import ch.jalu.configme.resource.PropertyPathTraverser;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileResource;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;
import static fr.xephi.authme.message.updater.MessageMigraterPropertyReader.CHARSET;
/**
* Extension of {@link YamlFileResource} to fine-tune the export style
* and to be able to specify the character encoding.
* Extension of {@link YamlFileResource} to fine-tune the export style.
*/
public class MigraterYamlFileResource extends YamlFileResource {
private static final String INDENTATION = " ";
private final File file;
private Yaml singleQuoteYaml;
public MigraterYamlFileResource(File file) {
super(file, MessageMigraterPropertyReader.loadFromFile(file), new LeafPropertiesGenerator());
this.file = file;
super(file);
}
@Override
protected Yaml getSingleQuoteYaml() {
public PropertyReader createReader() {
return MessageMigraterPropertyReader.loadFromFile(getFile());
}
@Override
protected Yaml createNewYaml() {
if (singleQuoteYaml == null) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
@@ -47,57 +36,4 @@ public class MigraterYamlFileResource extends YamlFileResource {
}
return singleQuoteYaml;
}
@Override
public void exportProperties(ConfigurationData configurationData) {
try (FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(fos, CHARSET)) {
PropertyPathTraverser pathTraverser = new PropertyPathTraverser(configurationData);
for (Property<?> property : convertPropertiesToExportableTypes(configurationData.getProperties())) {
List<PropertyPathTraverser.PathElement> pathElements = pathTraverser.getPathElements(property);
for (PropertyPathTraverser.PathElement pathElement : pathElements) {
writeComments(writer, pathElement.indentationLevel, pathElement.comments);
writer.append("\n")
.append(indent(pathElement.indentationLevel))
.append(pathElement.name)
.append(":");
}
writer.append(" ")
.append(toYaml(property, pathElements.get(pathElements.size() - 1).indentationLevel));
}
writer.flush();
writer.close();
} catch (IOException e) {
throw new ConfigMeException("Could not save config to '" + file.getPath() + "'", e);
} finally {
singleQuoteYaml = null;
}
}
private void writeComments(Writer writer, int indentation, String[] comments) throws IOException {
if (comments.length == 0) {
return;
}
String commentStart = "\n" + indent(indentation) + "# ";
for (String comment : comments) {
writer.append(commentStart).append(comment);
}
}
private <T> String toYaml(Property<T> property, int indent) {
Object value = property.getValue(this);
String representation = transformValue(property, value);
String[] lines = representation.split("\\n");
return String.join("\n" + indent(indent), lines);
}
private static String indent(int level) {
String result = "";
for (int i = 0; i < level; i++) {
result += INDENTATION;
}
return result;
}
}
@@ -1,6 +1,6 @@
package fr.xephi.authme.message.updater;
import ch.jalu.configme.resource.PropertyResource;
import ch.jalu.configme.resource.PropertyReader;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import fr.xephi.authme.message.MessageKey;
@@ -16,7 +16,6 @@ import static com.google.common.collect.ImmutableMap.of;
*/
final class OldMessageKeysMigrater {
@VisibleForTesting
static final Map<MessageKey, String> KEYS_TO_OLD_PATH = ImmutableMap.<MessageKey, String>builder()
.put(MessageKey.LOGIN_SUCCESS, "login")
@@ -130,23 +129,26 @@ final class OldMessageKeysMigrater {
/**
* Migrates any existing old key paths to their new paths if no text has been defined for the new key.
*
* @param resource the resource to modify and read from
* @param reader the property reader to get values from
* @param configurationData the configuration data to write to
* @return true if at least one message could be migrated, false otherwise
*/
static boolean migrateOldPaths(PropertyResource resource) {
static boolean migrateOldPaths(PropertyReader reader, MessageKeyConfigurationData configurationData) {
boolean wasPropertyMoved = false;
for (Map.Entry<MessageKey, String> migrationEntry : KEYS_TO_OLD_PATH.entrySet()) {
wasPropertyMoved |= moveIfApplicable(resource, migrationEntry.getKey(), migrationEntry.getValue());
wasPropertyMoved |= moveIfApplicable(reader, configurationData,
migrationEntry.getKey(), migrationEntry.getValue());
}
return wasPropertyMoved;
}
private static boolean moveIfApplicable(PropertyResource resource, MessageKey messageKey, String oldPath) {
if (resource.getString(messageKey.getKey()) == null) {
String textAtOldPath = resource.getString(oldPath);
private static boolean moveIfApplicable(PropertyReader reader, MessageKeyConfigurationData configurationData,
MessageKey messageKey, String oldPath) {
if (configurationData.getMessage(messageKey) == null) {
String textAtOldPath = reader.getString(oldPath);
if (textAtOldPath != null) {
textAtOldPath = replaceOldPlaceholders(messageKey, textAtOldPath);
resource.setValue(messageKey.getKey(), textAtOldPath);
configurationData.setMessage(messageKey, textAtOldPath);
return true;
}
}