From 4ea329060840c0c51833042af32232fdb23703b3 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Wed, 8 Jan 2020 15:20:33 +0100 Subject: [PATCH] Introduce and document static Config.appDataDir() See Javadoc added in this change and the previous commit message for further detail and context. --- .../main/java/bisq/common/config/Config.java | 28 +++++++++++++++---- .../bisq/desktop/main/overlays/Overlay.java | 2 +- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/bisq/common/config/Config.java b/common/src/main/java/bisq/common/config/Config.java index 4e26b39b17..c9897d6b5c 100644 --- a/common/src/main/java/bisq/common/config/Config.java +++ b/common/src/main/java/bisq/common/config/Config.java @@ -30,6 +30,7 @@ import java.util.Optional; import ch.qos.logback.classic.Level; +import static com.google.common.base.Preconditions.checkNotNull; import static java.lang.String.format; import static java.util.stream.Collectors.toList; @@ -90,10 +91,7 @@ public class Config { public static final boolean DEFAULT_FULL_DAO_NODE = false; static final String DEFAULT_CONFIG_FILE_NAME = "bisq.properties"; - // legacy mutable static field used to support opening bisq.log from - // Overlay.addReportErrorButtons. avoids the need to inject config into all Overlay - // subclasses. TODO: do the injection anyway - public static File CURRENT_APP_DATA_DIR; + private static File APP_DATA_DIR_VALUE; private static BaseCurrencyNetwork BASE_CURRENCY_NETWORK_VALUE = BaseCurrencyNetwork.BTC_MAINNET; // default data dir properties @@ -239,6 +237,7 @@ public class Config { .withValuesSeparatedBy(',') .describedAs("host:port[,...]"); + //noinspection rawtypes ArgumentAcceptingOptionSpec baseCurrencyNetworkOpt = parser.accepts(BASE_CURRENCY_NETWORK, "Base currency network") .withRequiredArg() @@ -638,8 +637,7 @@ public class Config { this.torDir = mkdir(btcNetworkDir, "tor"); this.walletDir = mkdir(btcNetworkDir, "wallet"); - // assign values to legacy mutable static fields - CURRENT_APP_DATA_DIR = appDataDir; + APP_DATA_DIR_VALUE = appDataDir; BASE_CURRENCY_NETWORK_VALUE = baseCurrencyNetwork; } @@ -739,6 +737,23 @@ public class Config { // == STATIC ACCESSORS ====================================================================== + /** + * Static accessor that returns the same value as the non-static + * {@link #getAppDataDir()} method. For use only in the {@code Overlay} class, where + * because of its large number of subclasses, injecting the Guice-managed + * {@link Config} class is not worth the effort. {@link #getAppDataDir()} should be + * favored in all other cases. + * @throws NullPointerException if the static value has not yet been assigned, i.e. if + * the Guice-managed {@link Config} class has not yet been instantiated elsewhere. + * This should never be the case, as Guice wiring always happens before any + * {@code Overlay} class is instantiated. + */ + public static File appDataDir() { + return checkNotNull(APP_DATA_DIR_VALUE, "The static appDataDir has not yet " + + "been assigned. A Config instance must be instantiated (usually by " + + "Guice) before calling this method."); + } + /** * Static accessor that returns either the default base currency network value of * {@link BaseCurrencyNetwork#BTC_MAINNET} or the value assigned via the @@ -764,6 +779,7 @@ public class Config { return BASE_CURRENCY_NETWORK_VALUE.getParameters(); } + // == ACCESSORS ====================================================================== public String getDefaultAppName() { diff --git a/desktop/src/main/java/bisq/desktop/main/overlays/Overlay.java b/desktop/src/main/java/bisq/desktop/main/overlays/Overlay.java index ffe711922a..2efa76baa4 100644 --- a/desktop/src/main/java/bisq/desktop/main/overlays/Overlay.java +++ b/desktop/src/main/java/bisq/desktop/main/overlays/Overlay.java @@ -843,7 +843,7 @@ public abstract class Overlay> { gridPane.getChildren().add(logButton); logButton.setOnAction(event -> { try { - File dataDir = Config.CURRENT_APP_DATA_DIR; + File dataDir = Config.appDataDir(); File logFile = new File(dataDir, "bisq.log"); Utilities.openFile(logFile); } catch (IOException e) {