Skip to content

Commit

Permalink
spike: binary configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Jul 6, 2024
1 parent 8647d6a commit 41e7279
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class ImposterLauncher(args: Array<String>) {

@JvmStatic
fun main(args: Array<String>) {
println("Ready")
readln()
LogUtil.configureLoggingFromEnvironment()
LogUtil.configureVertxLogging()
ImposterLauncher(args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)"
Expand Down
2 changes: 2 additions & 0 deletions core/config/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <T> 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 <T> deserialiseJOS(filePath: String): T {
val bytes = java.io.File(filePath).readBytes()
return ByteArrayInputStream(bytes).use { byteArrayInputStream ->
ObjectInputStream(byteArrayInputStream).use { objectInputStream ->
objectInputStream.readObject()
}
} as T
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 41e7279

Please sign in to comment.