From 72c983c68fba11e4e4a689e9aaeff9d97211a0f8 Mon Sep 17 00:00:00 2001 From: Igor Artamonov Date: Thu, 20 Jun 2019 16:39:15 -0400 Subject: [PATCH] solution: read configuration from dshackle.yaml (in current directory) --- .gitignore | 3 +- build.gradle | 1 + .../kotlin/io/emeraldpay/dshackle/Starter.kt | 4 +++ .../dshackle/YamlPropertySourceFactory.kt | 28 +++++++++++++++++++ src/main/resources/application.properties | 2 +- 5 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/io/emeraldpay/dshackle/YamlPropertySourceFactory.kt diff --git a/.gitignore b/.gitignore index 2469c991..2d7aca60 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .gradle/ build/ out/ -*.iml \ No newline at end of file +*.iml +*.yaml \ No newline at end of file diff --git a/build.gradle b/build.gradle index 8d021b68..27f3a45b 100644 --- a/build.gradle +++ b/build.gradle @@ -64,6 +64,7 @@ dependencies { compile 'io.projectreactor.addons:reactor-extra:3.2.3.RELEASE' compile 'io.projectreactor.kotlin:reactor-kotlin-extensions:1.0.0.M1' + compile 'org.yaml:snakeyaml:1.24' compile "io.infinitape:etherjar-domain:$etherjarVersion" compile "io.infinitape:etherjar-hex:$etherjarVersion" diff --git a/src/main/kotlin/io/emeraldpay/dshackle/Starter.kt b/src/main/kotlin/io/emeraldpay/dshackle/Starter.kt index 485221fe..5b06f471 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/Starter.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/Starter.kt @@ -1,10 +1,14 @@ package io.emeraldpay.dshackle +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.env.YamlPropertySourceLoader import org.springframework.context.annotation.Import +import org.springframework.context.annotation.PropertySource @SpringBootApplication(scanBasePackages = [ "io.emeraldpay.dshackle" ]) +@PropertySource("file:./dshackle.yaml", ignoreResourceNotFound = true, factory = YamlPropertySourceFactory::class) @Import(Config::class) open class Starter diff --git a/src/main/kotlin/io/emeraldpay/dshackle/YamlPropertySourceFactory.kt b/src/main/kotlin/io/emeraldpay/dshackle/YamlPropertySourceFactory.kt new file mode 100644 index 00000000..c37db036 --- /dev/null +++ b/src/main/kotlin/io/emeraldpay/dshackle/YamlPropertySourceFactory.kt @@ -0,0 +1,28 @@ +package io.emeraldpay.dshackle + +import org.apache.commons.lang3.StringUtils +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean +import org.springframework.core.env.PropertiesPropertySource +import org.springframework.core.env.PropertySource +import org.springframework.core.io.Resource +import org.springframework.core.io.support.EncodedResource +import org.springframework.core.io.support.PropertySourceFactory +import java.util.* + +class YamlPropertySourceFactory: PropertySourceFactory { + + override fun createPropertySource(name: String?, resource: EncodedResource): PropertySource<*> { + val loadedProperties = this.loadYamlIntoProperties(resource.resource) + + return PropertiesPropertySource(if (StringUtils.isNotBlank(name)) name else resource.resource.filename, loadedProperties) + + } + + private fun loadYamlIntoProperties(resource: Resource): Properties { + val factory = YamlPropertiesFactoryBean() + factory.setResources(resource) + factory.afterPropertiesSet() + + return factory.getObject() + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 61c5c2c5..3d6a802e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1 @@ -port=9001 +port=9001 \ No newline at end of file