Skip to content

Commit

Permalink
feat(settings): add old names / alias support in JvmSettings IQSS#7000
Browse files Browse the repository at this point in the history
- When renaming older JVM settings, these should be still retrievable from an old config
- The AliasConfigSource now takes aliased settings from JvmSettings for this
- This is just for pure renaming but could be further extended with data manipulation.
  • Loading branch information
poikilotherm committed Jun 23, 2022
1 parent a2edcf0 commit 8cb8d97
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
46 changes: 44 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/settings/JvmSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import org.eclipse.microprofile.config.ConfigProvider;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* Enum to store each and every JVM-based setting as a reference,
Expand All @@ -14,9 +19,9 @@
* settings that are destined to be made at the JVM level.
*
* Further extensions of this class include
* - adding aliases when renaming settings,
* - adding predicates for validation and
* - offering injecting parameters into keys (as used with the file access subsystem).
* - offering injecting parameters into keys (as used with the file access subsystem) and
* - adding data manipulation for aliased config names.
*/
public enum JvmSettings {
// the upmost root scope - every setting shall start with it.
Expand Down Expand Up @@ -45,17 +50,54 @@ public enum JvmSettings {
private final String key;
private final String scopedKey;
private final JvmSettings parent;
private final List<String> oldNames;

JvmSettings(String key) {
this.key = key;
this.scopedKey = key;
this.parent = null;
this.oldNames = List.of();
}

JvmSettings(JvmSettings scope, String key) {
this.key = key;
this.scopedKey = scope.scopedKey + SCOPE_SEPARATOR + key;
this.parent = scope;
this.oldNames = List.of();
}

JvmSettings(JvmSettings scope, String key, String... oldNames) {
this.key = key;
this.scopedKey = scope.scopedKey + SCOPE_SEPARATOR + key;
this.parent = scope;
this.oldNames = Arrays.stream(oldNames).collect(Collectors.toUnmodifiableList());
}

private static final List<JvmSettings> aliased = new ArrayList<>();
static {
for (JvmSettings setting : JvmSettings.values()) {
if (!setting.oldNames.isEmpty()) {
aliased.add(setting);
}
}
}

/**
* Get all settings having old names to include them in {@link edu.harvard.iq.dataverse.settings.source.AliasConfigSource}
* @return List of settings with old alias names. Can be empty, but will not be null.
*/
public static List<JvmSettings> getAliasedSettings() {
return Collections.unmodifiableList(aliased);
}

/**
* Return a list of old names to be used as aliases for backward compatibility.
* Will return empty list if no old names present.
*
* @return List of old names, may be empty, but never null.
*/
public List<String> getOldNames() {
return oldNames;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package edu.harvard.iq.dataverse.settings.source;

import edu.harvard.iq.dataverse.settings.JvmSettings;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.spi.ConfigSource;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
Expand All @@ -16,9 +18,6 @@
/**
* Enable using an old name for a new config name.
* Usages will be logged and this source will ALWAYS stand back if the new name is used anywhere.
*
* By using a DbSettingConfigSource value (dataverse.settings.fromdb.XXX) as old name, we can
* alias a new name to an old db setting, enabling backward compatibility.
*/
public final class AliasConfigSource implements ConfigSource {

Expand All @@ -33,11 +32,20 @@ public AliasConfigSource() {
Properties aliasProps = readAliases(ALIASES_PROP_FILE);
// store in our aliases map
importAliases(aliasProps);
// also store all old names from JvmSettings
importJvmSettings(JvmSettings.getAliasedSettings());
} catch (IOException e) {
logger.info("Could not read from "+ALIASES_PROP_FILE+". Skipping MPCONFIG alias setup.");
}
}

void importJvmSettings(List<JvmSettings> aliasedSettings) {
aliasedSettings.forEach(
setting -> setting.getOldNames().forEach(
oldName -> aliases.put(setting.getScopedKey(), oldName)));
}


Properties readAliases(String filePath) throws IOException {
// get resource from classpath
ClassLoader classLoader = this.getClass().getClassLoader();
Expand Down

0 comments on commit 8cb8d97

Please sign in to comment.