diff options
author | Luck <git@lucko.me> | 2021-08-01 15:41:59 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2021-08-01 15:41:59 +0100 |
commit | c8f45bdbf4f045bcad0a23d7741f8064d70c830d (patch) | |
tree | 7cbac3d082f6dd8b03170234d3397206d407c9e1 /spark-common/src/main/java/me/lucko/spark | |
parent | 111e52f3347e7ebec7bcca8a1cd185b2900753de (diff) | |
download | spark-c8f45bdbf4f045bcad0a23d7741f8064d70c830d.tar.gz spark-c8f45bdbf4f045bcad0a23d7741f8064d70c830d.tar.bz2 spark-c8f45bdbf4f045bcad0a23d7741f8064d70c830d.zip |
Some basic config options
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark')
4 files changed, 103 insertions, 14 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java index 7cb5d29..38fc715 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java +++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java @@ -45,6 +45,7 @@ import me.lucko.spark.common.tick.TickHook; import me.lucko.spark.common.tick.TickReporter; import me.lucko.spark.common.util.BytebinClient; import me.lucko.spark.common.util.ClassSourceLookup; +import me.lucko.spark.common.util.Configuration; import net.kyori.adventure.text.event.ClickEvent; @@ -79,16 +80,14 @@ import static net.kyori.adventure.text.format.TextDecoration.UNDERLINED; */ public class SparkPlatform { - /** The URL of the viewer frontend */ - public static final String VIEWER_URL = "https://spark.lucko.me/"; - /** The shared okhttp client */ - private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient(); - /** The bytebin instance used by the platform */ - public static final BytebinClient BYTEBIN_CLIENT = new BytebinClient(OK_HTTP_CLIENT, "https://bytebin.lucko.me/", "spark-plugin"); /** The date time formatter instance used by the platform */ private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss"); private final SparkPlugin plugin; + private final Configuration configuration; + private final String viewerUrl; + private final OkHttpClient httpClient; + private final BytebinClient bytebinClient; private final List<CommandModule> commandModules; private final List<Command> commands; private final ReentrantLock commandExecuteLock = new ReentrantLock(true); @@ -104,6 +103,14 @@ public class SparkPlatform { public SparkPlatform(SparkPlugin plugin) { this.plugin = plugin; + this.configuration = new Configuration(this.plugin.getPluginDirectory().resolve("config.json")); + + this.viewerUrl = this.configuration.getString("viewerUrl", "https://spark.lucko.me/"); + String bytebinUrl = this.configuration.getString("bytebinUrl", "https://bytebin.lucko.me/"); + + this.httpClient = new OkHttpClient(); + this.bytebinClient = new BytebinClient(this.httpClient, bytebinUrl, "spark-plugin"); + this.commandModules = ImmutableList.of( new SamplerModule(), new HealthModule(), @@ -170,14 +177,26 @@ public class SparkPlatform { // shutdown okhttp // see: https://github.com/square/okhttp/issues/4029 - OK_HTTP_CLIENT.dispatcher().executorService().shutdown(); - OK_HTTP_CLIENT.connectionPool().evictAll(); + this.httpClient.dispatcher().executorService().shutdown(); + this.httpClient.connectionPool().evictAll(); } public SparkPlugin getPlugin() { return this.plugin; } + public Configuration getConfiguration() { + return this.configuration; + } + + public String getViewerUrl() { + return this.viewerUrl; + } + + public BytebinClient getBytebinClient() { + return this.bytebinClient; + } + public ActivityLog getActivityLog() { return this.activityLog; } diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/HeapAnalysisModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/HeapAnalysisModule.java index 8a7d781..70f6c3c 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/HeapAnalysisModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/HeapAnalysisModule.java @@ -105,8 +105,8 @@ public class HeapAnalysisModule implements CommandModule { saveToFile = true; } else { try { - String key = SparkPlatform.BYTEBIN_CLIENT.postContent(output, SPARK_HEAP_MEDIA_TYPE).key(); - String url = SparkPlatform.VIEWER_URL + key; + String key = platform.getBytebinClient().postContent(output, SPARK_HEAP_MEDIA_TYPE).key(); + String url = platform.getViewerUrl() + key; resp.broadcastPrefixed(text("Heap dump summmary output:", GOLD)); resp.broadcast(text() @@ -135,7 +135,7 @@ public class HeapAnalysisModule implements CommandModule { .append(text(file.toString(), GRAY)) .build() ); - resp.broadcastPrefixed(text("You can read the heap dump summary file using the viewer web-app - " + SparkPlatform.VIEWER_URL, GRAY)); + resp.broadcastPrefixed(text("You can read the heap dump summary file using the viewer web-app - " + platform.getViewerUrl(), GRAY)); platform.getActivityLog().addToLog(Activity.fileActivity(sender, System.currentTimeMillis(), "Heap dump summary", file.toString())); } catch (IOException e) { diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java index b20d22f..c0a295a 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java @@ -312,8 +312,8 @@ public class SamplerModule implements CommandModule { saveToFile = true; } else { try { - String key = SparkPlatform.BYTEBIN_CLIENT.postContent(output, SPARK_SAMPLER_MEDIA_TYPE).key(); - String url = SparkPlatform.VIEWER_URL + key; + String key = platform.getBytebinClient().postContent(output, SPARK_SAMPLER_MEDIA_TYPE).key(); + String url = platform.getViewerUrl() + key; resp.broadcastPrefixed(text("Profiler results:", GOLD)); resp.broadcast(text() @@ -342,7 +342,7 @@ public class SamplerModule implements CommandModule { .append(text(file.toString(), GRAY)) .build() ); - resp.broadcastPrefixed(text("You can read the profile file using the viewer web-app - " + SparkPlatform.VIEWER_URL, GRAY)); + resp.broadcastPrefixed(text("You can read the profile file using the viewer web-app - " + platform.getViewerUrl(), GRAY)); platform.getActivityLog().addToLog(Activity.fileActivity(resp.sender(), System.currentTimeMillis(), "Profiler", file.toString())); } catch (IOException e) { 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 new file mode 100644 index 0000000..7588645 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/Configuration.java @@ -0,0 +1,70 @@ +/* + * 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.util; + +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.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 final JsonObject root; + + public Configuration(Path file) { + JsonObject root = null; + if (Files.exists(file)) { + try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) { + root = PARSER.parse(reader).getAsJsonObject(); + } catch (IOException e) { + e.printStackTrace(); + } + } + this.root = root != null ? root : new JsonObject(); + } + + public String getString(String path, String def) { + JsonElement el = this.root.get(path); + if (el == null || !el.isJsonPrimitive()) { + return def; + } + + return el.getAsJsonPrimitive().getAsString(); + } + + public boolean getBoolean(String path, boolean def) { + JsonElement el = this.root.get(path); + if (el == null || !el.isJsonPrimitive()) { + return def; + } + + JsonPrimitive val = el.getAsJsonPrimitive(); + return val.isBoolean() ? val.getAsBoolean() : def; + } + +} |