Skip to content

Commit

Permalink
refactor: factors out config loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Jul 16, 2024
1 parent 7e6c31a commit d3cf591
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 181 deletions.
16 changes: 11 additions & 5 deletions core/api/src/main/java/io/gatehill/imposter/config/LoadedConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,14 @@ package io.gatehill.imposter.config
/**
* Holds a loaded configuration and its reference.
*/
data class LoadedConfig(
val ref: ConfigReference,
val serialised: String,
val plugin: String,
)
interface LoadedConfig {
val ref: ConfigReference
val serialised: String
val plugin: String
}

data class LoadedConfigImpl(
override val ref: ConfigReference,
override val serialised: String,
override val plugin: String,
) : LoadedConfig
5 changes: 0 additions & 5 deletions core/config/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ dependencies {
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$version_jackson"
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:$version_jackson"

implementation "com.esotericsoftware:kryo:5.6.0"

// implementation "de.ruedigermoeller:fst:2.56"
implementation "de.ruedigermoeller:fst:3.0.1"

// test
testImplementation "junit:junit:$version_junit"
testImplementation group: 'org.hamcrest', name: 'hamcrest', version: version_hamcrest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024.
*
* This file is part of Imposter.
*
* "Commons Clause" License Condition v1.0
*
* The Software is provided to you by the Licensor under the License, as
* defined below, subject to the following condition.
*
* Without limiting other conditions in the License, the grant of rights
* under the License will not include, and the License does not grant to
* you, the right to Sell the Software.
*
* For purposes of the foregoing, "Sell" means practicing any or all of
* the rights granted to you under the License to provide to third parties,
* for a fee or other consideration (including without limitation fees for
* hosting or consulting/support services related to the Software), a
* product or service whose value derives, entirely or substantially, from
* the functionality of the Software. Any license notice or attribution
* required by the License must also include this Commons Clause License
* Condition notice.
*
* Software: Imposter
*
* License: GNU Lesser General Public License version 3
*
* Licensor: Peter Cornish
*
* Imposter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Imposter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Imposter. If not, see <https://www.gnu.org/licenses/>.
*/

package io.gatehill.imposter.config.loader

import io.gatehill.imposter.config.ConfigReference
import io.gatehill.imposter.config.LoadedConfig
import io.gatehill.imposter.plugin.config.BasicPluginConfig
import java.io.File

/**
* Reads configuration files and deserialises them to plugin configuration instances.
*
* @author Pete Cornish
*/
interface ConfigLoader<C : LoadedConfig> {
fun readPluginConfig(configRef: ConfigReference): C
fun <T : BasicPluginConfig> loadConfig(configFile: File, loadedConfig: C, configClass: Class<T>): T
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2024.
*
* This file is part of Imposter.
*
* "Commons Clause" License Condition v1.0
*
* The Software is provided to you by the Licensor under the License, as
* defined below, subject to the following condition.
*
* Without limiting other conditions in the License, the grant of rights
* under the License will not include, and the License does not grant to
* you, the right to Sell the Software.
*
* For purposes of the foregoing, "Sell" means practicing any or all of
* the rights granted to you under the License to provide to third parties,
* for a fee or other consideration (including without limitation fees for
* hosting or consulting/support services related to the Software), a
* product or service whose value derives, entirely or substantially, from
* the functionality of the Software. Any license notice or attribution
* required by the License must also include this Commons Clause License
* Condition notice.
*
* Software: Imposter
*
* License: GNU Lesser General Public License version 3
*
* Licensor: Peter Cornish
*
* Imposter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Imposter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Imposter. If not, see <https://www.gnu.org/licenses/>.
*/

package io.gatehill.imposter.config.loader

import com.fasterxml.jackson.databind.JsonMappingException
import io.gatehill.imposter.config.ConfigReference
import io.gatehill.imposter.config.LoadedConfig
import io.gatehill.imposter.config.LoadedConfigImpl
import io.gatehill.imposter.config.expression.EnvEvaluator
import io.gatehill.imposter.config.expression.SystemEvaluator
import io.gatehill.imposter.config.model.LightweightConfig
import io.gatehill.imposter.config.util.ConfigUtil
import io.gatehill.imposter.config.util.EnvVars
import io.gatehill.imposter.expression.eval.ExpressionEvaluator
import io.gatehill.imposter.expression.util.ExpressionUtil
import io.gatehill.imposter.plugin.config.BasicPluginConfig
import java.io.File
import java.io.IOException

/**
* YAML (and JSON) implementation of a configuration loader.
*
* @author Pete Cornish
*/
class YamlConfigLoaderImpl : ConfigLoader<LoadedConfig> {
private var expressionEvaluators: Map<String, ExpressionEvaluator<*>> = emptyMap()

init {
initInterpolators(EnvVars.getEnv())
}

fun initInterpolators(environment: Map<String, String>) {
// reset the environment used by the expression evaluator
expressionEvaluators = mapOf(
"env" to EnvEvaluator(environment),
"system" to SystemEvaluator,
)
}

/**
* Reads the contents of the configuration file, performing necessary string substitutions.
*
* @param configRef the configuration reference
* @return the loaded configuration
*/
override fun readPluginConfig(configRef: ConfigReference): LoadedConfig {
val configFile = configRef.file
try {
val rawContents = configFile.readText()
val parsedContents = ExpressionUtil.eval(rawContents, expressionEvaluators, nullifyUnsupported = false)

val config = ConfigUtil.lookupMapper(configFile).readValue(parsedContents, LightweightConfig::class.java)
config.plugin
?: throw IllegalStateException("No plugin specified in configuration file: $configFile")

return LoadedConfigImpl(configRef, parsedContents, config.plugin)

} catch (e: JsonMappingException) {
throw RuntimeException("Error reading configuration file: " + configFile.absolutePath + ", reason: ${e.message}")
} catch (e: IOException) {
throw RuntimeException("Error reading configuration file: " + configFile.absolutePath, e)
}
}

override fun <T : BasicPluginConfig> loadConfig(configFile: File, loadedConfig: LoadedConfig, configClass: Class<T>): T =
ConfigUtil.lookupMapper(configFile).readValue(loadedConfig.serialised, configClass)!!
}

This file was deleted.

Loading

0 comments on commit d3cf591

Please sign in to comment.