Fix handling of trade lists

This commit is contained in:
Manfred Karrer 2015-03-24 22:59:31 +01:00
parent d9047ec1b2
commit 9cb029c21b
7 changed files with 49 additions and 34 deletions

View File

@ -297,6 +297,7 @@ public class PendingTradesView extends ActivatableViewAndModel<AnchorPane, Pendi
case OFFERER_BUYER_START_PAYMENT:
processBar.setSelectedIndex(1);
paymentStartedButton.setDisable(false);
setPaymentsControlsVisible(true);
statusTextField.setText("Deposit transaction has at least one block chain confirmation. " +

View File

@ -174,14 +174,20 @@ public class FileManager<T> {
}
}
public void removeFile(T serializable) {
storageFile.delete();
public void removeFile(String fileName) {
File file = new File(dir, fileName);
boolean result = file.delete();
if (!result)
log.warn("Could not delete file: " + file.toString());
File backupDir = new File(Paths.get(dir.getAbsolutePath(), "backup").toString());
if (backupDir.exists()) {
File backupFile = new File(backupDir, serializable.getClass().getSimpleName());
if (backupFile.exists())
backupFile.delete();
File backupFile = new File(Paths.get(dir.getAbsolutePath(), "backup", fileName).toString());
if (backupFile.exists()) {
result = backupFile.delete();
if (!result)
log.warn("Could not delete backupFile: " + file.toString());
}
}
}
@ -203,20 +209,22 @@ public class FileManager<T> {
}
}
public void removeAndBackupFile(File storageFile, File dir, String name) throws IOException {
public void removeAndBackupFile(String fileName) throws IOException {
File corruptedBackupDir = new File(Paths.get(dir.getAbsolutePath(), "corrupted").toString());
if (!corruptedBackupDir.exists())
corruptedBackupDir.mkdir();
renameTempFileToFile(storageFile, new File(corruptedBackupDir, serializable.getClass().getSimpleName()));
File corruptedFile = new File(Paths.get(dir.getAbsolutePath(), "corrupted", fileName).toString());
renameTempFileToFile(storageFile, corruptedFile);
}
public void backupFile(T serializable) throws IOException {
public void backupFile(String fileName) throws IOException {
File backupDir = new File(Paths.get(dir.getAbsolutePath(), "backup").toString());
if (!backupDir.exists())
backupDir.mkdir();
Files.copy(storageFile, new File(backupDir, serializable.getClass().getSimpleName()));
File backupFile = new File(Paths.get(dir.getAbsolutePath(), "backup", fileName).toString());
Files.copy(storageFile, backupFile);
}
///////////////////////////////////////////////////////////////////////////////////////////

View File

@ -25,8 +25,6 @@ import java.io.IOException;
import java.io.InvalidClassException;
import java.io.Serializable;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
@ -60,6 +58,7 @@ public class Storage<T extends Serializable> {
private FileManager<T> fileManager;
private File storageFile;
private T serializable;
private String fileName;
///////////////////////////////////////////////////////////////////////////////////////////
@ -77,6 +76,7 @@ public class Storage<T extends Serializable> {
public T initAndGetPersisted(T serializable, String fileName) {
this.serializable = serializable;
this.fileName = fileName;
storageFile = new File(dir, fileName);
fileManager = new FileManager<>(dir, storageFile, 500, TimeUnit.MILLISECONDS);
@ -91,8 +91,8 @@ public class Storage<T extends Serializable> {
fileManager.saveLater(serializable);
}
public void remove() {
fileManager.removeFile(serializable);
public void remove(String fileName) {
fileManager.removeFile(fileName);
}
@ -113,7 +113,7 @@ public class Storage<T extends Serializable> {
// If we did not get any exception we can be sure the data are consistent so we make a backup
now = System.currentTimeMillis();
fileManager.backupFile(serializable);
fileManager.backupFile(fileName);
log.info("Backup {} completed in {}msec", serializable.getClass().getSimpleName(), System.currentTimeMillis() - now);
return persistedObject;
@ -121,8 +121,7 @@ public class Storage<T extends Serializable> {
log.error("Version of persisted class has changed. We cannot read the persisted data anymore. We make a backup and remove the inconsistent file.");
try {
// In case the persisted data have been critical (keys) we keep a backup which might be used for recovery
fileManager.removeAndBackupFile(storageFile, new File(Paths.get(dir.getAbsolutePath(), "inconsistent").toString()),
serializable.getClass().getSimpleName());
fileManager.removeAndBackupFile(fileName);
} catch (IOException e1) {
e1.printStackTrace();
log.error(e1.getMessage());

View File

@ -19,8 +19,7 @@ package io.bitsquare.trade;
import io.bitsquare.storage.Storage;
import com.google.inject.Inject;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
@ -38,11 +37,10 @@ public class TradeList extends ArrayList<Trade> implements Serializable {
transient final private Storage<TradeList> storage;
transient private ObservableList<Trade> observableList;
@Inject
public TradeList(Storage<TradeList> storage) {
this.storage = storage;
public TradeList(File storageDir, String fileName) {
this.storage = new Storage<>(storageDir);
TradeList persisted = storage.initAndGetPersisted(this);
TradeList persisted = storage.initAndGetPersisted(this, fileName);
if (persisted != null) {
this.addAll(persisted);
observableList = FXCollections.observableArrayList(this);

View File

@ -95,15 +95,12 @@ public class TradeManager {
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
public TradeManager(User user, AccountSettings accountSettings, TradeList openOfferTrades, TradeList pendingTrades, TradeList closedTrades,
public TradeManager(User user, AccountSettings accountSettings,
MessageService messageService, MailboxService mailboxService, AddressService addressService, BlockChainService blockChainService,
WalletService walletService, SignatureService signatureService, EncryptionService<MailboxMessage> encryptionService,
OfferBookService offerBookService, @Named("storage.dir") File storageDir) {
this.user = user;
this.accountSettings = accountSettings;
this.openOfferTrades = openOfferTrades;
this.pendingTrades = pendingTrades;
this.closedTrades = closedTrades;
this.messageService = messageService;
this.mailboxService = mailboxService;
this.addressService = addressService;
@ -113,6 +110,10 @@ public class TradeManager {
this.encryptionService = encryptionService;
this.offerBookService = offerBookService;
this.storageDir = storageDir;
this.openOfferTrades = new TradeList(storageDir, "OpenOfferTrades");
this.pendingTrades = new TradeList(storageDir, "PendingTrades");
this.closedTrades = new TradeList(storageDir, "ClosedTrades");
}

View File

@ -39,7 +39,7 @@ public class OffererAsBuyerModel extends SharedTradeModel implements Serializabl
transient private Storage<OffererAsBuyerModel> storage;
transient public final Trade trade;
public final Taker taker;
public final Offerer offerer;
@ -63,8 +63,8 @@ public class OffererAsBuyerModel extends SharedTradeModel implements Serializabl
this.trade = trade;
this.storage = new Storage<>(storageDir);
OffererAsBuyerModel persisted = storage.initAndGetPersisted(this, getClass().getSimpleName() + id);
OffererAsBuyerModel persisted = storage.initAndGetPersisted(this, getFileName());
if (persisted != null) {
log.debug("Model reconstructed form persisted model.");
@ -98,7 +98,7 @@ public class OffererAsBuyerModel extends SharedTradeModel implements Serializabl
@Override
public void onComplete() {
// Just in case of successful completion we delete our persisted object
storage.remove();
storage.remove(getFileName());
}
public String getTakeOfferFeeTxId() {
@ -108,4 +108,8 @@ public class OffererAsBuyerModel extends SharedTradeModel implements Serializabl
public void setTakeOfferFeeTxId(String takeOfferFeeTxId) {
this.takeOfferFeeTxId = takeOfferFeeTxId;
}
private String getFileName() {
return getClass().getSimpleName() + "_" + id;
}
}

View File

@ -55,7 +55,7 @@ public class TakerAsSellerModel extends SharedTradeModel implements Serializable
WalletService walletService,
BlockChainService blockChainService,
SignatureService signatureService,
User user,
User user,
File storageDir) {
super(trade.getOffer(),
messageService,
@ -67,7 +67,7 @@ public class TakerAsSellerModel extends SharedTradeModel implements Serializable
this.trade = trade;
this.storage = new Storage<>(storageDir);
TakerAsSellerModel persisted = storage.initAndGetPersisted(this, getClass().getSimpleName() + id);
TakerAsSellerModel persisted = storage.initAndGetPersisted(this, getFileName());
if (persisted != null) {
log.debug("Model reconstructed from persisted model.");
@ -101,10 +101,9 @@ public class TakerAsSellerModel extends SharedTradeModel implements Serializable
@Override
public void onComplete() {
// Just in case of successful completion we delete our persisted object
storage.remove();
storage.remove(getFileName());
}
public Transaction getTakeOfferFeeTx() {
return takeOfferFeeTx;
}
@ -120,4 +119,9 @@ public class TakerAsSellerModel extends SharedTradeModel implements Serializable
public void setPayoutTx(Transaction payoutTx) {
this.payoutTx = payoutTx;
}
private String getFileName() {
return getClass().getSimpleName() + "_" + id;
}
}