forked from j62/ctbrec
1
0
Fork 0

Removed JavaFX code from Config

This commit is contained in:
0xboobface 2018-07-07 14:18:20 +02:00
parent 32172fe203
commit 160aaa51a1
3 changed files with 35 additions and 14 deletions

View File

@ -6,6 +6,7 @@ import static java.nio.file.StandardOpenOption.WRITE;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
@ -16,8 +17,6 @@ import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi; import com.squareup.moshi.Moshi;
import ctbrec.recorder.OS; import ctbrec.recorder.OS;
import ctbrec.ui.AutosizeAlert;
import javafx.scene.control.Alert;
import okio.Buffer; import okio.Buffer;
import okio.BufferedSource; import okio.BufferedSource;
@ -25,11 +24,11 @@ public class Config {
private static final transient Logger LOG = LoggerFactory.getLogger(Config.class); private static final transient Logger LOG = LoggerFactory.getLogger(Config.class);
private static Config instance = new Config(); private static Config instance;
private Settings settings; private Settings settings;
private String filename; private String filename;
private Config() { private Config() throws FileNotFoundException, IOException {
if(System.getProperty("ctbrec.config") != null) { if(System.getProperty("ctbrec.config") != null) {
filename = System.getProperty("ctbrec.config"); filename = System.getProperty("ctbrec.config");
} else { } else {
@ -38,7 +37,7 @@ public class Config {
load(); load();
} }
private void load() { private void load() throws FileNotFoundException, IOException {
Moshi moshi = new Moshi.Builder().build(); Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Settings> adapter = moshi.adapter(Settings.class); JsonAdapter<Settings> adapter = moshi.adapter(Settings.class);
File configDir = OS.getConfigDir(); File configDir = OS.getConfigDir();
@ -48,12 +47,6 @@ public class Config {
try(FileInputStream fin = new FileInputStream(configFile); Buffer buffer = new Buffer()) { try(FileInputStream fin = new FileInputStream(configFile); Buffer buffer = new Buffer()) {
BufferedSource source = buffer.readFrom(fin); BufferedSource source = buffer.readFrom(fin);
settings = adapter.fromJson(source); 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 { } else {
LOG.error("Config file does not exist. Falling back to default values."); 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; return instance;
} }

View File

@ -26,12 +26,22 @@ public class HttpServer {
private Server server = new Server(); private Server server = new Server();
public HttpServer() throws Exception { public HttpServer() throws Exception {
addShutdownHook(); // for graceful termination
if(System.getProperty("ctbrec.config") == null) { if(System.getProperty("ctbrec.config") == null) {
System.setProperty("ctbrec.config", "server.json"); 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(); config = Config.getInstance();
if(config.getSettings().key != null) {
LOG.info("HMAC authentication is enabled");
}
recorder = new LocalRecorder(config); recorder = new LocalRecorder(config);
startHttpServer(); startHttpServer();
} }

View File

@ -35,6 +35,15 @@ public class Launcher extends Application {
@Override @Override
public void start(Stage primaryStage) throws Exception { 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(); hostServices = getHostServices();
Config config = Config.getInstance(); Config config = Config.getInstance();
client = HttpClient.getInstance(); client = HttpClient.getInstance();