Move default blockchain.json into resources

This commit is contained in:
catbref 2019-05-30 11:04:40 +01:00
parent a4f2cf50b0
commit 94fceeb34a
3 changed files with 26 additions and 18 deletions

View File

@ -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

View File

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