Browse Source

Move default blockchain.json into resources

split-DB
catbref 5 years ago
parent
commit
94fceeb34a
  1. 40
      src/main/java/org/qora/block/BlockChain.java
  2. 4
      src/main/java/org/qora/settings/Settings.java
  3. 0
      src/main/resources/blockchain.json

40
src/main/java/org/qora/block/BlockChain.java

@ -1,9 +1,7 @@
package org.qora.block;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.MathContext;
import java.sql.SQLException;
@ -122,7 +120,8 @@ public class BlockChain {
return instance;
}
public static void fileInstance(String filename) {
/** Use blockchain config read from <tt>path</tt> + <tt>filename</tt>, or use resources-based default if <tt>filename</tt> is <tt>null</tt>. */
public static void fileInstance(String path, String filename) {
JAXBContext jc;
Unmarshaller unmarshaller;
@ -147,15 +146,30 @@ public class BlockChain {
}
BlockChain blockchain = null;
StreamSource jsonSource;
LOGGER.info("Using blockchain config file: " + filename);
if (filename != null) {
LOGGER.info("Using blockchain config file: " + path + filename);
// Create the StreamSource by creating Reader to the JSON input
try (Reader settingsReader = new FileReader(filename)) {
StreamSource json = new StreamSource(settingsReader);
File jsonFile = new File(path + filename);
if (!jsonFile.exists()) {
LOGGER.error("Blockchain config file not found: " + path + filename);
throw new RuntimeException("Blockchain config file not found: " + path + filename);
}
jsonSource = new StreamSource(jsonFile);
} else {
LOGGER.info("Using default, resources-based blockchain config");
ClassLoader classLoader = BlockChain.class.getClassLoader();
InputStream in = classLoader.getResourceAsStream("blockchain.json");
jsonSource = new StreamSource(in);
}
try {
// Attempt to unmarshal JSON stream to BlockChain config
blockchain = unmarshaller.unmarshal(json, BlockChain.class).getValue();
blockchain = unmarshaller.unmarshal(jsonSource, BlockChain.class).getValue();
} catch (UnmarshalException e) {
Throwable linkedException = e.getLinkedException();
if (linkedException instanceof XMLMarshalException) {
@ -166,15 +180,9 @@ public class BlockChain {
LOGGER.error("Unable to process blockchain config file", e);
throw new RuntimeException("Unable to process blockchain config file", e);
} catch (FileNotFoundException e) {
LOGGER.error("Blockchain config file not found: " + filename);
throw new RuntimeException("Blockchain config file not found: " + filename);
} catch (JAXBException e) {
LOGGER.error("Unable to process blockchain config file", e);
throw new RuntimeException("Unable to process blockchain config file", e);
} catch (IOException e) {
LOGGER.error("Unable to process blockchain config file", e);
throw new RuntimeException("Unable to process blockchain config file", e);
}
// Validate config

4
src/main/java/org/qora/settings/Settings.java

@ -72,7 +72,7 @@ public class Settings {
private int maxPeers = 30;
// Which blockchains this node is running
private String blockchainConfig = "blockchain.json";
private String blockchainConfig = null; // use default from resources
private boolean useBitcoinTestNet = false;
// Repository related
@ -176,7 +176,7 @@ public class Settings {
instance = settings;
// Now read blockchain config
BlockChain.fileInstance(settings.getUserPath() + settings.getBlockchainConfig());
BlockChain.fileInstance(settings.getUserPath(), settings.getBlockchainConfig());
}
private void validate() {

0
blockchain.json → src/main/resources/blockchain.json

Loading…
Cancel
Save