Skip to content

Commit

Permalink
Allow central storage of config file (issue #83)
Browse files Browse the repository at this point in the history
Config file (config.json) is searched in the following locations in the
following order:
1. If an environment variable with the name "KSE_CONFIG_DIR" is set,
then inside this directory
2. In the KSE base directory (where kse.jar is located)
3. In the HOME directory of the user (following OS-specific rules)
  • Loading branch information
kaikramer committed Jan 3, 2024
1 parent bebc9ad commit 22cbe75
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion kse/src/main/java/org/kse/KSE.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.kse.crypto.x509.KseX500NameStyle;
import org.kse.gui.CreateApplicationGui;
import org.kse.gui.CurrentDirectory;
import org.kse.gui.KseRestart;
import org.kse.gui.error.DError;
import org.kse.gui.preferences.PreferencesManager;
import org.kse.gui.preferences.data.KsePreferences;
Expand Down Expand Up @@ -158,7 +159,7 @@ private static void setAppleSystemProperties() {

private static void setInstallDirProperty() {
// Use this for restarts; install directory is always user.dir, but we change user.dir in CurrentDirectory class
System.setProperty("kse.install.dir", System.getProperty("user.dir"));
System.setProperty(KseRestart.KSE_INSTALL_DIR, System.getProperty("user.dir"));
}

private static void setCurrentDirectory(String currentDir) {
Expand Down
2 changes: 1 addition & 1 deletion kse/src/main/java/org/kse/gui/KseRestart.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class KseRestart {
private static final String JAVA_HOME = "java.home";
private static final String JAVA_CLASS_PATH = "java.class.path";

private static final String KSE_INSTALL_DIR = "kse.install.dir";
public static final String KSE_INSTALL_DIR = "kse.install.dir";

// macOS: location of native app stub (passed from appbundler)
private static final String KSE_APP_STUB = "kse.app.stub";
Expand Down
30 changes: 24 additions & 6 deletions kse/src/main/java/org/kse/gui/preferences/PreferencesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;

import org.kse.gui.JEscFrame;
import org.kse.gui.KseRestart;
import org.kse.gui.error.DError;
import org.kse.gui.preferences.data.KsePreferences;
import org.kse.gui.preferences.json.KseJacksonJrExtension;
Expand All @@ -51,6 +52,7 @@ public class PreferencesManager {
private static final String CONFIG_BASE_DIR = "kse";
private static final String CONFIG_DOTTED_BASE_DIR = ".kse";
private static final String CONFIG_FILE_NAME = "config.json";
private static final String ENV_VAR_CONFIG_DIR = "KSE_CONFIG_DIR";

private static KsePreferences ksePreferences;

Expand All @@ -77,22 +79,38 @@ private static KsePreferences load() {
try {
return json.beanFrom(KsePreferences.class, determineConfigFilePath());
} catch (FileNotFoundException e) {
// ignore
// ignore, happens always on first run
return new KsePreferences();
} catch (Exception e) {
DError.displayError(new JEscFrame(), e);
return new KsePreferences();
}
}

private static File determineConfigFilePath() {
private static File determineConfigFilePath() throws IOException {

// 1. Location with highest priority: Config dir set from outside via env var
File envDirConfigFile = new File(System.getenv(ENV_VAR_CONFIG_DIR), CONFIG_FILE_NAME);
if (envDirConfigFile.exists()) {
return envDirConfigFile.getCanonicalFile();
}

// 2. Config found in KSE base directory (where kse.jar is located)
File kseInstallDirConfigFile = new File(System.getProperty(KseRestart.KSE_INSTALL_DIR), CONFIG_FILE_NAME);
if (kseInstallDirConfigFile.exists()) {
return kseInstallDirConfigFile.getCanonicalFile();
}

// 3. OS specific local/user config
if (OperatingSystem.isWindows()) {
return new File(getAppDataConfigDir(), CONFIG_FILE_NAME);
return new File(getAppDataConfigDir(), CONFIG_FILE_NAME).getCanonicalFile();
} else if (OperatingSystem.isLinux()) {
return new File(getXdgConfigDir(), CONFIG_FILE_NAME);
return new File(getXdgConfigDir(), CONFIG_FILE_NAME).getCanonicalFile();
} else if (OperatingSystem.isMacOs()){
return new File(getXdgConfigDir(), CONFIG_FILE_NAME);
return new File(getXdgConfigDir(), CONFIG_FILE_NAME).getCanonicalFile();
}

// default to HOME dir
return new File(System.getProperty("user.home"), ".kse" + File.separator + CONFIG_FILE_NAME);
}

Expand Down Expand Up @@ -122,7 +140,7 @@ public static void persist() {
File configFile = determineConfigFilePath();
configFile.getParentFile().mkdirs();
json.write(ksePreferences, configFile);
} catch (IOException e) {
} catch (Exception e) {
DError.displayError(new JEscFrame(), e);
}
}
Expand Down

0 comments on commit 22cbe75

Please sign in to comment.