From 94fceeb34a90efecbca40e0269d79b061eb64c32 Mon Sep 17 00:00:00 2001 From: catbref Date: Thu, 30 May 2019 11:04:40 +0100 Subject: [PATCH] Move default blockchain.json into resources --- src/main/java/org/qora/block/BlockChain.java | 40 +++++++++++-------- src/main/java/org/qora/settings/Settings.java | 4 +- .../main/resources/blockchain.json | 0 3 files changed, 26 insertions(+), 18 deletions(-) rename blockchain.json => src/main/resources/blockchain.json (100%) diff --git a/src/main/java/org/qora/block/BlockChain.java b/src/main/java/org/qora/block/BlockChain.java index ecb913d8..912b7bc5 100644 --- a/src/main/java/org/qora/block/BlockChain.java +++ b/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 path + filename, or use resources-based default if filename is null. */ + 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 diff --git a/src/main/java/org/qora/settings/Settings.java b/src/main/java/org/qora/settings/Settings.java index bd1d94c1..f29e1cc2 100644 --- a/src/main/java/org/qora/settings/Settings.java +++ b/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() { diff --git a/blockchain.json b/src/main/resources/blockchain.json similarity index 100% rename from blockchain.json rename to src/main/resources/blockchain.json