Skip to content

Commit

Permalink
refactor: improves internal naming of eval scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Jul 16, 2024
1 parent fada096 commit 5dc7628
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ interface ScriptService {
// no op
}

fun initInlineScript(scriptId: String, scriptCode: String): Unit =
fun initEvalScript(scriptId: String, scriptCode: String): Unit =
throw NotImplementedError()

/**
Expand All @@ -69,9 +69,8 @@ interface ScriptService {
*/
fun executeScript(script: ScriptSource, runtimeContext: RuntimeContext): ReadWriteResponseBehaviour

fun evalInlineScript(scriptId: String, scriptCode: String, runtimeContext: RuntimeContext): Boolean =
fun executeEvalScript(scriptId: String, scriptCode: String, runtimeContext: RuntimeContext): Boolean =
throw NotImplementedError()

}

typealias ScriptRequestBuilder = (request: HttpRequest) -> ScriptRequest
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import io.gatehill.imposter.plugin.config.resource.conditional.MatchOperator
import io.gatehill.imposter.plugin.config.resource.request.BaseRequestBodyConfig
import io.gatehill.imposter.plugin.config.resource.request.RequestBodyResourceConfig
import io.gatehill.imposter.plugin.config.system.SystemConfigHolder
import io.gatehill.imposter.service.script.InlineScriptService
import io.gatehill.imposter.service.script.EvalScriptService
import io.gatehill.imposter.util.BodyQueryUtil
import io.gatehill.imposter.util.InjectorUtil
import io.gatehill.imposter.util.LogUtil
Expand All @@ -66,7 +66,7 @@ import java.util.regex.Pattern
* @author Pete Cornish
*/
abstract class AbstractResourceMatcher : ResourceMatcher {
private val inlineScriptService: InlineScriptService by lazy { InjectorUtil.getInstance() }
private val evalScriptService: EvalScriptService by lazy { InjectorUtil.getInstance() }

override fun matchAllResourceConfigs(
pluginConfig: PluginConfig,
Expand Down Expand Up @@ -319,7 +319,7 @@ abstract class AbstractResourceMatcher : ResourceMatcher {
httpExchange: HttpExchange,
pluginConfig: PluginConfig,
resource: ResolvedResourceConfig,
) = inlineScriptService.evalScript(httpExchange, pluginConfig, resource.config)
) = evalScriptService.evalScript(httpExchange, pluginConfig, resource.config)

fun determineMatch(
results: List<ResourceMatchResult>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import io.gatehill.imposter.service.StepService
import io.gatehill.imposter.service.UpstreamService
import io.gatehill.imposter.service.script.EmbeddedScriptService
import io.gatehill.imposter.service.script.EmbeddedScriptServiceImpl
import io.gatehill.imposter.service.script.InlineScriptService
import io.gatehill.imposter.service.script.EvalScriptService
import io.gatehill.imposter.service.script.ScriptServiceFactory
import io.gatehill.imposter.service.script.ScriptedResponseServiceImpl
import io.gatehill.imposter.service.security.CorsService
Expand All @@ -90,7 +90,7 @@ internal class EngineModule : AbstractModule() {
.asEagerSingleton()

bind(EmbeddedScriptService::class.java).to(EmbeddedScriptServiceImpl::class.java).asSingleton()
bind(InlineScriptService::class.java).asSingleton()
bind(EvalScriptService::class.java).asSingleton()

// needs to be eager to register lifecycle listener
bind(SecurityService::class.java).to(SecurityServiceImpl::class.java).asEagerSingleton()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import io.gatehill.imposter.util.InjectorUtil
import io.gatehill.imposter.util.LogUtil
import org.apache.logging.log4j.LogManager

class InlineScriptService {
class EvalScriptService {
private val jsScriptService: ScriptService by lazy {
InjectorUtil.getInstance<ScriptServiceFactory>().fetchScriptService("inline.js")
InjectorUtil.getInstance<ScriptServiceFactory>().fetchScriptService("eval.js")
}

fun initScript(config: EvalResourceConfig) {
if (config.eval.isNullOrBlank()) {
return
}
jsScriptService.initInlineScript(config.resourceId, config.eval!!)
jsScriptService.initEvalScript(config.resourceId, config.eval!!)
}

fun evalScript(
Expand All @@ -45,7 +45,7 @@ class InlineScriptService {
emptyMap(),
executionContext
)
val result = jsScriptService.evalInlineScript(scriptId, config.eval!!, runtimeContext)
val result = jsScriptService.executeEvalScript(scriptId, config.eval!!, runtimeContext)
if (logger.isTraceEnabled) {
logger.trace("Evaluation of inline script {} result: {}: {}", scriptId, result, config.eval)
} else {
Expand All @@ -71,6 +71,6 @@ class InlineScriptService {
}

companion object {
private val logger = LogManager.getLogger(InlineScriptService::class.java)
private val logger = LogManager.getLogger(EvalScriptService::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ScriptedResponseServiceImpl @Inject constructor(
engineLifecycle: EngineLifecycleHooks,
private val scriptLifecycle: ScriptLifecycleHooks,
private val scriptServiceFactory: ScriptServiceFactory,
private val inlineScriptService: InlineScriptService,
private val evalScriptService: EvalScriptService,
private val stepService: StepService,
) : ScriptedResponseService, EngineLifecycleListener {

Expand Down Expand Up @@ -126,7 +126,7 @@ class ScriptedResponseServiceImpl @Inject constructor(

// inline eval scripts
if (resource is EvalResourceConfig) {
inlineScriptService.initScript(resource)
evalScriptService.initScript(resource)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ class DelegatingJsScriptServiceImpl @Inject constructor(

override fun initScript(script: ScriptSource) = impl.initScript(script)

override fun initInlineScript(scriptId: String, scriptCode: String) = impl.initInlineScript(scriptId, scriptCode)
override fun initEvalScript(scriptId: String, scriptCode: String) = impl.initEvalScript(scriptId, scriptCode)

override fun executeScript(
script: ScriptSource,
runtimeContext: RuntimeContext
) = impl.executeScript(script, runtimeContext)

override fun evalInlineScript(
override fun executeEvalScript(
scriptId: String,
scriptCode: String,
runtimeContext: RuntimeContext
) = impl.evalInlineScript(scriptId, scriptCode, runtimeContext)
) = impl.executeEvalScript(scriptId, scriptCode, runtimeContext)
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class GraalvmScriptServiceImpl : ScriptService, Plugin {
checkEnableStoreProxy()
}

override fun initInlineScript(scriptId: String, scriptCode: String) {
override fun initEvalScript(scriptId: String, scriptCode: String) {
checkEnableStoreProxy()
}

Expand Down Expand Up @@ -137,12 +137,12 @@ class GraalvmScriptServiceImpl : ScriptService, Plugin {
throw RuntimeException("Script execution terminated abnormally", e)
}

override fun evalInlineScript(
override fun executeEvalScript(
scriptId: String,
scriptCode: String,
runtimeContext: RuntimeContext,
): Boolean {
LOGGER.trace("Executing inline script: {}", scriptId)
LOGGER.trace("Executing eval script: {}", scriptId)

try {
buildContext().use { context ->
Expand All @@ -160,7 +160,7 @@ class GraalvmScriptServiceImpl : ScriptService, Plugin {
return resultValue is Boolean && resultValue
}
} catch (e: Exception) {
throw RuntimeException("Inline script evaluation terminated abnormally", e)
throw RuntimeException("Eval script execution terminated abnormally", e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class NashornScriptServiceImpl : ScriptService, Plugin {
}
}

override fun initInlineScript(scriptId: String, scriptCode: String) {
override fun initEvalScript(scriptId: String, scriptCode: String) {
if (ScriptUtil.shouldPrecompile) {
LOGGER.debug("Precompiling inline script: $scriptId")
getCompiledInlineScript(scriptId, scriptCode)
Expand Down Expand Up @@ -145,12 +145,12 @@ class NashornScriptServiceImpl : ScriptService, Plugin {
}
}

override fun evalInlineScript(
override fun executeEvalScript(
scriptId: String,
scriptCode: String,
runtimeContext: RuntimeContext
): Boolean {
LOGGER.trace("Executing inline script: {}", scriptId)
LOGGER.trace("Executing eval script: {}", scriptId)

try {
val bindings = SimpleBindings(
Expand All @@ -166,7 +166,7 @@ class NashornScriptServiceImpl : ScriptService, Plugin {
return result is Boolean && result

} catch (e: Exception) {
throw RuntimeException("Inline script evaluation terminated abnormally", e)
throw RuntimeException("Eval script execution terminated abnormally", e)
}
}

Expand Down Expand Up @@ -200,10 +200,10 @@ class NashornScriptServiceImpl : ScriptService, Plugin {
): CompiledJsScript<CompiledScript> =
compiledScripts.get(scriptId) {
try {
LOGGER.trace("Compiling inline script: {}", scriptCode)
LOGGER.trace("Compiling eval script: {}", scriptCode)
return@get CompiledJsScript(0, scriptEngine.compile(scriptCode))
} catch (e: Exception) {
throw RuntimeException("Failed to compile inline script: $scriptCode", e)
throw RuntimeException("Failed to compile eval script: $scriptCode", e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ abstract class AbstractScriptServiceImplTest : AbstractBaseScriptTest() {
)
val runtimeContext = buildRuntimeContext(additionalBindings)
val script = ScriptSource(
source = "${UUID.randomUUID()}_inline.js",
source = "${UUID.randomUUID()}_eval.js",
code = scriptCode,
)
val actual = getService().executeScript(script, runtimeContext)
Expand Down

0 comments on commit 5dc7628

Please sign in to comment.