Skip to content

Commit 4e493e2

Browse files
committed
WIP (works now).
1 parent 3586d98 commit 4e493e2

File tree

4 files changed

+51
-45
lines changed

4 files changed

+51
-45
lines changed

application/src/main/kotlin/de/micromata/kotlinscripting/CustomClassLoader.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import java.util.*
77

88
private val log = KotlinLogging.logger {}
99

10+
/**
11+
* Custom class loader for testing purposes.
12+
*/
1013
class CustomClassLoader(parent: ClassLoader?) : ClassLoader("CustomClassLoader", parent) {
1114
override fun loadClass(name: String, resolve: Boolean): Class<*> {
1215
log.info { "loadClass(\"$name\", $resolve)" }

application/src/main/kotlin/de/micromata/kotlinscripting/MyScriptingHost.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
77

88
private val log = KotlinLogging.logger {}
99

10+
/**
11+
* If you want to pass a threadlocal variable, you can use this class.
12+
* Otherwise, BasicJvmScriptingHost() can be used directly.
13+
*/
1014
class CustomScriptingHost(
11-
private val customClassLoader: ClassLoader
15+
// private val customClassLoader: ClassLoader
1216
) : BasicJvmScriptingHost() {
1317

1418
override fun eval(
1519
script: SourceCode,
1620
compilationConfiguration: ScriptCompilationConfiguration,
1721
evaluationConfiguration: ScriptEvaluationConfiguration?
1822
): ResultWithDiagnostics<EvaluationResult> {
19-
val originalClassLoader = Thread.currentThread().contextClassLoader
23+
//val originalClassLoader = Thread.currentThread().contextClassLoader
2024
return try {
2125
ThreadLocalStorage.threadLocal.set(Constants.THREADLOCAL_TEST)
2226
// Trying to set the custom ClassLoader here.

application/src/main/kotlin/de/micromata/kotlinscripting/ScriptExecutor.kt

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,26 @@ class ScriptExecutor {
1717
private var evalException: Exception? = null
1818

1919
fun executeScript(): ResultWithDiagnostics<EvaluationResult>? {
20-
val classLoader = CustomClassLoader(Thread.currentThread().contextClassLoader)
20+
//val classLoader = CustomClassLoader(Thread.currentThread().contextClassLoader)
2121

22-
val scriptingHost = CustomScriptingHost(classLoader)
22+
val scriptingHost = CustomScriptingHost() // (classLoader)
2323
val compilationConfig = ScriptCompilationConfiguration {
2424
jvm {
25-
dependenciesFromClassloader(classLoader = classLoader, wholeClasspath = true)
26-
//dependencies(JvmDependency(dependencies))
27-
//dependenciesFromClassloader(classLoader = classLoader, wholeClasspath = true)
25+
// dependenciesFromClassloader(classLoader = classLoader, wholeClasspath = true)
26+
dependenciesFromClassloader(wholeClasspath = true)
2827
}
2928
providedProperties("context" to KotlinScriptContext::class)
3029
compilerOptions.append("-nowarn")
3130
}
3231
val context = KotlinScriptContext()
3332
context.setProperty("testVariable", Constants.TEST_VAR)
3433
val evaluationConfiguration = ScriptEvaluationConfiguration {
35-
jvm {
36-
baseClassLoader(classLoader)
37-
}
34+
/*jvm {
35+
baseClassLoader(classLoader) // Without effect. ClassLoader will be overwritten by the UrlClassLoader.
36+
}*/
3837
providedProperties("context" to context)
3938
}
40-
val scriptSource = script.toScriptSource()
39+
val scriptSource = Constants.CHECK_SCRIPT.toScriptSource()
4140
val executor = Executors.newSingleThreadExecutor()
4241
var future: Future<ResultWithDiagnostics<EvaluationResult>>? = null
4342
try {
@@ -59,36 +58,4 @@ class ScriptExecutor {
5958
}
6059
return null
6160
}
62-
63-
companion object {
64-
val simplestScript = "\"Hello world!\""
65-
val script = """
66-
import de.micromata.kotlinscripting.business.ThreadLocalStorage
67-
val sb = StringBuilder()
68-
sb.appendLine("Hello world!")
69-
val threadLocalVal = ThreadLocalStorage.threadLocal.get()
70-
if (threadLocalVal == "${Constants.THREADLOCAL_TEST}") {
71-
sb.appendLine("ThreadLocal: ${'$'}threadLocalVal (OK)")
72-
} else {
73-
sb.appendLine("ThreadLocal: ${'$'}threadLocalVal (*** ERROR, ${Constants.THREADLOCAL_TEST} expected)")
74-
}
75-
val testVar = context.getProperty("testVariable")
76-
if (testVar == "${Constants.TEST_VAR}") {
77-
sb.appendLine("Context: ${'$'}testVar (OK)")
78-
} else {
79-
sb.appendLine("Context: ${'$'}testVar (*** ERROR, ${Constants.TEST_VAR} expected)")
80-
}
81-
var loader = Thread.currentThread().contextClassLoader
82-
val classLoaders = mutableListOf<ClassLoader>()
83-
while (loader != null) {
84-
classLoaders.add(loader)
85-
loader = loader.parent
86-
}
87-
sb.append("ClassLoader: ")
88-
sb.appendLine("${'$'}{classLoaders.joinToString(", parent: ") { it.toString() }}")
89-
sb.appendLine("Classpath: ${'$'}{System.getProperty("java.class.path")}")
90-
//sb.appendLine(de.micromata.springbootkotlinscripting.ScriptExecutor.script)
91-
sb.toString()
92-
""".trimIndent()
93-
}
9461
}
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
package de.micromata.kotlinscripting
22

33
object Constants {
4-
val THREADLOCAL_TEST = "Hello script! Greetings from ThreadLocal."
5-
val TEST_VAR = "Hello script! Greetings from testVar."
4+
const val THREADLOCAL_TEST = "Hello script! Greetings from ThreadLocal."
5+
const val TEST_VAR = "Hello script! Greetings from testVar."
6+
7+
8+
const val HELLO_WORLD_SCRIPT = "\"Hello world!\""
9+
val CHECK_SCRIPT = """
10+
import de.micromata.kotlinscripting.business.ThreadLocalStorage
11+
val sb = StringBuilder()
12+
sb.appendLine("Hello world!")
13+
val threadLocalVal = ThreadLocalStorage.threadLocal.get()
14+
if (threadLocalVal == "${Constants.THREADLOCAL_TEST}") {
15+
sb.appendLine("ThreadLocal: ${'$'}threadLocalVal (OK)")
16+
} else {
17+
sb.appendLine("ThreadLocal: ${'$'}threadLocalVal (*** ERROR, ${Constants.THREADLOCAL_TEST} expected)")
18+
}
19+
val testVar = context.getProperty("testVariable")
20+
if (testVar == "${Constants.TEST_VAR}") {
21+
sb.appendLine("Context: ${'$'}testVar (OK)")
22+
} else {
23+
sb.appendLine("Context: ${'$'}testVar (*** ERROR, ${Constants.TEST_VAR} expected)")
24+
}
25+
var loader = Thread.currentThread().contextClassLoader
26+
val classLoaders = mutableListOf<ClassLoader>()
27+
while (loader != null) {
28+
classLoaders.add(loader)
29+
loader = loader.parent
30+
}
31+
sb.append("ClassLoader: ")
32+
sb.appendLine("${'$'}{classLoaders.joinToString(", parent: ") { it.toString() }}")
33+
sb.appendLine("Classpath: ${'$'}{System.getProperty("java.class.path")}")
34+
//sb.appendLine(de.micromata.springbootkotlinscripting.ScriptExecutor.script)
35+
sb.toString()
36+
""".trimIndent()
37+
638
}

0 commit comments

Comments
 (0)