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.
This commit is contained in:
Chris Beams 2014-11-11 11:34:12 +01:00
parent 8f76a1d37b
commit b10958e790
No known key found for this signature in database
GPG key ID: 3D214F8F5BC5ED73
4 changed files with 43 additions and 69 deletions

View file

@ -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");
}});
}

View file

@ -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() {

View file

@ -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<String, Serializable> 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);
}
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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);
}
}
}