Skip to content

Commit

Permalink
Fixed _. translation issue when loading settings for MQTT
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Jan 10, 2024
1 parent 11ac90f commit 364c37a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static Properties addEnvironment(Properties wrapped) {
Properties wrapper = new Properties(wrapped);

for (Map.Entry<String, String> entry : environment.entrySet()) {
String key = entry.getKey().replace("_", ".");
String key = entry.getKey().replace('_', '.');
LOGGER.debug("Added environment variable: {}", key);
wrapper.setProperty(key, entry.getValue());
}
Expand Down Expand Up @@ -141,7 +141,7 @@ public void setLogSensitiveData(boolean logSensitiveData) {
* @return prefix + propertyName
*/
private String getPropertyKey(String propertyName) {
return prefix + propertyName.replace("_", ".");
return prefix + propertyName.replace('_', '.');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,6 +40,7 @@ public class SettingsMigrator {

private final Map<String, Map<String, ReplaceList>> valueChanges = new HashMap<>();
private final Map<String, String> keyChanges = new HashMap<>();
private final Map<String, ValueChecker> valueCheckers = new HashMap<>();

private Map<String, ReplaceList> getReplaceValue(String key) {
return valueChanges.computeIfAbsent(key, t -> new TreeMap<>());
Expand Down Expand Up @@ -77,6 +79,18 @@ public void migrateOldSettings(Properties properties) {
for (Map.Entry<String, Map<String, ReplaceList>> change : valueChanges.entrySet()) {
migrateOldSettings(properties, change.getKey(), change.getValue());
}
for (Map.Entry<String, ValueChecker> checkEntry : valueCheckers.entrySet()) {
String key = checkEntry.getKey();
ValueChecker checker = checkEntry.getValue();
String valueOld = properties.getProperty(key);
if (valueOld != null) {
String valueNew = checker.getNewValue(valueOld);
if (!valueOld.equals(valueNew)) {
LOGGER.warn("Converting setting with key: {} from old value: {} to new value: {}", key, valueOld, valueNew);
properties.setProperty(key, valueNew);
}
}
}
}

private void migrateOldSettings(Properties properties, String key, Map<String, ReplaceList> replaces) {
Expand All @@ -98,7 +112,7 @@ private void migrateOldSettings(Properties properties, String key, Map<String, R
}

private void migrateOldSettings(Properties properties, String oldKey, String newKey) {
Object oldValue = properties.get(oldKey);
String oldValue = properties.getProperty(oldKey);
if (oldValue != null) {
LOGGER.warn("Converting setting with old key: {} to new key: {} with value: {}", oldKey, newKey, oldValue);
properties.remove(oldKey);
Expand Down Expand Up @@ -135,4 +149,8 @@ public ReplaceList copy() {

}

private static interface ValueChecker {

public String getNewValue(String oldValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ public ConfigWrapper(Settings wrappedSettings) {

@Override
public void setProperty(String name, String value) {
wrappedSettings.set(name, value);
wrappedSettings.set(name.replace('_', '.'), value);
}

@Override
public String getProperty(String name) {
return wrappedSettings.get(name, (String) null);
return wrappedSettings.get(name.replace('_', '.'), (String) null);
}

@Override
public String getProperty(String name, String defaultValue) {
return wrappedSettings.get(name, defaultValue);
return wrappedSettings.get(name.replace('_', '.'), defaultValue);
}

@Override
public int intProp(String propertyName, int defaultValue) {
return wrappedSettings.getInt(propertyName, defaultValue);
return wrappedSettings.getInt(propertyName.replace('_', '.'), defaultValue);
}

@Override
public boolean boolProp(String propertyName, boolean defaultValue) {
return wrappedSettings.getBoolean(propertyName, defaultValue);
return wrappedSettings.getBoolean(propertyName.replace('_', '.'), defaultValue);
}

@Override
Expand Down

0 comments on commit 364c37a

Please sign in to comment.