From 918f63b1f5db597667f594828a5f423a8f17c30f Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Fri, 30 Nov 2018 13:42:51 +0100 Subject: [PATCH] Use defaults, if settings cannot be loaded If the settings cannot be loaded, make a backup of the settings file and use the defaults, so that the application at least starts. --- .../java/ctbrec/ui/CamrecApplication.java | 3 +-- common/src/main/java/ctbrec/Config.java | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/client/src/main/java/ctbrec/ui/CamrecApplication.java b/client/src/main/java/ctbrec/ui/CamrecApplication.java index 72c0259d..b55622ff 100644 --- a/client/src/main/java/ctbrec/ui/CamrecApplication.java +++ b/client/src/main/java/ctbrec/ui/CamrecApplication.java @@ -250,9 +250,8 @@ public class CamrecApplication extends Application { LOG.error("Couldn't load settings", e); Alert alert = new AutosizeAlert(Alert.AlertType.ERROR); alert.setTitle("Whoopsie"); - alert.setContentText("Couldn't load settings."); + alert.setContentText("Couldn't load settings. Falling back to defaults. A backup of your settings has been created."); alert.showAndWait(); - System.exit(1); } config = Config.getInstance(); } diff --git a/common/src/main/java/ctbrec/Config.java b/common/src/main/java/ctbrec/Config.java index 871c36ff..9170a5f1 100644 --- a/common/src/main/java/ctbrec/Config.java +++ b/common/src/main/java/ctbrec/Config.java @@ -7,6 +7,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @@ -46,7 +47,6 @@ public class Config { } else { filename = "settings.json"; } - load(); } private void load() throws FileNotFoundException, IOException { @@ -61,6 +61,13 @@ public class Config { BufferedSource source = buffer.readFrom(fin); settings = adapter.fromJson(source); settings.httpTimeout = Math.max(settings.httpTimeout, 10_000); + } catch(Throwable e) { + settings = OS.getDefaultSettings(); + for (Site site : sites) { + site.setEnabled(!settings.disabledSites.contains(site.getName())); + } + makeBackup(configFile); + throw e; } } else { LOG.error("Config file does not exist. Falling back to default values."); @@ -71,9 +78,22 @@ public class Config { } } + private void makeBackup(File source) { + try { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); + String timestamp = sdf.format(new Date()); + String backup = source.getName() + '.' + timestamp; + File target = new File(source.getParentFile(), backup); + Files.copy(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); + } catch(Throwable e) { + LOG.error("Couldn't create backup of settings file", e); + } + } + public static synchronized void init(List sites) throws FileNotFoundException, IOException { if(instance == null) { instance = new Config(sites); + instance.load(); } }