diff options
author | Luck <git@lucko.me> | 2022-06-28 20:24:46 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2022-06-28 20:24:46 +0100 |
commit | 3b93798a312a0b63b7d80a6b9a7ae3150f8fdaca (patch) | |
tree | 216309d70c359d4eac9aad9d0daaf2b7697a1d3e /spark-common/src/main/java/me/lucko | |
parent | 7f422948755c2988180c32fda9554ea1531949c2 (diff) | |
download | spark-3b93798a312a0b63b7d80a6b9a7ae3150f8fdaca.tar.gz spark-3b93798a312a0b63b7d80a6b9a7ae3150f8fdaca.tar.bz2 spark-3b93798a312a0b63b7d80a6b9a7ae3150f8fdaca.zip |
Fix config filtering on nested files
Diffstat (limited to 'spark-common/src/main/java/me/lucko')
4 files changed, 120 insertions, 97 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java index 5a14382..501851a 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java @@ -21,24 +21,10 @@ package me.lucko.spark.common.platform.serverconfig; import com.google.common.collect.ImmutableMap; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import java.io.BufferedReader; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Arrays; import java.util.Collection; -import java.util.Collections; -import java.util.Deque; -import java.util.LinkedList; import java.util.Map; -import java.util.stream.Collectors; /** * Abstract implementation of {@link ServerConfigProvider}. @@ -48,31 +34,23 @@ import java.util.stream.Collectors; */ public abstract class AbstractServerConfigProvider implements ServerConfigProvider { private final Map<String, ConfigParser> files; - private final Collection<String> hiddenPaths; - - private final Gson gson; + private final ExcludedConfigFilter hiddenPathFilters; protected AbstractServerConfigProvider(Map<String, ConfigParser> files, Collection<String> hiddenPaths) { this.files = files; - this.hiddenPaths = hiddenPaths; - - GsonBuilder gson = new GsonBuilder(); - customiseGson(gson); - this.gson = gson.create(); + this.hiddenPathFilters = new ExcludedConfigFilter(hiddenPaths); } @Override public final Map<String, JsonElement> loadServerConfigurations() { ImmutableMap.Builder<String, JsonElement> builder = ImmutableMap.builder(); - this.files.forEach((path, reader) -> { + this.files.forEach((path, parser) -> { try { - JsonElement json = load(path, reader); + JsonElement json = parser.load(path, this.hiddenPathFilters); if (json == null) { return; } - - delete(json, this.hiddenPaths); builder.put(path, json); } catch (Exception e) { e.printStackTrace(); @@ -82,71 +60,4 @@ public abstract class AbstractServerConfigProvider implements ServerConfigProvid return builder.build(); } - private JsonElement load(String path, ConfigParser parser) throws IOException { - Map<String, Object> values = parser.parse(path); - if (values == null) { - return null; - } - - return this.gson.toJsonTree(values); - } - - protected void customiseGson(GsonBuilder gson) { - - } - - /** - * Deletes the given paths from the json element. - * - * @param json the json element - * @param paths the paths to delete - */ - private static void delete(JsonElement json, Collection<String> paths) { - for (String path : paths) { - Deque<String> pathDeque = new LinkedList<>(Arrays.asList(path.split("\\."))); - delete(json, pathDeque); - } - } - - private static void delete(JsonElement json, Deque<String> path) { - if (path.isEmpty()) { - return; - } - if (!json.isJsonObject()) { - return; - } - - JsonObject jsonObject = json.getAsJsonObject(); - String expected = path.removeFirst().replace("<dot>", "."); - - Collection<String> keys; - if (expected.equals("*")) { - keys = jsonObject.entrySet().stream() - .map(Map.Entry::getKey) - .collect(Collectors.toList()); - } else if (expected.endsWith("*")) { - String pattern = expected.substring(0, expected.length() - 1); - keys = jsonObject.entrySet().stream() - .map(Map.Entry::getKey) - .filter(key -> key.startsWith(pattern)) - .collect(Collectors.toList()); - } else if (jsonObject.has(expected)) { - keys = Collections.singletonList(expected); - } else { - keys = Collections.emptyList(); - } - - for (String key : keys) { - if (path.isEmpty()) { - jsonObject.remove(key); - } else { - Deque<String> pathCopy = keys.size() > 1 - ? new LinkedList<>(path) - : path; - - delete(jsonObject.get(key), pathCopy); - } - } - } - } diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ConfigParser.java b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ConfigParser.java index dfbf816..675a32e 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ConfigParser.java +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ConfigParser.java @@ -20,19 +20,18 @@ package me.lucko.spark.common.platform.serverconfig; +import com.google.gson.JsonElement; + import java.io.BufferedReader; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Map; public interface ConfigParser { - default Map<String, Object> parse(String file) throws IOException { - return parse(Paths.get(file)); - } + JsonElement load(String file, ExcludedConfigFilter filter) throws IOException; default Map<String, Object> parse(Path file) throws IOException { if (!Files.exists(file)) { diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ExcludedConfigFilter.java b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ExcludedConfigFilter.java new file mode 100644 index 0000000..c11c7f8 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ExcludedConfigFilter.java @@ -0,0 +1,97 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.platform.serverconfig; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Deque; +import java.util.LinkedList; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Filtered excluded paths from {@link JsonElement}s (parsed configuration files). + */ +public class ExcludedConfigFilter { + private final Collection<String> pathsToExclude; + + public ExcludedConfigFilter(Collection<String> pathsToExclude) { + this.pathsToExclude = pathsToExclude; + } + + /** + * Deletes the excluded paths from the json element. + * + * @param json the json element + */ + public JsonElement apply(JsonElement json) { + for (String path : this.pathsToExclude) { + Deque<String> pathDeque = new LinkedList<>(Arrays.asList(path.split("\\."))); + delete(json, pathDeque); + } + return json; + } + + private static void delete(JsonElement json, Deque<String> path) { + if (path.isEmpty()) { + return; + } + if (!json.isJsonObject()) { + return; + } + + JsonObject jsonObject = json.getAsJsonObject(); + String expected = path.removeFirst().replace("<dot>", "."); + + Collection<String> keys; + if (expected.equals("*")) { + keys = jsonObject.entrySet().stream() + .map(Map.Entry::getKey) + .collect(Collectors.toList()); + } else if (expected.endsWith("*")) { + String pattern = expected.substring(0, expected.length() - 1); + keys = jsonObject.entrySet().stream() + .map(Map.Entry::getKey) + .filter(key -> key.startsWith(pattern)) + .collect(Collectors.toList()); + } else if (jsonObject.has(expected)) { + keys = Collections.singletonList(expected); + } else { + keys = Collections.emptyList(); + } + + for (String key : keys) { + if (path.isEmpty()) { + jsonObject.remove(key); + } else { + Deque<String> pathCopy = keys.size() > 1 + ? new LinkedList<>(path) + : path; + + delete(jsonObject.get(key), pathCopy); + } + } + } +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParser.java b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParser.java index 4c7c2c1..344ba1c 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParser.java +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParser.java @@ -20,8 +20,12 @@ package me.lucko.spark.common.platform.serverconfig; +import com.google.gson.Gson; +import com.google.gson.JsonElement; + import java.io.BufferedReader; import java.io.IOException; +import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -32,6 +36,18 @@ import java.util.Properties; public enum PropertiesConfigParser implements ConfigParser { INSTANCE; + private static final Gson GSON = new Gson(); + + @Override + public JsonElement load(String file, ExcludedConfigFilter filter) throws IOException { + Map<String, Object> values = this.parse(Paths.get(file)); + if (values == null) { + return null; + } + + return filter.apply(GSON.toJsonTree(values)); + } + @Override public Map<String, Object> parse(BufferedReader reader) throws IOException { Properties properties = new Properties(); |