From b10958e790a45579ee377dd2ea005c2c784d7a31 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Tue, 11 Nov 2014 11:34:12 +0100 Subject: [PATCH] Introduce explicit dir and prefix params in Persistence Like the previous commit, this change eliminates the reliance on "appName" within the Persistence class in favor of explicit and independently configurable parameters. Additionally, the FileUtil class has been removed as it is no longer necessary. --- .../bitsquare/app/BitsquareEnvironment.java | 4 ++ .../bitsquare/app/gui/BitsquareAppModule.java | 13 ++++- .../io/bitsquare/persistence/Persistence.java | 38 ++++++++++--- src/main/java/io/bitsquare/util/FileUtil.java | 57 ------------------- 4 files changed, 43 insertions(+), 69 deletions(-) delete mode 100644 src/main/java/io/bitsquare/util/FileUtil.java diff --git a/src/main/java/io/bitsquare/app/BitsquareEnvironment.java b/src/main/java/io/bitsquare/app/BitsquareEnvironment.java index aef285ecb5..b3dbfecac9 100644 --- a/src/main/java/io/bitsquare/app/BitsquareEnvironment.java +++ b/src/main/java/io/bitsquare/app/BitsquareEnvironment.java @@ -19,6 +19,7 @@ package io.bitsquare.app; import io.bitsquare.BitsquareException; import io.bitsquare.btc.WalletFacade; +import io.bitsquare.persistence.Persistence; import com.google.common.base.Preconditions; @@ -74,6 +75,9 @@ public class BitsquareEnvironment extends StandardEnvironment { setProperty(WalletFacade.PREFIX_KEY, appName); setProperty(WalletFacade.USERAGENT_NAME_KEY, appName); setProperty(WalletFacade.USERAGENT_VERSION_KEY, "0.1"); + + setProperty(Persistence.DIR_KEY, AppDirectory.dir(appName).toString()); + setProperty(Persistence.PREFIX_KEY, appName + "_pref"); }}); } diff --git a/src/main/java/io/bitsquare/app/gui/BitsquareAppModule.java b/src/main/java/io/bitsquare/app/gui/BitsquareAppModule.java index 8e2343f059..bbd890758c 100644 --- a/src/main/java/io/bitsquare/app/gui/BitsquareAppModule.java +++ b/src/main/java/io/bitsquare/app/gui/BitsquareAppModule.java @@ -32,12 +32,15 @@ import io.bitsquare.trade.TradeModule; import io.bitsquare.user.User; import com.google.inject.Injector; -import com.google.inject.name.Names; + +import java.io.File; import javafx.stage.Stage; import org.springframework.core.env.Environment; +import static com.google.inject.name.Names.named; + class BitsquareAppModule extends BitsquareModule { private final Stage primaryStage; @@ -50,9 +53,13 @@ class BitsquareAppModule extends BitsquareModule { @Override protected void configure() { bind(User.class).asEagerSingleton(); - bind(Persistence.class).asEagerSingleton(); bind(Settings.class).asEagerSingleton(); + File persistenceDir = new File(env.getRequiredProperty(Persistence.DIR_KEY)); + bind(File.class).annotatedWith(named(Persistence.DIR_KEY)).toInstance(persistenceDir); + bindConstant().annotatedWith(named(Persistence.PREFIX_KEY)).to(env.getRequiredProperty(Persistence.PREFIX_KEY)); + bind(Persistence.class).asEagerSingleton(); + install(messageModule()); install(bitcoinModule()); install(cryptoModule()); @@ -62,7 +69,7 @@ class BitsquareAppModule extends BitsquareModule { String appName = env.getRequiredProperty(BitsquareEnvironment.APP_NAME_KEY); - bindConstant().annotatedWith(Names.named("appName")).to(appName); + bindConstant().annotatedWith(named("appName")).to(appName); } protected MessageModule messageModule() { diff --git a/src/main/java/io/bitsquare/persistence/Persistence.java b/src/main/java/io/bitsquare/persistence/Persistence.java index 126b6e747f..2394b4716d 100644 --- a/src/main/java/io/bitsquare/persistence/Persistence.java +++ b/src/main/java/io/bitsquare/persistence/Persistence.java @@ -17,8 +17,7 @@ package io.bitsquare.persistence; -import io.bitsquare.util.FileUtil; - +import org.bitcoinj.core.Utils; import org.bitcoinj.utils.Threading; import java.io.File; @@ -52,22 +51,27 @@ public class Persistence { private static final Logger log = LoggerFactory.getLogger(Persistence.class); private static final ReentrantLock lock = Threading.lock("Storage"); + public static final String DIR_KEY = "persistence.dir"; + public static final String PREFIX_KEY = "persistence.prefix"; + @GuardedBy("lock") private Map rootMap = new HashMap<>(); + private final File dir; private final String prefix; private final File storageFile; - private String appName; /////////////////////////////////////////////////////////////////////////////////////////// // Constructor /////////////////////////////////////////////////////////////////////////////////////////// @Inject - public Persistence(@Named("appName") String appName) { - this.appName = appName; - this.prefix = appName + "_pref"; - this.storageFile = FileUtil.getFile(appName, prefix, "ser"); + public Persistence( + @Named(DIR_KEY) File dir, + @Named(PREFIX_KEY) String prefix) { + this.dir = dir; + this.prefix = prefix; + this.storageFile = new File(dir, prefix + ".ser"); } /////////////////////////////////////////////////////////////////////////////////////////// @@ -220,7 +224,7 @@ public class Persistence { FileOutputStream fileOutputStream = null; ObjectOutputStream objectOutputStream = null; try { - tempFile = FileUtil.getTempFile(appName, prefix); + tempFile = File.createTempFile("temp_" + prefix, null, dir); // Don't use auto closeable resources in try() as we would need too many try/catch clauses (for tempFile) // and we need to close it @@ -240,7 +244,7 @@ public class Persistence { fileOutputStream.close(); objectOutputStream.close(); - FileUtil.writeTempFileToFile(tempFile, storageFile); + writeTempFileToFile(tempFile, storageFile); } catch (IOException e) { e.printStackTrace(); log.error("save object to file failed." + e); @@ -269,4 +273,20 @@ public class Persistence { lock.unlock(); } } + + public void writeTempFileToFile(File tempFile, File file) throws IOException { + if (Utils.isWindows()) { + // Work around an issue on Windows whereby you can't rename over existing files. + final File canonical = file.getCanonicalFile(); + if (canonical.exists() && !canonical.delete()) { + throw new IOException("Failed to delete canonical file for replacement with save"); + } + if (!tempFile.renameTo(canonical)) { + throw new IOException("Failed to rename " + tempFile + " to " + canonical); + } + } + else if (!tempFile.renameTo(file)) { + throw new IOException("Failed to rename " + tempFile + " to " + file); + } + } } diff --git a/src/main/java/io/bitsquare/util/FileUtil.java b/src/main/java/io/bitsquare/util/FileUtil.java deleted file mode 100644 index 89580f33ae..0000000000 --- a/src/main/java/io/bitsquare/util/FileUtil.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * This file is part of Bitsquare. - * - * Bitsquare is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bitsquare is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bitsquare. If not, see . - */ - -package io.bitsquare.util; - -import org.bitcoinj.core.Utils; - -import java.io.File; -import java.io.IOException; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import lighthouse.files.AppDirectory; - -public class FileUtil { - private static final Logger log = LoggerFactory.getLogger(FileUtil.class); - - public static File getFile(String appName, String name, String suffix) { - return new File(AppDirectory.dir(appName).toFile(), name + "." + suffix); - } - - public static File getTempFile(String appName, String prefix) throws IOException { - return File.createTempFile("temp_" + prefix, null, AppDirectory.dir(appName).toFile()); - } - - public static void writeTempFileToFile(File tempFile, File file) throws IOException { - if (Utils.isWindows()) { - // Work around an issue on Windows whereby you can't rename over existing files. - final File canonical = file.getCanonicalFile(); - if (canonical.exists() && !canonical.delete()) { - throw new IOException("Failed to delete canonical file for replacement with save"); - } - if (!tempFile.renameTo(canonical)) { - throw new IOException("Failed to rename " + tempFile + " to " + canonical); - } - } - else if (!tempFile.renameTo(file)) { - throw new IOException("Failed to rename " + tempFile + " to " + file); - } - - } -}