@Test public void testGenerateNodeIdFromPropsFileReadsExistingFile() throws Exception { ECKey key = new ECKey(); String expectedNodePrivateKey = Hex.toHexString(key.getPrivKeyBytes()); String expectedNodeId = Hex.toHexString(key.getNodeId()); createNodeIdPropertiesFile("database-test", key); new GetNodeIdFromPropsFile("database-test").getNodePrivateKey(); assertTrue(nodeIdPropertiesFile.exists()); String contents = FileCopyUtils.copyToString(new FileReader(nodeIdPropertiesFile)); String[] lines = StringUtils.tokenizeToStringArray(contents, "\n"); assertEquals(4, lines.length); assertTrue(lines[0].startsWith("#Generated NodeID.")); assertTrue(lines[1].startsWith("#")); assertTrue(lines[2].startsWith("nodeIdPrivateKey=" + expectedNodePrivateKey)); assertTrue(lines[3].startsWith("nodeId=" + expectedNodeId)); }
@Override public String getNodePrivateKey() { Properties props = new Properties(); File file = new File(databaseDir, "nodeId.properties"); if (file.canRead()) { try (Reader r = new FileReader(file)) { props.load(r); return props.getProperty("nodeIdPrivateKey"); } catch (IOException e) { throw new RuntimeException("Error reading 'nodeId.properties' file", e); } } else { if (fallbackGenerateNodeIdStrategy != null) { return fallbackGenerateNodeIdStrategy.getNodePrivateKey(); } else { throw new RuntimeException("Can't read 'nodeId.properties' and no fallback method has been set"); } } }
GetNodeIdFromPropsFile implements GenerateNodeIdStrategy { @Override public String getNodePrivateKey() { Properties props = new Properties(); File file = new File(databaseDir, "nodeId.properties"); if (file.canRead()) { try (Reader r = new FileReader(file)) { props.load(r); return props.getProperty("nodeIdPrivateKey"); } catch (IOException e) { throw new RuntimeException("Error reading 'nodeId.properties' file", e); } } else { if (fallbackGenerateNodeIdStrategy != null) { return fallbackGenerateNodeIdStrategy.getNodePrivateKey(); } else { throw new RuntimeException("Can't read 'nodeId.properties' and no fallback method has been set"); } } } }
GetNodeIdFromPropsFile implements GenerateNodeIdStrategy { @Override public String getNodePrivateKey() { Properties props = new Properties(); File file = new File(databaseDir, "nodeId.properties"); if (file.canRead()) { try (Reader r = new FileReader(file)) { props.load(r); return props.getProperty("nodeIdPrivateKey"); } catch (IOException e) { throw new RuntimeException("Error reading 'nodeId.properties' file", e); } } else { if (fallbackGenerateNodeIdStrategy != null) { return fallbackGenerateNodeIdStrategy.getNodePrivateKey(); } else { throw new RuntimeException("Can't read 'nodeId.properties' and no fallback method has been set"); } } } GetNodeIdFromPropsFile(String databaseDir); }
GetNodeIdFromPropsFile implements GenerateNodeIdStrategy { @Override public String getNodePrivateKey() { Properties props = new Properties(); File file = new File(databaseDir, "nodeId.properties"); if (file.canRead()) { try (Reader r = new FileReader(file)) { props.load(r); return props.getProperty("nodeIdPrivateKey"); } catch (IOException e) { throw new RuntimeException("Error reading 'nodeId.properties' file", e); } } else { if (fallbackGenerateNodeIdStrategy != null) { return fallbackGenerateNodeIdStrategy.getNodePrivateKey(); } else { throw new RuntimeException("Can't read 'nodeId.properties' and no fallback method has been set"); } } } GetNodeIdFromPropsFile(String databaseDir); @Override String getNodePrivateKey(); GenerateNodeIdStrategy withFallback(GenerateNodeIdStrategy generateNodeIdStrategy); }
GetNodeIdFromPropsFile implements GenerateNodeIdStrategy { @Override public String getNodePrivateKey() { Properties props = new Properties(); File file = new File(databaseDir, "nodeId.properties"); if (file.canRead()) { try (Reader r = new FileReader(file)) { props.load(r); return props.getProperty("nodeIdPrivateKey"); } catch (IOException e) { throw new RuntimeException("Error reading 'nodeId.properties' file", e); } } else { if (fallbackGenerateNodeIdStrategy != null) { return fallbackGenerateNodeIdStrategy.getNodePrivateKey(); } else { throw new RuntimeException("Can't read 'nodeId.properties' and no fallback method has been set"); } } } GetNodeIdFromPropsFile(String databaseDir); @Override String getNodePrivateKey(); GenerateNodeIdStrategy withFallback(GenerateNodeIdStrategy generateNodeIdStrategy);  }