From 41e7279ec85626689cac8cb71466bc854394fb32 Mon Sep 17 00:00:00 2001 From: Pete Cornish Date: Sat, 6 Jul 2024 12:59:03 +0100 Subject: [PATCH] spike: binary configuration. --- .../gatehill/imposter/cmd/ImposterLauncher.kt | 2 + .../config/resource/AbstractResourceConfig.kt | 4 +- core/config/build.gradle | 2 + .../config/util/BinarySerialisationUtil.kt | 58 +++++++++++++++++++ .../imposter/config/util/ConfigUtil.kt | 9 ++- 5 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 core/config/src/main/java/io/gatehill/imposter/config/util/BinarySerialisationUtil.kt diff --git a/cmd/src/main/java/io/gatehill/imposter/cmd/ImposterLauncher.kt b/cmd/src/main/java/io/gatehill/imposter/cmd/ImposterLauncher.kt index 25e797a1e..0d71400c8 100644 --- a/cmd/src/main/java/io/gatehill/imposter/cmd/ImposterLauncher.kt +++ b/cmd/src/main/java/io/gatehill/imposter/cmd/ImposterLauncher.kt @@ -123,6 +123,8 @@ class ImposterLauncher(args: Array) { @JvmStatic fun main(args: Array) { + println("Ready") + readln() LogUtil.configureLoggingFromEnvironment() LogUtil.configureVertxLogging() ImposterLauncher(args) diff --git a/core/api/src/main/java/io/gatehill/imposter/plugin/config/resource/AbstractResourceConfig.kt b/core/api/src/main/java/io/gatehill/imposter/plugin/config/resource/AbstractResourceConfig.kt index f3bfd8a46..17b7543fd 100644 --- a/core/api/src/main/java/io/gatehill/imposter/plugin/config/resource/AbstractResourceConfig.kt +++ b/core/api/src/main/java/io/gatehill/imposter/plugin/config/resource/AbstractResourceConfig.kt @@ -48,7 +48,7 @@ import io.gatehill.imposter.plugin.config.capture.CaptureConfigHolder import io.gatehill.imposter.plugin.config.capture.ItemCaptureConfig import io.gatehill.imposter.plugin.config.security.SecurityConfig import io.gatehill.imposter.plugin.config.security.SecurityConfigHolder -import java.util.UUID +import java.util.* /** * Base configuration for plugins and sub-resources. @@ -75,7 +75,7 @@ abstract class AbstractResourceConfig : BasicResourceConfig, SecurityConfigHolde override val continueToNext: Boolean? = null @get:JsonIgnore - override val resourceId by lazy { UUID.randomUUID().toString() } + override val resourceId = UUID.randomUUID().toString() override fun toString(): String { return "AbstractResourceConfig(path=$path, securityConfig=$securityConfig, captureConfig=$captureConfig, responseConfig=$responseConfig, continueToNext=$continueToNext)" diff --git a/core/config/build.gradle b/core/config/build.gradle index 85ee43df8..795dd054c 100644 --- a/core/config/build.gradle +++ b/core/config/build.gradle @@ -14,6 +14,8 @@ 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" + // test testImplementation "junit:junit:$version_junit" testImplementation group: 'org.hamcrest', name: 'hamcrest', version: version_hamcrest diff --git a/core/config/src/main/java/io/gatehill/imposter/config/util/BinarySerialisationUtil.kt b/core/config/src/main/java/io/gatehill/imposter/config/util/BinarySerialisationUtil.kt new file mode 100644 index 000000000..928caae0f --- /dev/null +++ b/core/config/src/main/java/io/gatehill/imposter/config/util/BinarySerialisationUtil.kt @@ -0,0 +1,58 @@ +package io.gatehill.imposter.config.util + +import com.esotericsoftware.kryo.Kryo +import com.esotericsoftware.kryo.io.Input +import com.esotericsoftware.kryo.io.Output +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.io.File +import java.io.ObjectInputStream +import java.io.ObjectOutputStream + +object BinarySerialisationUtil { + val kryo = Kryo().apply { + isRegistrationRequired = false + references = true + } + + /** + * Serialises an object to a file using Kryo. + */ + fun serialise(obj: Any, filePath: String) { + File(filePath).outputStream().use { + val output = Output(it) + kryo.writeClassAndObject(output, obj) + output.close() + } + } + + /** + * Deserialises an object from a file using Kryo. + */ + fun deserialise(filePath: String): T { + return File(filePath).inputStream().use { + val input = Input(it) + val obj = kryo.readClassAndObject(input) as T + input.close() + obj + } + } + + fun serialiseJOS(obj: Any, filePath: String) { + ByteArrayOutputStream().use { byteArrayOutputStream -> + ObjectOutputStream(byteArrayOutputStream).use { objectOutputStream -> + objectOutputStream.writeObject(obj) + java.io.File(filePath).writeBytes(byteArrayOutputStream.toByteArray()) + } + } + } + + fun deserialiseJOS(filePath: String): T { + val bytes = java.io.File(filePath).readBytes() + return ByteArrayInputStream(bytes).use { byteArrayInputStream -> + ObjectInputStream(byteArrayInputStream).use { objectInputStream -> + objectInputStream.readObject() + } + } as T + } +} diff --git a/core/config/src/main/java/io/gatehill/imposter/config/util/ConfigUtil.kt b/core/config/src/main/java/io/gatehill/imposter/config/util/ConfigUtil.kt index b3f978eb4..5cf938de5 100644 --- a/core/config/src/main/java/io/gatehill/imposter/config/util/ConfigUtil.kt +++ b/core/config/src/main/java/io/gatehill/imposter/config/util/ConfigUtil.kt @@ -293,7 +293,14 @@ object ConfigUtil { ): T { val configFile = loadedConfig.ref.file try { - val config = lookupMapper(configFile).readValue(loadedConfig.serialised, configClass)!! + var config = lookupMapper(configFile).readValue(loadedConfig.serialised, configClass)!! + + BinarySerialisationUtil.serialise(config, "/tmp/config.bin") + println("serialised config") + + config = BinarySerialisationUtil.deserialise("/tmp/config.bin") + println("deserialised config") + check(config.plugin != null) { "No plugin specified in configuration file: $configFile" } config.dir = configFile.parentFile