Commit 8dd75331 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Refactor settings loader

parent d57b099b
...@@ -32,7 +32,7 @@ class App { ...@@ -32,7 +32,7 @@ class App {
SftpClient(settings.sftpSettings).use { sftp -> SftpClient(settings.sftpSettings).use { sftp ->
val validator = FileValidation(sftp) val validator = FileValidation(sftp)
val files = sftp.listFiles(settings.appSettings["directory"].orEmpty()).map { File(it) } val files = sftp.listFiles(settings.appSettings.getProperty("directory")).map { File(it) }
for (file in files) { for (file in files) {
val report = validator.validate(file) val report = validator.validate(file)
......
...@@ -51,14 +51,15 @@ class SettingsLoader { ...@@ -51,14 +51,15 @@ class SettingsLoader {
private val rawKafkaProperties: Map<String, String> private val rawKafkaProperties: Map<String, String>
val topic: String val topic: String
val appSettings: Map<String, String> val appSettings = Properties()
val sftpSettings: Map<String, String> val sftpSettings = Properties()
val kafkaProducerProperties = Properties() val kafkaProducerProperties = Properties()
val mappedYaml: Map<String, Any>
init { init {
try { try {
val rawYaml = loadYaml() val rawYaml = loadYaml()
val mappedYaml = rawYaml as Map<String, Any> mappedYaml = rawYaml as Map<String, Any>
val kafkaOptions = mappedYaml["kafka"] as Map<String, Any> val kafkaOptions = mappedYaml["kafka"] as Map<String, Any>
val topics = kafkaOptions["topic"] as Map<String, String> val topics = kafkaOptions["topic"] as Map<String, String>
topic = topics["out"].orEmpty() topic = topics["out"].orEmpty()
...@@ -67,15 +68,13 @@ class SettingsLoader { ...@@ -67,15 +68,13 @@ class SettingsLoader {
} }
rawKafkaProperties = kafkaOptions["producer"] as Map<String, String> rawKafkaProperties = kafkaOptions["producer"] as Map<String, String>
appSettings = rawYaml["app"] as Map<String, String> appSettings.setProperty("directory", addSetting("app", "directory"))
ensurePropertyIsSet("directory", appSettings)
sftpSettings = rawYaml["sftp"] as Map<String, String> sftpSettings.setProperty("host", addSetting("sftp", "host"))
ensurePropertyIsSet("host", sftpSettings) sftpSettings.setProperty("port", addSetting("sftp", "port"))
ensurePropertyIsSet("port", sftpSettings) sftpSettings.setProperty("user", addSetting("sftp", "user"))
ensurePropertyIsSet("user", sftpSettings) sftpSettings.setProperty("password", addSetting("sftp", "password"))
ensurePropertyIsSet("password", sftpSettings) sftpSettings.setProperty("fingerprint", addSetting("sftp", "fingerprint"))
ensurePropertyIsSet("fingerprint", sftpSettings)
} catch (ex: ClassCastException) { } catch (ex: ClassCastException) {
ex.printStackTrace() ex.printStackTrace()
log.error("The properties file has an invalid structure: $ex") log.error("The properties file has an invalid structure: $ex")
...@@ -87,9 +86,16 @@ class SettingsLoader { ...@@ -87,9 +86,16 @@ class SettingsLoader {
initializeKafkaProperties() initializeKafkaProperties()
} }
private fun ensurePropertyIsSet(setting: String, settings: Map<String, String>) { private fun addSetting(base: String, setting: String): String {
if (settings[setting].isNullOrEmpty()) { val settings = mappedYaml[base] as Map<String, Any>
throw MissingSettingException("missing", setting) if (settings.containsKey(setting)) {
return when (val value = settings[setting]) {
is String -> if (value.isNotEmpty()) value else throw MissingSettingException("missing", "$base.$setting")
is Int -> value.toString()
else -> throw MissingSettingException("missing", "$base.$setting")
}
} else {
throw MissingSettingException("missing", "$base.$setting")
} }
} }
......
...@@ -22,6 +22,7 @@ import java.io.Closeable ...@@ -22,6 +22,7 @@ import java.io.Closeable
import java.io.File import java.io.File
import java.net.ConnectException import java.net.ConnectException
import java.net.UnknownHostException import java.net.UnknownHostException
import java.util.Properties
import kotlin.system.exitProcess import kotlin.system.exitProcess
import net.schmizz.sshj.SSHClient import net.schmizz.sshj.SSHClient
import net.schmizz.sshj.sftp.FileAttributes import net.schmizz.sshj.sftp.FileAttributes
...@@ -32,16 +33,16 @@ import net.schmizz.sshj.sftp.SFTPClient ...@@ -32,16 +33,16 @@ import net.schmizz.sshj.sftp.SFTPClient
import net.schmizz.sshj.userauth.UserAuthException import net.schmizz.sshj.userauth.UserAuthException
import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.LogManager
class SftpClient(sftpSettings: Map<String, String>) : Closeable { class SftpClient(sftpSettings: Properties) : Closeable {
private val log = LogManager.getLogger("SftpClient") private val log = LogManager.getLogger("SftpClient")
private val ssh = SSHClient() private val ssh = SSHClient()
private val instance: SFTPClient private val instance: SFTPClient
init { init {
try { try {
ssh.loadKnownHosts() ssh.loadKnownHosts()
ssh.addHostKeyVerifier(sftpSettings["fingerprint"]) ssh.addHostKeyVerifier(sftpSettings.getProperty("fingerprint"))
ssh.connect(sftpSettings["host"]) ssh.connect(sftpSettings.getProperty("host"))
ssh.authPassword(sftpSettings["user"], sftpSettings["password"]) ssh.authPassword(sftpSettings.getProperty("user"), sftpSettings.getProperty("password"))
instance = ssh.newSFTPClient() instance = ssh.newSFTPClient()
} catch (ex: UserAuthException) { } catch (ex: UserAuthException) {
log.error("SFTP User Authentication Error: Invalid user authentication supplied.") log.error("SFTP User Authentication Error: Invalid user authentication supplied.")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment