-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from ghubstan/typesafe-config-poc
Demo lightbend / typesafe configuration library
- Loading branch information
Showing
7 changed files
with
191 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
common/src/main/java/network/misq/common/configuration/MisqConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* This file is part of Bisq. | ||
* | ||
* Bisq is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* Bisq is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package network.misq.common.configuration; | ||
|
||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigFactory; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* TIPS | ||
* <p> | ||
* Set the Java system property -Dconfig.trace=loads to get output on stderr describing each file that is loaded. | ||
* (The output will show the library attempting to load misq.json, misq.properties, and reference.conf, but not fail.) | ||
* <p> | ||
* Use myConfig.root().render() to get a Config as a string with comments showing where each value came from. | ||
* This string can be printed out on console or logged to a file etc. | ||
* <p> | ||
* If you see errors like com.typesafe.config.ConfigException$Missing: No configuration setting found for key foo, | ||
* and you're sure that key is defined in your config file, they might appear e.g. when you're loading configuration | ||
* from a thread that's not the JVM's main thread. Try passing the ClassLoader in manually - e.g. with | ||
* ConfigFactory.load(getClass().getClassLoader()) or setting the context class loader. If you don't pass one, | ||
* Lightbend Config uses the calling thread's contextClassLoader, and in some cases, it may not have your configuration | ||
* files in its classpath, so loading the config on that thread can yield unexpected, erroneous results. | ||
* <p> | ||
* REFERENCES | ||
* <p> | ||
* https://github.com/lightbend/config | ||
* https://www.stubbornjava.com/posts/typesafe-config-features-and-example-usage | ||
* https://florentfo.rest/2019/01/07/configuring-spark-applications-with-typesafe-config.html | ||
*/ | ||
@Slf4j | ||
public class MisqConfig { | ||
|
||
public static final String NETWORK_CONFIG_PATH = "misq.networkConfig"; | ||
public static final String NETWORK_IO_POOL_CONFIG_PATH = NETWORK_CONFIG_PATH + ".networkIOPool"; | ||
|
||
private static final Config MISQ_CONFIG = ConfigFactory.load("misq"); | ||
|
||
static { | ||
try { | ||
MISQ_CONFIG.checkValid(ConfigFactory.defaultReference(), "misq"); | ||
} catch (Exception ex) { | ||
throw new IllegalStateException("misq.conf validation failed", ex); | ||
} | ||
} | ||
|
||
/** | ||
* Return the global Config object. | ||
* | ||
* @return Config | ||
*/ | ||
public static Config getGlobalConfig() { | ||
return MISQ_CONFIG; | ||
} | ||
|
||
/** | ||
* Return a Config for a given configuration path, e.g., | ||
* "misq.networkConfig.torPeerGroupServiceConfig.peerExchangeConfig". | ||
* | ||
* @param path String representing a valid path in misq.conf | ||
* @return Config | ||
*/ | ||
public static Config getConfig(String path) { | ||
MISQ_CONFIG.checkValid(ConfigFactory.defaultReference(), path); | ||
return MISQ_CONFIG.getConfig(path); | ||
} | ||
|
||
public static void main(String[] args) { | ||
log.info("MISQ_CONFIG = {}", MISQ_CONFIG); | ||
log.info("misq.networkConfig.torPeerGroupServiceConfig = {}", getConfig("misq.networkConfig.torPeerGroupServiceConfig")); | ||
log.info("misq.networkConfig.i2pPeerGroupServiceConfig = {}", getConfig("misq.networkConfig.i2pPeerGroupServiceConfig")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// A comment | ||
# Another comment | ||
misq { | ||
|
||
networkConfig = { | ||
|
||
networkIOPool { | ||
name = "NETWORK_IO_POOL" | ||
corePoolSize = 1 | ||
maximumPoolSize = 5000 | ||
keepAliveTimeInSec = 10 | ||
} | ||
|
||
baseDirPath = "some/path" | ||
supportedTransportTypes = "TOR,I2P" | ||
|
||
transportConfig { | ||
baseDirPath = "some/transport-config/path" | ||
} | ||
|
||
serviceNodeConfig { | ||
p2pServiceNodeConfig="PEER_GROUP,DATA,CONFIDENTIAL,RELAY,MONITOR" | ||
} | ||
|
||
i2pPeerGroupServiceConfig { | ||
peerGroupConfig { | ||
minNumConnectedPeers=8 | ||
maxNumConnectedPeers=12 | ||
minNumReportedPeers=30 | ||
} | ||
|
||
peerExchangeConfig { | ||
numSeeNodesAtBoostrap=2 | ||
numPersistedPeersAtBoostrap=100 | ||
numReportedPeersAtBoostrap=50 | ||
} | ||
|
||
keepAliveServiceConfig { | ||
bootstrapTime=1111 | ||
interval=2222 | ||
} | ||
} | ||
|
||
torPeerGroupServiceConfig { | ||
peerGroupConfig { | ||
minNumConnectedPeers=8 | ||
maxNumConnectedPeers=12 | ||
minNumReportedPeers=30 | ||
} | ||
|
||
peerExchangeConfig { | ||
numSeeNodesAtBoostrap=2 | ||
numPersistedPeersAtBoostrap=100 | ||
numReportedPeersAtBoostrap=50 | ||
} | ||
|
||
keepAliveServiceConfig { | ||
bootstrapTime=1111 | ||
interval=2222 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
network/src/test/java/network/misq/network/TypesafeConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package network.misq.network; | ||
|
||
import com.typesafe.config.Config; | ||
import network.misq.common.configuration.MisqConfig; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static network.misq.common.configuration.MisqConfig.NETWORK_IO_POOL_CONFIG_PATH; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
public class TypesafeConfigTest { | ||
|
||
@Test | ||
public void testNetworkPoolConfig() { | ||
Config config = MisqConfig.getConfig(NETWORK_IO_POOL_CONFIG_PATH); | ||
assertNotNull(config); | ||
|
||
assertEquals("NETWORK_IO_POOL", config.getString("name")); | ||
assertEquals(1, config.getInt("corePoolSize")); | ||
assertEquals(5000, config.getInt("maximumPoolSize")); | ||
assertEquals(10, config.getLong("keepAliveTimeInSec")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters