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: case OFFERER_BUYER_START_PAYMENT:
processBar.setSelectedIndex(1); processBar.setSelectedIndex(1);
paymentStartedButton.setDisable(false);
setPaymentsControlsVisible(true); setPaymentsControlsVisible(true);
statusTextField.setText("Deposit transaction has at least one block chain confirmation. " + 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) { public void removeFile(String fileName) {
storageFile.delete(); 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()); File backupDir = new File(Paths.get(dir.getAbsolutePath(), "backup").toString());
if (backupDir.exists()) { if (backupDir.exists()) {
File backupFile = new File(backupDir, serializable.getClass().getSimpleName()); File backupFile = new File(Paths.get(dir.getAbsolutePath(), "backup", fileName).toString());
if (backupFile.exists()) if (backupFile.exists()) {
backupFile.delete(); 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()); File corruptedBackupDir = new File(Paths.get(dir.getAbsolutePath(), "corrupted").toString());
if (!corruptedBackupDir.exists()) if (!corruptedBackupDir.exists())
corruptedBackupDir.mkdir(); 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()); File backupDir = new File(Paths.get(dir.getAbsolutePath(), "backup").toString());
if (!backupDir.exists()) if (!backupDir.exists())
backupDir.mkdir(); 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.InvalidClassException;
import java.io.Serializable; import java.io.Serializable;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.inject.Inject; import javax.inject.Inject;
@ -60,6 +58,7 @@ public class Storage<T extends Serializable> {
private FileManager<T> fileManager; private FileManager<T> fileManager;
private File storageFile; private File storageFile;
private T serializable; private T serializable;
private String fileName;
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@ -77,6 +76,7 @@ public class Storage<T extends Serializable> {
public T initAndGetPersisted(T serializable, String fileName) { public T initAndGetPersisted(T serializable, String fileName) {
this.serializable = serializable; this.serializable = serializable;
this.fileName = fileName;
storageFile = new File(dir, fileName); storageFile = new File(dir, fileName);
fileManager = new FileManager<>(dir, storageFile, 500, TimeUnit.MILLISECONDS); fileManager = new FileManager<>(dir, storageFile, 500, TimeUnit.MILLISECONDS);
@ -91,8 +91,8 @@ public class Storage<T extends Serializable> {
fileManager.saveLater(serializable); fileManager.saveLater(serializable);
} }
public void remove() { public void remove(String fileName) {
fileManager.removeFile(serializable); 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 // If we did not get any exception we can be sure the data are consistent so we make a backup
now = System.currentTimeMillis(); now = System.currentTimeMillis();
fileManager.backupFile(serializable); fileManager.backupFile(fileName);
log.info("Backup {} completed in {}msec", serializable.getClass().getSimpleName(), System.currentTimeMillis() - now); log.info("Backup {} completed in {}msec", serializable.getClass().getSimpleName(), System.currentTimeMillis() - now);
return persistedObject; 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."); 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 { try {
// In case the persisted data have been critical (keys) we keep a backup which might be used for recovery // 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()), fileManager.removeAndBackupFile(fileName);
serializable.getClass().getSimpleName());
} catch (IOException e1) { } catch (IOException e1) {
e1.printStackTrace(); e1.printStackTrace();
log.error(e1.getMessage()); log.error(e1.getMessage());

View file

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

View file

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

View file

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

View file

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