Skip to content

Commit

Permalink
refactor: moves graal store proxy initialisation to engine lifecycle …
Browse files Browse the repository at this point in the history
…listener.
  • Loading branch information
outofcoffee committed Jul 17, 2024
1 parent 5dc7628 commit 43c14f2
Showing 1 changed file with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@
*/
package io.gatehill.imposter.scripting.graalvm.service

import io.gatehill.imposter.ImposterConfig
import io.gatehill.imposter.config.util.EnvVars
import io.gatehill.imposter.http.HttpRouter
import io.gatehill.imposter.lifecycle.EngineLifecycleListener
import io.gatehill.imposter.plugin.Plugin
import io.gatehill.imposter.plugin.PluginInfo
import io.gatehill.imposter.plugin.RequireModules
import io.gatehill.imposter.plugin.config.PluginConfig
import io.gatehill.imposter.script.ReadWriteResponseBehaviour
import io.gatehill.imposter.script.RuntimeContext
import io.gatehill.imposter.script.dsl.Dsl
Expand All @@ -72,7 +76,7 @@ import org.graalvm.polyglot.HostAccess
*/
@PluginInfo("js-graal")
@RequireModules(GraalvmScriptingModule::class)
class GraalvmScriptServiceImpl : ScriptService, Plugin {
class GraalvmScriptServiceImpl : ScriptService, Plugin, EngineLifecycleListener {
private val engine: Engine

override val requestBuilder: ScriptRequestBuilder
Expand All @@ -87,30 +91,29 @@ class GraalvmScriptServiceImpl : ScriptService, Plugin {
}

private val enableStoreProxy = EnvVars.getEnv(ENV_IMPOSTER_GRAAL_STORE_PROXY)?.toBoolean() != false
private var checkedStoreProxy = false

override fun initScript(script: ScriptSource) {
checkEnableStoreProxy()
}

override fun initEvalScript(scriptId: String, scriptCode: String) {
override fun afterRoutesConfigured(
imposterConfig: ImposterConfig,
allPluginConfigs: List<PluginConfig>,
router: HttpRouter
) {
checkEnableStoreProxy()
}

fun checkEnableStoreProxy() {
if (!checkedStoreProxy) {
checkedStoreProxy = true

if (enableStoreProxy) {
LOGGER.trace("Graal store proxy enabled")
val storeFactory = InjectorUtil.getInstance<StoreFactory>()
storeFactory.storeInterceptors += { ObjectProxyingStore(it) }
} else {
LOGGER.trace("Graal store proxy disabled")
}
if (enableStoreProxy) {
LOGGER.trace("Graal store proxy enabled")
val storeFactory = InjectorUtil.getInstance<StoreFactory>()
storeFactory.storeInterceptors += { ObjectProxyingStore(it) }
} else {
LOGGER.trace("Graal store proxy disabled")
}
}

override fun initEvalScript(scriptId: String, scriptCode: String) {
LOGGER.trace("No-op eval script init")
}

override fun executeScript(
script: ScriptSource,
runtimeContext: RuntimeContext,
Expand Down

0 comments on commit 43c14f2

Please sign in to comment.