Skip to content
New issue

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

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

Already on GitHub? # to your account

Unable to switch ClassLoader if default ClassLoader is insufficient #27

Open
ckosmowski opened this issue Oct 7, 2024 · 0 comments
Open

Comments

@ckosmowski
Copy link

I am trying to use Kotlin Scripting inside a Quarkus Application. However the Quarkus RuntimeClassloader seems to be insufficient to load the classes into the Scripting Engine. That is basically because it only loads classes that has been indexed at startup time.

I also filed a ticket with quarkus:

quarkusio/quarkus#43271 (comment)

And i created a minimal reproducer for the problem:

https://github.com/ckosmowski/kotlin-script-reproducer

1.) Start the Main class directly -> The script works. It outputs "Hello World!" and "A Model From Outside"
2.) run quarkus dev -> The output will be "Hello World" and a longish output with the message " ERROR Unresolved reference 'printName'. " at its end.

Something to add, there is a way that you can usually replace the used classloader like this:

val result = host.eval(StringScriptSource("""
            printName()
        """.trimIndent()), ScriptWithMavenDepsConfiguration, ScriptEvaluationConfiguration {
            implicitReceivers(modelFromOutside)
            jvm {
                baseClassLoader(FilteringClassLoader(Thread.currentThread().contextClassLoader, listOf("org.example", "java.lang")))
            }
        })

FilterClassLoader:

class FilteringClassLoader(
        parent: ClassLoader,
        val allowedPackages: List<String>
    ) : ClassLoader(parent) {

        override fun loadClass(name: String, resolve: Boolean): Class<*> {
            if (allowedPackages.any { name.startsWith(it) }) {
                return super.loadClass(name, resolve)
            }
            throw ClassNotFoundException("Class $name is not allowed")
        }
    }

I hoped that this does the trick but however this works in the plain java version but not with quarkus dev.
Is there a way that you kotlin people could assist here, or work together with quarkus?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant