diff options
author | Luck <git@lucko.me> | 2022-11-27 23:38:21 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2022-11-27 23:38:21 +0000 |
commit | fc1e371d67551e9548491e9bf50534d91ce5d170 (patch) | |
tree | fc7580c99d918e4f69f990d7b5038a0c4ab44f22 /spark-common/src/main/java/me/lucko/spark/common/util | |
parent | 115ff5d8d58f6793fd8ea980a95718e7ffca1454 (diff) | |
download | spark-fc1e371d67551e9548491e9bf50534d91ce5d170.tar.gz spark-fc1e371d67551e9548491e9bf50534d91ce5d170.tar.bz2 spark-fc1e371d67551e9548491e9bf50534d91ce5d170.zip |
Temporary solution to async-profiler JVM crashing issues (#271, #273, #274)
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common/util')
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java | 60 |
1 files changed, 53 insertions, 7 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java b/spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java index ce63878..32f3bc6 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java @@ -20,32 +20,58 @@ package me.lucko.spark.common.util; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; public final class Configuration { - private static final JsonParser PARSER = new JsonParser(); + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); - private final JsonObject root; + private final Path file; + private JsonObject root; public Configuration(Path file) { + this.file = file; + load(); + } + + public void load() { JsonObject root = null; - if (Files.exists(file)) { - try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) { - root = PARSER.parse(reader).getAsJsonObject(); + if (Files.exists(this.file)) { + try (BufferedReader reader = Files.newBufferedReader(this.file, StandardCharsets.UTF_8)) { + root = GSON.fromJson(reader, JsonObject.class); } catch (IOException e) { e.printStackTrace(); } } - this.root = root != null ? root : new JsonObject(); + if (root == null) { + root = new JsonObject(); + root.addProperty("_header", "spark configuration file - https://spark.lucko.me/docs/Configuration"); + } + this.root = root; + } + + public void save() { + try { + Files.createDirectories(this.file.getParent()); + } catch (IOException e) { + // ignore + } + + try (BufferedWriter writer = Files.newBufferedWriter(this.file, StandardCharsets.UTF_8)) { + GSON.toJson(this.root, writer); + } catch (IOException e) { + e.printStackTrace(); + } } public String getString(String path, String def) { @@ -77,4 +103,24 @@ public final class Configuration { return val.isBoolean() ? val.getAsInt() : def; } + public void setString(String path, String value) { + this.root.add(path, new JsonPrimitive(value)); + } + + public void setBoolean(String path, boolean value) { + this.root.add(path, new JsonPrimitive(value)); + } + + public void setInteger(String path, int value) { + this.root.add(path, new JsonPrimitive(value)); + } + + public boolean contains(String path) { + return this.root.has(path); + } + + public void remove(String path) { + this.root.remove(path); + } + } |