All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gitlab.arturbosch.detekt.test.KotlinScriptEnginePool.kt Maven / Gradle / Ivy

There is a newer version: 1.23.7
Show newest version
package io.gitlab.arturbosch.detekt.test

import org.jetbrains.kotlin.cli.common.environment.setIdeaIoUseFallback
import org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine
import javax.script.ScriptEngineManager

/**
 * The object to manage a pool of Kotlin script engines to distribute the load for compiling code.
 * The load for compiling code is distributed over a number of engines.
 */
object KotlinScriptEnginePool {

    private const val NUMBER_OF_ENGINES = 8

    private val engines: Array by lazy {
        Array(NUMBER_OF_ENGINES) { createEngine() }
    }
    private var id = 0

    fun getEngine(): KotlinJsr223JvmLocalScriptEngine {
        id++
        if (id == NUMBER_OF_ENGINES) {
            id = 0
        }
        return engines[id]
    }

    fun recoverEngine() {
        engines[id] = createEngine()
    }

    private fun createEngine(): KotlinJsr223JvmLocalScriptEngine {
        setIdeaIoUseFallback() // To avoid error on Windows

        val scriptEngineManager = ScriptEngineManager()
        val engine = scriptEngineManager.getEngineByExtension("kts") as? KotlinJsr223JvmLocalScriptEngine
        requireNotNull(engine) { "Kotlin script engine not supported" }
        return engine
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy