[groovyscripting] Prevent CNFE for scoped classes unavailable to the class loader (#17860)

Fixes the ClassNotFoundException when using Thing actions caused by #17383.
The GroovyClassLoader loads classes by name however the Thing actions classes cannot be loaded by name because they are internal classes.

Fixes #17683

Signed-off-by: Wouter Born <github@maindrain.net>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Wouter Born 2024-12-07 17:10:28 +01:00 committed by Ciprian Pascu
parent aee5cc9c25
commit d7ca3da98c

View File

@ -51,7 +51,15 @@ public class GroovyScriptEngineFactory extends AbstractScriptEngineFactory {
ImportCustomizer importCustomizer = new ImportCustomizer();
for (Map.Entry<String, Object> entry : scopeValues.entrySet()) {
if (entry.getValue() instanceof Class<?> clazz) {
importCustomizer.addImport(entry.getKey(), clazz.getCanonicalName());
String canonicalName = clazz.getCanonicalName();
try {
// Only add imports for classes that are available to the classloader
getClass().getClassLoader().loadClass(canonicalName);
importCustomizer.addImport(entry.getKey(), canonicalName);
logger.debug("Added import for {} as {}", entry.getKey(), canonicalName);
} catch (ClassNotFoundException e) {
logger.debug("Unable to add import for {} as {}", entry.getKey(), canonicalName, e);
}
} else {
scriptEngine.put(entry.getKey(), entry.getValue());
}