From 160aaa51a1aae6ca0d29502417a208cd7efd736a Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Sat, 7 Jul 2018 14:18:20 +0200 Subject: [PATCH] Removed JavaFX code from Config --- src/main/java/ctbrec/Config.java | 26 ++++++++++--------- .../ctbrec/recorder/server/HttpServer.java | 14 ++++++++-- src/main/java/ctbrec/ui/Launcher.java | 9 +++++++ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/main/java/ctbrec/Config.java b/src/main/java/ctbrec/Config.java index a539ac0d..dbfcf1e6 100644 --- a/src/main/java/ctbrec/Config.java +++ b/src/main/java/ctbrec/Config.java @@ -6,6 +6,7 @@ import static java.nio.file.StandardOpenOption.WRITE; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Files; @@ -16,8 +17,6 @@ import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.Moshi; import ctbrec.recorder.OS; -import ctbrec.ui.AutosizeAlert; -import javafx.scene.control.Alert; import okio.Buffer; import okio.BufferedSource; @@ -25,11 +24,11 @@ public class Config { private static final transient Logger LOG = LoggerFactory.getLogger(Config.class); - private static Config instance = new Config(); + private static Config instance; private Settings settings; private String filename; - private Config() { + private Config() throws FileNotFoundException, IOException { if(System.getProperty("ctbrec.config") != null) { filename = System.getProperty("ctbrec.config"); } else { @@ -38,7 +37,7 @@ public class Config { load(); } - private void load() { + private void load() throws FileNotFoundException, IOException { Moshi moshi = new Moshi.Builder().build(); JsonAdapter adapter = moshi.adapter(Settings.class); File configDir = OS.getConfigDir(); @@ -48,12 +47,6 @@ public class Config { try(FileInputStream fin = new FileInputStream(configFile); Buffer buffer = new Buffer()) { BufferedSource source = buffer.readFrom(fin); settings = adapter.fromJson(source); - } catch(Exception e) { - Alert alert = new AutosizeAlert(Alert.AlertType.ERROR); - alert.setTitle("Whoopsie"); - alert.setContentText("Couldn't load settings."); - alert.showAndWait(); - System.exit(1); } } else { LOG.error("Config file does not exist. Falling back to default values."); @@ -61,7 +54,16 @@ public class Config { } } - public static Config getInstance() { + public static synchronized void init() throws FileNotFoundException, IOException { + if(instance == null) { + instance = new Config(); + } + } + + public static synchronized Config getInstance() { + if(instance == null) { + throw new IllegalStateException("Config not initialized, call init() first"); + } return instance; } diff --git a/src/main/java/ctbrec/recorder/server/HttpServer.java b/src/main/java/ctbrec/recorder/server/HttpServer.java index 8632e865..c6492001 100644 --- a/src/main/java/ctbrec/recorder/server/HttpServer.java +++ b/src/main/java/ctbrec/recorder/server/HttpServer.java @@ -26,12 +26,22 @@ public class HttpServer { private Server server = new Server(); public HttpServer() throws Exception { - addShutdownHook(); // for graceful termination - if(System.getProperty("ctbrec.config") == null) { System.setProperty("ctbrec.config", "server.json"); } + try { + Config.init(); + } catch (Exception e) { + LOG.error("Couldn't load config", e); + System.exit(1); + } + + addShutdownHook(); // for graceful termination + config = Config.getInstance(); + if(config.getSettings().key != null) { + LOG.info("HMAC authentication is enabled"); + } recorder = new LocalRecorder(config); startHttpServer(); } diff --git a/src/main/java/ctbrec/ui/Launcher.java b/src/main/java/ctbrec/ui/Launcher.java index 2de240a1..7b3e6069 100644 --- a/src/main/java/ctbrec/ui/Launcher.java +++ b/src/main/java/ctbrec/ui/Launcher.java @@ -35,6 +35,15 @@ public class Launcher extends Application { @Override public void start(Stage primaryStage) throws Exception { + try { + Config.init(); + } catch (Exception e) { + Alert alert = new AutosizeAlert(Alert.AlertType.ERROR); + alert.setTitle("Whoopsie"); + alert.setContentText("Couldn't load settings."); + alert.showAndWait(); + System.exit(1); + } hostServices = getHostServices(); Config config = Config.getInstance(); client = HttpClient.getInstance();