Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boot fat jar support #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.spring.demo.scriptengine

import org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineFactoryBase
import org.jetbrains.kotlin.cli.common.repl.ScriptArgsWithTypes
import org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngine
import org.jetbrains.kotlin.script.jsr223.KotlinStandardJsr223ScriptTemplate
import org.slf4j.LoggerFactory
import javax.script.Bindings
import javax.script.ScriptContext
import javax.script.ScriptEngine

class KotlinScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase() {

private val kotlinScriptEngine: ScriptEngine by lazy {
LoggerFactory.getLogger(javaClass).info("Creating a new script engine with classpath: ${springBootClassPath}")
KotlinJsr223JvmLocalScriptEngine(
this,
springBootClassPath,
KotlinStandardJsr223ScriptTemplate::class.qualifiedName!!,
{ ctx, types ->
ScriptArgsWithTypes(arrayOf(ctx.getBindings(ScriptContext.ENGINE_SCOPE)), types ?: emptyArray())
},
arrayOf(Bindings::class)
)
}

override fun getScriptEngine(): ScriptEngine {
return kotlinScriptEngine
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.spring.demo.scriptengine

import java.io.File
import java.io.FileInputStream
import java.nio.file.Files
import java.util.jar.JarInputStream
import kotlin.script.experimental.jvm.util.scriptCompilationClasspathFromContextOrStlib

val springBootFatJar: File? by lazy {
Thread.currentThread().contextClassLoader.getResource("BOOT-INF/classes")?.path?.substringBefore("!/")?.substringAfter("file:").toFile()
}

val springBootClassPath: List<File> by lazy {
if (springBootFatJar == null) {
scriptCompilationClasspathFromContextOrStlib("kotlin-script-util.jar", wholeClasspath = true)
} else {
val unpackDir = Files.createTempDirectory("boot-").toFile().apply {
Runtime.getRuntime().addShutdownHook(Thread {
deleteRecursively()
})
}
ArrayList<File>().apply {
JarInputStream(FileInputStream(springBootFatJar)).use { jarInputStream ->
add(File("$unpackDir/BOOT-INF/classes"))
do {
val entry = jarInputStream.nextJarEntry
if (entry != null) {
try {
if (!entry.isDirectory) {
val file = File("$unpackDir/${entry.name}")
if (entry.name.startsWith("BOOT-INF/lib")) {
add(file)
}
Files.createDirectories(file.parentFile.toPath())
file.outputStream().use { outputStream ->
jarInputStream.copyTo(outputStream)
outputStream.flush()
}
}
} finally {
jarInputStream.closeEntry()
}
}
} while (entry != null)
}
}
}
}

private fun String?.toFile(): File? {
return if (this == null) null else File(this)
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory
io.spring.demo.scriptengine.KotlinScriptEngineFactory