From 5b7fb86b6b1b5e288df98285a01ef1e9f77aed62 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Tue, 22 Mar 2016 10:52:24 +0000 Subject: [PATCH] Use TypeSafe Config library instead of Java properties files for the config file format. The default formats are compatible so this should not be disruptive. --- build.gradle | 5 ++- .../kotlin/core/node/NodeConfiguration.kt | 34 +++++-------------- src/main/kotlin/demos/TraderDemo.kt | 12 +++---- src/main/resources/reference.conf | 2 ++ 4 files changed, 18 insertions(+), 35 deletions(-) create mode 100644 src/main/resources/reference.conf diff --git a/build.gradle b/build.gradle index 36e1441744..b783282a5e 100644 --- a/build.gradle +++ b/build.gradle @@ -108,7 +108,10 @@ dependencies { compile "io.dropwizard.metrics:metrics-core:3.1.2" // JimFS: in memory java.nio filesystem. Used for test and simulation utilities. - compile 'com.google.jimfs:jimfs:1.1' + compile "com.google.jimfs:jimfs:1.1" + + // TypeSafe Config: for simple and human friendly config files. + compile "com.typesafe:config:1.3.0" // Unit testing helpers. testCompile 'junit:junit:4.12' diff --git a/src/main/kotlin/core/node/NodeConfiguration.kt b/src/main/kotlin/core/node/NodeConfiguration.kt index d4233c17ef..a61a80cd38 100644 --- a/src/main/kotlin/core/node/NodeConfiguration.kt +++ b/src/main/kotlin/core/node/NodeConfiguration.kt @@ -8,37 +8,19 @@ package core.node -import java.util.* -import kotlin.reflect.declaredMemberProperties +import com.typesafe.config.Config +import com.typesafe.config.ConfigFactory +import kotlin.reflect.KProperty interface NodeConfiguration { val myLegalName: String val exportJMXto: String } -object DefaultConfiguration : NodeConfiguration { - override val myLegalName: String = "Vast Global MegaCorp" - override val exportJMXto: String = "" // can be "http" or empty +// Allow the use of "String by config" syntax. TODO: Make it more flexible. +operator fun Config.getValue(receiver: NodeConfigurationFromConfig, metadata: KProperty<*>) = getString(metadata.name) - fun toProperties(): Properties { - val settings = DefaultConfiguration::class.declaredMemberProperties.map { it.name to it.get(this@DefaultConfiguration).toString() } - val p = Properties().apply { - for (setting in settings) { - setProperty(setting.first, setting.second) - } - } - return p - } -} - -/** - * A simple wrapper around a plain old Java .properties file. The keys have the same name as in the source code. - * - * TODO: Replace Java properties file with a better config file format (maybe yaml). - * We want to be able to configure via a GUI too, so an ability to round-trip whitespace, comments etc when machine - * editing the file is a must-have. - */ -class NodeConfigurationFromProperties(private val properties: Properties) : NodeConfiguration { - override val myLegalName: String get() = properties.getProperty("myLegalName") - override val exportJMXto: String get() = properties.getProperty("exportJMXto") +class NodeConfigurationFromConfig(val config: Config = ConfigFactory.load()) : NodeConfiguration { + override val myLegalName: String by config + override val exportJMXto: String by config } \ No newline at end of file diff --git a/src/main/kotlin/demos/TraderDemo.kt b/src/main/kotlin/demos/TraderDemo.kt index a99ac5e2d5..ea914c01fe 100644 --- a/src/main/kotlin/demos/TraderDemo.kt +++ b/src/main/kotlin/demos/TraderDemo.kt @@ -10,16 +10,16 @@ package demos import co.paralleluniverse.fibers.Suspendable import com.google.common.net.HostAndPort +import com.typesafe.config.ConfigFactory import contracts.CommercialPaper import core.* import core.crypto.SecureHash import core.crypto.generateKeyPair import core.messaging.LegallyIdentifiableNode import core.messaging.SingleMessageRecipient -import core.node.DefaultConfiguration import core.node.Node import core.node.NodeConfiguration -import core.node.NodeConfigurationFromProperties +import core.node.NodeConfigurationFromConfig import core.node.services.ArtemisMessagingService import core.node.services.NodeAttachmentService import core.node.services.NodeWalletService @@ -37,7 +37,6 @@ import java.nio.file.Path import java.nio.file.Paths import java.security.PublicKey import java.time.Instant -import java.util.* import kotlin.system.exitProcess import kotlin.test.assertEquals @@ -291,11 +290,8 @@ private fun loadConfigFile(configFile: Path): NodeConfiguration { askAdminToEditConfig(configFile) } - val config = configFile.toFile().reader().use { - NodeConfigurationFromProperties( - Properties(DefaultConfiguration.toProperties()).apply { load(it) } - ) - } + System.setProperty("config.file", configFile.toAbsolutePath().toString()) + val config = NodeConfigurationFromConfig(ConfigFactory.load()) // Make sure admin did actually edit at least the legal name. if (config.myLegalName == defaultLegalName) diff --git a/src/main/resources/reference.conf b/src/main/resources/reference.conf new file mode 100644 index 0000000000..00fff9a9d9 --- /dev/null +++ b/src/main/resources/reference.conf @@ -0,0 +1,2 @@ +myLegalName = "Vast Global MegaCorp, Ltd" +exportJMXto = "http" \ No newline at end of file