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

Refactor settings loader

parent d57b099b
......@@ -32,7 +32,7 @@ class App {
SftpClient(settings.sftpSettings).use { 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) {
val report = validator.validate(file)
......
......@@ -51,14 +51,15 @@ class SettingsLoader {
private val rawKafkaProperties: Map<String, String>
val topic: String
val appSettings: Map<String, String>
val sftpSettings: Map<String, String>
val appSettings = Properties()
val sftpSettings = Properties()
val kafkaProducerProperties = Properties()
val mappedYaml: Map<String, Any>
init {
try {
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 topics = kafkaOptions["topic"] as Map<String, String>
topic = topics["out"].orEmpty()
......@@ -67,15 +68,13 @@ class SettingsLoader {
}
rawKafkaProperties = kafkaOptions["producer"] as Map<String, String>
appSettings = rawYaml["app"] as Map<String, String>
ensurePropertyIsSet("directory", appSettings)
appSettings.setProperty("directory", addSetting("app", "directory"))
sftpSettings = rawYaml["sftp"] as Map<String, String>
ensurePropertyIsSet("host", sftpSettings)
ensurePropertyIsSet("port", sftpSettings)
ensurePropertyIsSet("user", sftpSettings)
ensurePropertyIsSet("password", sftpSettings)
ensurePropertyIsSet("fingerprint", sftpSettings)
sftpSettings.setProperty("host", addSetting("sftp", "host"))
sftpSettings.setProperty("port", addSetting("sftp", "port"))
sftpSettings.setProperty("user", addSetting("sftp", "user"))
sftpSettings.setProperty("password", addSetting("sftp", "password"))
sftpSettings.setProperty("fingerprint", addSetting("sftp", "fingerprint"))
} catch (ex: ClassCastException) {
ex.printStackTrace()
log.error("The properties file has an invalid structure: $ex")
......@@ -87,9 +86,16 @@ class SettingsLoader {
initializeKafkaProperties()
}
private fun ensurePropertyIsSet(setting: String, settings: Map<String, String>) {
if (settings[setting].isNullOrEmpty()) {
throw MissingSettingException("missing", setting)
private fun addSetting(base: String, setting: String): String {
val settings = mappedYaml[base] as Map<String, Any>
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
import java.io.File
import java.net.ConnectException
import java.net.UnknownHostException
import java.util.Properties
import kotlin.system.exitProcess
import net.schmizz.sshj.SSHClient
import net.schmizz.sshj.sftp.FileAttributes
......@@ -32,16 +33,16 @@ import net.schmizz.sshj.sftp.SFTPClient
import net.schmizz.sshj.userauth.UserAuthException
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 ssh = SSHClient()
private val instance: SFTPClient
init {
try {
ssh.loadKnownHosts()
ssh.addHostKeyVerifier(sftpSettings["fingerprint"])
ssh.connect(sftpSettings["host"])
ssh.authPassword(sftpSettings["user"], sftpSettings["password"])
ssh.addHostKeyVerifier(sftpSettings.getProperty("fingerprint"))
ssh.connect(sftpSettings.getProperty("host"))
ssh.authPassword(sftpSettings.getProperty("user"), sftpSettings.getProperty("password"))
instance = ssh.newSFTPClient()
} catch (ex: UserAuthException) {
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