Change property file handling

This commit is contained in:
Manfred Karrer 2015-05-21 23:48:16 +02:00
parent f24e539207
commit a57f246a0c
3 changed files with 48 additions and 51 deletions

View file

@ -28,10 +28,8 @@ import io.bitsquare.util.Utilities;
import io.bitsquare.util.spring.JOptCommandLinePropertySource; import io.bitsquare.util.spring.JOptCommandLinePropertySource;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -77,53 +75,44 @@ public class BitsquareEnvironment extends StandardEnvironment {
private final ResourceLoader resourceLoader = new DefaultResourceLoader(); private final ResourceLoader resourceLoader = new DefaultResourceLoader();
protected final String appName; private final String appName;
protected final String userDataDir; private final String userDataDir;
protected final String appDataDir; private final String appDataDir;
protected final String btcNetworkDir; private final String btcNetworkDir;
protected final String bootstrapNodePort; private final String bootstrapNodePort;
private BitcoinNetwork bitcoinNetwork;
public BitsquareEnvironment(OptionSet options) { public BitsquareEnvironment(OptionSet options) {
this(new JOptCommandLinePropertySource(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME, checkNotNull(options))); this(new JOptCommandLinePropertySource(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME, checkNotNull(options)));
} }
public BitcoinNetwork getBtcNetworkProperty() { public BitcoinNetwork getBitcoinNetwork() {
String dirString = Paths.get(userDataDir, appName).toString(); return bitcoinNetwork;
String fileString = Paths.get(dirString, BITCOIN_NETWORK_PROP).toString();
File dir = new File(dirString);
File file = new File(fileString);
if (!dir.exists())
dir.mkdirs();
if (!file.exists()) {
try {
file.createNewFile();
} catch (Throwable e) {
log.error(e.getMessage());
}
}
try (InputStream fileInputStream = new FileInputStream(file)) {
Properties properties = new Properties();
properties.load(fileInputStream);
String bitcoinNetwork = properties.getProperty("bitcoinNetwork", BitcoinNetwork.DEFAULT.name());
return BitcoinNetwork.valueOf(bitcoinNetwork);
} catch (Throwable e) {
e.printStackTrace();
log.error(e.getMessage());
return BitcoinNetwork.DEFAULT;
}
} }
public void setBitcoinNetwork(BitcoinNetwork bitcoinNetwork) { public void saveBitcoinNetwork(BitcoinNetwork bitcoinNetwork) {
String path = Paths.get(userDataDir, appName, BITCOIN_NETWORK_PROP).toString(); try {
File file = new File(path); Resource resource = getAppDirPropertiesResource();
try (FileOutputStream fos = new FileOutputStream(file)) { File file = resource.getFile();
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty("bitcoinNetwork", bitcoinNetwork.name()); if (file.exists()) {
properties.store(fos, null); Object propertiesObject = appDirProperties().getSource();
} catch (IOException e) { if (propertiesObject instanceof Properties) {
e.printStackTrace(); properties = (Properties) propertiesObject;
log.error(e.getMessage()); }
else {
log.warn("propertiesObject not instance of Properties");
}
}
properties.setProperty(BitcoinNetwork.KEY, bitcoinNetwork.name());
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
properties.store(fileOutputStream, null);
} catch (IOException e1) {
log.error(e1.getMessage());
}
} catch (Exception e2) {
e2.printStackTrace();
} }
} }
@ -140,11 +129,6 @@ public class BitsquareEnvironment extends StandardEnvironment {
(String) commandLineProperties.getProperty(APP_DATA_DIR_KEY) : (String) commandLineProperties.getProperty(APP_DATA_DIR_KEY) :
appDataDir(userDataDir, appName); appDataDir(userDataDir, appName);
btcNetworkDir = Paths.get(appDataDir, getBtcNetworkProperty().name().toLowerCase()).toString();
File btcNetworkDirFile = new File(btcNetworkDir);
if (!btcNetworkDirFile.exists())
btcNetworkDirFile.mkdir();
bootstrapNodePort = commandLineProperties.containsProperty(TomP2PModule.BOOTSTRAP_NODE_PORT_KEY) ? bootstrapNodePort = commandLineProperties.containsProperty(TomP2PModule.BOOTSTRAP_NODE_PORT_KEY) ?
(String) commandLineProperties.getProperty(TomP2PModule.BOOTSTRAP_NODE_PORT_KEY) : "-1"; (String) commandLineProperties.getProperty(TomP2PModule.BOOTSTRAP_NODE_PORT_KEY) : "-1";
@ -154,15 +138,27 @@ public class BitsquareEnvironment extends StandardEnvironment {
propertySources.addLast(appDirProperties()); propertySources.addLast(appDirProperties());
propertySources.addLast(homeDirProperties()); propertySources.addLast(homeDirProperties());
propertySources.addLast(classpathProperties()); propertySources.addLast(classpathProperties());
bitcoinNetwork = BitcoinNetwork.valueOf(getProperty(BitcoinNetwork.KEY, BitcoinNetwork.DEFAULT.name()).toUpperCase());
btcNetworkDir = Paths.get(appDataDir, bitcoinNetwork.name().toLowerCase()).toString();
File btcNetworkDirFile = new File(btcNetworkDir);
if (!btcNetworkDirFile.exists())
btcNetworkDirFile.mkdir();
// btcNetworkDir used in defaultProperties
propertySources.addLast(defaultProperties()); propertySources.addLast(defaultProperties());
} catch (Exception ex) { } catch (Exception ex) {
throw new BitsquareException(ex); throw new BitsquareException(ex);
} }
} }
private Resource getAppDirPropertiesResource() {
String location = String.format("file:%s/bitsquare.properties", appDataDir);
return resourceLoader.getResource(location);
}
PropertySource<?> appDirProperties() throws Exception { PropertySource<?> appDirProperties() throws Exception {
String location = String.format("file:%s/bitsquare.properties", btcNetworkDir); Resource resource = getAppDirPropertiesResource();
Resource resource = resourceLoader.getResource(location);
if (!resource.exists()) if (!resource.exists())
return new PropertySource.StubPropertySource(BITSQUARE_APP_DIR_PROPERTY_SOURCE_NAME); return new PropertySource.StubPropertySource(BITSQUARE_APP_DIR_PROPERTY_SOURCE_NAME);

View file

@ -88,7 +88,7 @@ public class Preferences implements Serializable {
displaySecurityDepositInfo = persisted.getDisplaySecurityDepositInfo(); displaySecurityDepositInfo = persisted.getDisplaySecurityDepositInfo();
} }
setBitcoinNetwork(bitsquareEnvironment.getBtcNetworkProperty()); this.bitcoinNetwork = bitsquareEnvironment.getBitcoinNetwork();
// Use that to guarantee update of the serializable field and to make a storage update in case of a change // Use that to guarantee update of the serializable field and to make a storage update in case of a change
btcDenominationProperty.addListener((ov) -> { btcDenominationProperty.addListener((ov) -> {
@ -134,7 +134,7 @@ public class Preferences implements Serializable {
public void setBitcoinNetwork(BitcoinNetwork bitcoinNetwork) { public void setBitcoinNetwork(BitcoinNetwork bitcoinNetwork) {
if (this.bitcoinNetwork != bitcoinNetwork) if (this.bitcoinNetwork != bitcoinNetwork)
bitsquareEnvironment.setBitcoinNetwork(bitcoinNetwork); bitsquareEnvironment.saveBitcoinNetwork(bitcoinNetwork);
this.bitcoinNetwork = bitcoinNetwork; this.bitcoinNetwork = bitcoinNetwork;
} }

View file

@ -4,4 +4,5 @@ Release notes:
- Add support for new release download - Add support for new release download
- Only use mailbox for certain P2P messages - Only use mailbox for certain P2P messages
- Bootstrap node update - Bootstrap node update
- Fix bug with empty log file - Fix bug with empty log file
- Add warning popup when using mainnet