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.

This commit is contained in:
Mike Hearn 2016-03-22 10:52:24 +00:00
parent 9f7ae4c61d
commit 5b7fb86b6b
4 changed files with 18 additions and 35 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1,2 @@
myLegalName = "Vast Global MegaCorp, Ltd"
exportJMXto = "http"