This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch gs
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 9fc0fe75f2264b7bc3b6fc06092269f2c2134725
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Jul 14 14:45:37 2025 +0200

    CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for 
shared functions and DTOs
---
 .../groovy/DefaultGroovyScriptCompiler.java        | 26 ++++++++++++++++------
 .../language/groovy/GroovyScriptClassLoader.java   | 20 ++++++++++++-----
 2 files changed, 33 insertions(+), 13 deletions(-)

diff --git 
a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/DefaultGroovyScriptCompiler.java
 
b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/DefaultGroovyScriptCompiler.java
index 5dffbc130e8..64602f4a5b2 100644
--- 
a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/DefaultGroovyScriptCompiler.java
+++ 
b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/DefaultGroovyScriptCompiler.java
@@ -95,6 +95,20 @@ public class DefaultGroovyScriptCompiler extends 
ServiceSupport
         }
     }
 
+    @Override
+    protected void doBuild() throws Exception {
+        // register Groovy classloader to camel, so we are able to load 
classes we have compiled
+        CamelContext context = getCamelContext();
+        if (context != null) {
+            // use existing class loader if available
+            classLoader = (GroovyScriptClassLoader) 
context.getClassResolver().getClassLoader("GroovyScriptClassLoader");
+            if (classLoader == null) {
+                classLoader = new GroovyScriptClassLoader();
+                context.getClassResolver().addClassLoader(classLoader);
+            }
+        }
+    }
+
     @Override
     protected void doStart() throws Exception {
         super.doStart();
@@ -104,13 +118,6 @@ public class DefaultGroovyScriptCompiler extends 
ServiceSupport
 
             LOG.info("Pre-compiling Groovy sources from: {}", scriptPattern);
 
-            ClassLoader cl = camelContext.getApplicationContextClassLoader();
-            if (cl == null) {
-                cl = GroovyShell.class.getClassLoader();
-            }
-            classLoader = new GroovyScriptClassLoader(cl);
-            camelContext.getClassResolver().addClassLoader(classLoader);
-
             // make classloader available for groovy language
             
camelContext.getCamelContextExtension().addContextPlugin(GroovyScriptClassLoader.class,
 classLoader);
 
@@ -139,6 +146,11 @@ public class DefaultGroovyScriptCompiler extends 
ServiceSupport
             // setup compiler via groovy shell
             CompilerConfiguration cc = new CompilerConfiguration();
             cc.setClasspathList(cps);
+
+            ClassLoader cl = camelContext.getApplicationContextClassLoader();
+            if (cl == null) {
+                cl = GroovyShell.class.getClassLoader();
+            }
             GroovyShell shell = new GroovyShell(cl, cc);
 
             // parse code into classes and add to classloader
diff --git 
a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyScriptClassLoader.java
 
b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyScriptClassLoader.java
index 5b90a5722d8..97709f5042c 100644
--- 
a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyScriptClassLoader.java
+++ 
b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyScriptClassLoader.java
@@ -27,8 +27,8 @@ public class GroovyScriptClassLoader extends ClassLoader 
implements Closeable {
 
     private final Map<String, Class<?>> classes = new HashMap<>();
 
-    public GroovyScriptClassLoader(ClassLoader parent) {
-        super(parent);
+    public GroovyScriptClassLoader() {
+        super(GroovyScriptClassLoader.class.getClassLoader());
     }
 
     @Override
@@ -48,13 +48,21 @@ public class GroovyScriptClassLoader extends ClassLoader 
implements Closeable {
     }
 
     @Override
-    public Class<?> loadClass(String name) throws ClassNotFoundException {
-        return classes.get(name);
+    protected Class<?> findClass(String name) throws ClassNotFoundException {
+        Class<?> clazz = classes.get(name);
+        if (clazz != null) {
+            return clazz;
+        }
+        throw new ClassNotFoundException(name);
     }
 
     @Override
-    protected Class<?> findClass(String name) throws ClassNotFoundException {
-        return classes.get(name);
+    public Class<?> loadClass(String name) throws ClassNotFoundException {
+        Class<?> clazz = classes.get(name);
+        if (clazz != null) {
+            return clazz;
+        }
+        throw new ClassNotFoundException(name);
     }
 
     @Override

Reply via email to