You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2025/07/14 14:59:34 UTC
(camel) branch gs created (now 21b7fa00aea)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a change to branch gs
in repository https://gitbox.apache.org/repos/asf/camel.git
at 21b7fa00aea CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
This branch includes the following new commits:
new 376face1dfe CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
new 9df42b19ee5 CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
new b37dfbf0cab CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
new 7f060889f6c CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
new 0a05ed3c13c CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
new 80cc26517ab CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
new f53880207a3 CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
new 16de38e18bc CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
new 28d12d81583 CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
new a84fddf6b99 CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
new 9fc0fe75f22 CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
new 5d0d1e6d214 CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
new 21b7fa00aea CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
The 13 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(camel) 11/13: CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
Posted by da...@apache.org.
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 <cl...@gmail.com>
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
(camel) 13/13: CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
Posted by da...@apache.org.
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 21b7fa00aeadf8ac201612276c2d50b9b43048a7
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 14 16:59:08 2025 +0200
CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
---
components/camel-groovy/src/main/docs/groovy-language.adoc | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/components/camel-groovy/src/main/docs/groovy-language.adoc b/components/camel-groovy/src/main/docs/groovy-language.adoc
index 8077a2bdcc8..67d9170aa3e 100644
--- a/components/camel-groovy/src/main/docs/groovy-language.adoc
+++ b/components/camel-groovy/src/main/docs/groovy-language.adoc
@@ -173,14 +173,15 @@ In *Camel 4.14* we have added support for loading groovy source files and pre-co
on startup. This allows to have a common set of groovy classes and functions which can be
used by Camel and Java.
-To enable this you need to configure this in `camel-main` via
+By default, scripts can be placed in `src/main/resources/camel-groovy` folder, but
+can be fully configured via ANT path style such as:
[source,properties]
----
camel.main.groovyScriptPattern = myscript/*.groovy
----
-Then in the `src/main/resources/myscript` folder you can have groovy source files that Camel
+Then in the `src/main/resources/camel-groovy` folder you can have groovy source files that Camel
will pre-compile on startup, and make global available via a special `GroovyScriptClassLoader`.
Because this class-loader is required to be in use for being able to load the groovy pre-compiled
(camel) 12/13: CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
Posted by da...@apache.org.
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 5d0d1e6d214f234dc3e256daf222a1c4b24dfdd3
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 14 16:52:43 2025 +0200
CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
---
.../language/groovy/DefaultGroovyScriptCompiler.java | 13 ++++++++++++-
.../camel/processor/groovy/DefaultGroovyCompilerTest.java | 2 +-
.../camel/processor/groovy/GroovyCompilerRouteTest.java | 4 +++-
.../processor/groovy/MainGroovyCompilerRouteTest.java | 4 +---
.../resources/{myscript => camel-groovy}/Cheese.groovy | 0
.../test/resources/{myscript => camel-groovy}/Dude.groovy | 0
.../org/apache/camel/impl/engine/SimpleCamelContext.java | 7 ++-----
.../META-INF/camel-main-configuration-metadata.json | 2 +-
core/camel-main/src/main/docs/main.adoc | 2 +-
.../apache/camel/main/DefaultConfigurationConfigurer.java | 10 ++++++----
.../apache/camel/main/DefaultConfigurationProperties.java | 6 +++++-
.../camel/dsl/jbang/core/commands/ExportBaseCommand.java | 4 ++++
.../org/apache/camel/dsl/jbang/core/commands/Run.java | 15 +++++++++++++++
.../src/main/java/org/apache/camel/main/KameletMain.java | 6 ++++++
14 files changed, 57 insertions(+), 18 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 64602f4a5b2..eea750eb45b 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
@@ -31,8 +31,10 @@ import org.apache.camel.spi.Resource;
import org.apache.camel.spi.annotations.JdkService;
import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.FileUtil;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StopWatch;
+import org.apache.camel.util.StringHelper;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -135,8 +137,17 @@ public class DefaultGroovyScriptCompiler extends ServiceSupport
loc = resource.getLocation();
}
if (loc != null) {
- cps.add(loc);
String code = IOHelper.loadText(resource.getInputStream());
+ loc = StringHelper.after(loc, ":", loc);
+
+ String cp = FileUtil.onlyPath(loc);
+ if (cp == null) {
+ cp = ".";
+ }
+ if (!cps.contains(cp)) {
+ cps.add(cp);
+ }
+
codes.add(code);
}
}
diff --git a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/DefaultGroovyCompilerTest.java b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/DefaultGroovyCompilerTest.java
index 31350946601..62057ad0dcb 100644
--- a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/DefaultGroovyCompilerTest.java
+++ b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/DefaultGroovyCompilerTest.java
@@ -30,7 +30,7 @@ public class DefaultGroovyCompilerTest extends CamelTestSupport {
public void testCompiler() throws Exception {
DefaultGroovyScriptCompiler compiler = new DefaultGroovyScriptCompiler();
compiler.setCamelContext(context);
- compiler.setScriptPattern("myscript/*.groovy");
+ compiler.setScriptPattern("camel-groovy/*");
compiler.start();
Class<?> clazz = compiler.loadClass("Dude");
diff --git a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java
index f29a827801a..62c4cc512b4 100644
--- a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java
+++ b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java
@@ -32,7 +32,9 @@ public class GroovyCompilerRouteTest extends CamelTestSupport {
DefaultGroovyScriptCompiler compiler = new DefaultGroovyScriptCompiler();
compiler.setCamelContext(context);
- compiler.setScriptPattern("file:src/test/resources/myscript/*.groovy");
+ // test with file instead of default
+ // compiler.setScriptPattern("classpath:camel-groovy/*");
+ compiler.setScriptPattern("file:src/test/resources/camel-groovy/*");
context.addService(compiler);
return context;
diff --git a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/MainGroovyCompilerRouteTest.java b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/MainGroovyCompilerRouteTest.java
index d9319504930..19126fa6614 100644
--- a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/MainGroovyCompilerRouteTest.java
+++ b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/MainGroovyCompilerRouteTest.java
@@ -31,7 +31,6 @@ public class MainGroovyCompilerRouteTest {
public void testCompilerRoute() throws Exception {
Main main = new Main();
main.configure().addRoutesBuilder(createRouteBuilder());
- main.configure().withGroovyScriptPattern("myscript/*.groovy");
main.start();
CamelContext context = main.getCamelContext();
@@ -45,12 +44,11 @@ public class MainGroovyCompilerRouteTest {
DefaultGroovyScriptCompiler compiler = context.hasService(DefaultGroovyScriptCompiler.class);
Assertions.assertNotNull(compiler);
- Assertions.assertEquals("myscript/*.groovy", compiler.getScriptPattern());
+ Assertions.assertEquals("classpath:camel-groovy/*", compiler.getScriptPattern());
Assertions.assertEquals(2, compiler.getClassesSize());
Assertions.assertTrue(compiler.getCompileTime() > 0, "Should take time to compile, was: " + compiler.getCompileTime());
main.stop();
-
}
protected RoutesBuilder createRouteBuilder() throws Exception {
diff --git a/components/camel-groovy/src/test/resources/myscript/Cheese.groovy b/components/camel-groovy/src/test/resources/camel-groovy/Cheese.groovy
similarity index 100%
rename from components/camel-groovy/src/test/resources/myscript/Cheese.groovy
rename to components/camel-groovy/src/test/resources/camel-groovy/Cheese.groovy
diff --git a/components/camel-groovy/src/test/resources/myscript/Dude.groovy b/components/camel-groovy/src/test/resources/camel-groovy/Dude.groovy
similarity index 100%
rename from components/camel-groovy/src/test/resources/myscript/Dude.groovy
rename to components/camel-groovy/src/test/resources/camel-groovy/Dude.groovy
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
index 45fc96b0158..941ce38253e 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
@@ -481,11 +481,8 @@ public class SimpleCamelContext extends AbstractCamelContext {
getCamelContextExtension().getBootstrapFactoryFinder(),
GroovyScriptCompiler.FACTORY,
GroovyScriptCompiler.class);
- if (result.isPresent()) {
- return result.get();
- } else {
- throw new IllegalArgumentException("Cannot find GroovyScriptCompiler on classpath. Add camel-groovy to classpath.");
- }
+ // camel-groovy is optional
+ return result.orElse(null);
}
private CliConnectorFactory createCliConnectorFactory() {
diff --git a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
index c857def438b..6d6f1b846e3 100644
--- a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
+++ b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
@@ -72,7 +72,7 @@
{ "name": "camel.main.extraShutdownTimeout", "description": "Extra timeout in seconds to graceful shutdown Camel. When Camel is shutting down then Camel first shutdown all the routes (shutdownTimeout). Then additional services is shutdown (extraShutdownTimeout).", "sourceType": "org.apache.camel.main.MainConfigurationProperties", "type": "integer", "javaType": "int", "defaultValue": 15 },
{ "name": "camel.main.fileConfigurations", "description": "Directory to load additional configuration files that contains configuration values that takes precedence over any other configuration. This can be used to refer to files that may have secret configuration that has been mounted on the file system for containers. You can specify a pattern to load from sub directories and a name pattern such as \/var\/app\/secret\/.properties, multiple directories can be separated by comma.", " [...]
{ "name": "camel.main.globalOptions", "description": "Sets global options that can be referenced in the camel context Important: This has nothing to do with property placeholders, and is just a plain set of key\/value pairs which are used to configure global options on CamelContext, such as a maximum debug logging length etc.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "object", "javaType": "java.util.Map" },
- { "name": "camel.main.groovyScriptPattern", "description": "Directories to scan for groovy source to be pre-compiled. For example: scripts\/.groovy will scan inside the classpath folder scripts for all groovy source files. By default, sources are scanned from the classpath, but you can prefix with file: to use file system. The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma.", "sourceType": "org.apache.camel.main.DefaultConfi [...]
+ { "name": "camel.main.groovyScriptPattern", "description": "Directories to scan for groovy source to be pre-compiled. For example: scripts\/.groovy will scan inside the classpath folder scripts for all groovy source files. By default, sources are scanned from the classpath, but you can prefix with file: to use file system. The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma. This requires having camel-groovy JAR on the classp [...]
{ "name": "camel.main.inflightRepositoryBrowseEnabled", "description": "Sets whether the inflight repository should allow browsing each inflight exchange. This is by default disabled as there is a very slight performance overhead when enabled.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean", "defaultValue": "false" },
{ "name": "camel.main.javaRoutesExcludePattern", "description": "Used for exclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. For example to exclude all classes starting with Bar use: **\/Bar* To exclude all routes form a specific package use: com\/mycomp [...]
{ "name": "camel.main.javaRoutesIncludePattern", "description": "Used for inclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. Multiple patterns can be specified separated by comma. For example to include all classes starting with Foo use: **\/Foo To include a [...]
diff --git a/core/camel-main/src/main/docs/main.adoc b/core/camel-main/src/main/docs/main.adoc
index dcbbafc5283..5853f280995 100644
--- a/core/camel-main/src/main/docs/main.adoc
+++ b/core/camel-main/src/main/docs/main.adoc
@@ -69,7 +69,7 @@ The camel.main supports 123 options, which are listed below.
| *camel.main.extraShutdown{zwsp}Timeout* | Extra timeout in seconds to graceful shutdown Camel. When Camel is shutting down then Camel first shutdown all the routes (shutdownTimeout). Then additional services is shutdown (extraShutdownTimeout). | 15 | int
| *camel.main.fileConfigurations* | Directory to load additional configuration files that contains configuration values that takes precedence over any other configuration. This can be used to refer to files that may have secret configuration that has been mounted on the file system for containers. You can specify a pattern to load from sub directories and a name pattern such as /var/app/secret/.properties, multiple directories can be separated by comma. | | String
| *camel.main.globalOptions* | Sets global options that can be referenced in the camel context Important: This has nothing to do with property placeholders, and is just a plain set of key/value pairs which are used to configure global options on CamelContext, such as a maximum debug logging length etc. | | Map
-| *camel.main.groovyScriptPattern* | Directories to scan for groovy source to be pre-compiled. For example: scripts/.groovy will scan inside the classpath folder scripts for all groovy source files. By default, sources are scanned from the classpath, but you can prefix with file: to use file system. The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma. | | String
+| *camel.main.groovyScriptPattern* | Directories to scan for groovy source to be pre-compiled. For example: scripts/.groovy will scan inside the classpath folder scripts for all groovy source files. By default, sources are scanned from the classpath, but you can prefix with file: to use file system. The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma. This requires having camel-groovy JAR on the classpath. | classpath:camel-groov [...]
| *camel.main.inflightRepository{zwsp}BrowseEnabled* | Sets whether the inflight repository should allow browsing each inflight exchange. This is by default disabled as there is a very slight performance overhead when enabled. | false | boolean
| *camel.main.javaRoutesExclude{zwsp}Pattern* | Used for exclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. For example to exclude all classes starting with Bar use: **/Bar* To exclude all routes form a specific package use: com/mycompany/bar/* To exclud [...]
| *camel.main.javaRoutesInclude{zwsp}Pattern* | Used for inclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. Multiple patterns can be specified separated by comma. For example to include all classes starting with Foo use: **/Foo To include all routes form a speci [...]
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
index 01c95520170..7ee6a68df70 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
@@ -354,10 +354,12 @@ public final class DefaultConfigurationConfigurer {
}
if (config.getGroovyScriptPattern() != null) {
GroovyScriptCompiler gsc = camelContext.getCamelContextExtension().getContextPlugin(GroovyScriptCompiler.class);
- gsc.setScriptPattern(config.getGroovyScriptPattern());
- camelContext.addService(gsc);
- // force start compiler eager so Camel routes can load these pre-compiled classes
- ServiceHelper.startService(gsc);
+ if (gsc != null) {
+ gsc.setScriptPattern(config.getGroovyScriptPattern());
+ camelContext.addService(gsc);
+ // force start compiler eager so Camel routes can load these pre-compiled classes
+ ServiceHelper.startService(gsc);
+ }
}
if (config.getRouteFilterIncludePattern() != null || config.getRouteFilterExcludePattern() != null) {
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
index f09fca6bb2c..288fb9deafb 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
@@ -129,7 +129,7 @@ public abstract class DefaultConfigurationProperties<T> {
@Metadata(defaultValue = "true")
private boolean routesReloadRemoveAllRoutes = true;
private boolean routesReloadRestartDuration;
- private String groovyScriptPattern;
+ private String groovyScriptPattern = "classpath:camel-groovy/*";
@Metadata(defaultValue = "default", enums = "default,prototype,pooled")
private String exchangeFactory = "default";
private int exchangeFactoryCapacity = 100;
@@ -1406,6 +1406,8 @@ public abstract class DefaultConfigurationProperties<T> {
* By default, sources are scanned from the classpath, but you can prefix with file: to use file system.
*
* The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma.
+ *
+ * This requires having camel-groovy JAR on the classpath.
*/
public void setGroovyScriptPattern(String groovyScriptPattern) {
this.groovyScriptPattern = groovyScriptPattern;
@@ -2792,6 +2794,8 @@ public abstract class DefaultConfigurationProperties<T> {
* By default, sources are scanned from the classpath, but you can prefix with file: to use file system.
*
* The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma.
+ *
+ * This requires having camel-groovy JAR on the classpath.
*/
public T withGroovyScriptPattern(String groovyScriptPattern) {
this.groovyScriptPattern = groovyScriptPattern;
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
index 955ca465adb..e28970ce739 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
@@ -76,6 +76,7 @@ public abstract class ExportBaseCommand extends CamelCommand {
"camel.component.kamelet.location",
"camel.jbang.classpathFiles",
"camel.jbang.localKameletDir",
+ "camel.jbang.groovyFiles",
"camel.jbang.scriptFiles",
"camel.jbang.tlsFiles",
"camel.jbang.jkubeFiles",
@@ -616,6 +617,7 @@ public abstract class ExportBaseCommand extends CamelCommand {
boolean camel = !kamelet && "camel.main.routesIncludePattern".equals(k);
boolean jkube = "camel.jbang.jkubeFiles".equals(k);
boolean script = "camel.jbang.scriptFiles".equals(k);
+ boolean groovy = "camel.jbang.groovyFiles".equals(k);
boolean tls = "camel.jbang.tlsFiles".equals(k);
boolean web = ext != null && List.of("css", "html", "ico", "jpeg", "jpg", "js", "png").contains(ext);
Path targetDir;
@@ -627,6 +629,8 @@ public abstract class ExportBaseCommand extends CamelCommand {
targetDir = srcKameletsResourcesDir;
} else if (script) {
targetDir = srcJavaDirRoot.getParent().resolve("scripts");
+ } else if (groovy) {
+ targetDir = srcResourcesDir.resolve("camel-groovy");
} else if (tls) {
targetDir = srcJavaDirRoot.getParent().resolve("tls");
} else if (web) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 38bcdba303f..4e15ab82eb7 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -716,6 +716,7 @@ public class Run extends CamelCommand {
StringJoiner sjReload = new StringJoiner(",");
StringJoiner sjClasspathFiles = new StringJoiner(",");
StringJoiner sjScriptFiles = new StringJoiner(",");
+ StringJoiner sjGroovyFiles = new StringJoiner(",");
StringJoiner sjTlsFiles = new StringJoiner(",");
StringJoiner sjKamelets = new StringJoiner(",");
StringJoiner sjJKubeFiles = new StringJoiner(",");
@@ -755,6 +756,10 @@ public class Run extends CamelCommand {
// script files
sjScriptFiles.add(file);
continue;
+ } else if (isGroovyFile(file)) {
+ // groovy files
+ sjGroovyFiles.add("file:" + file);
+ continue;
} else if (isTlsFile(file)) {
// tls files
sjTlsFiles.add(file);
@@ -869,6 +874,12 @@ public class Run extends CamelCommand {
} else {
writeSetting(main, profileProperties, "camel.jbang.scriptFiles", () -> null);
}
+ if (sjGroovyFiles.length() > 0) {
+ main.addInitialProperty("camel.jbang.groovyFiles", sjGroovyFiles.toString());
+ writeSettings("camel.jbang.groovyFiles", sjGroovyFiles.toString());
+ } else {
+ writeSetting(main, profileProperties, "camel.jbang.groovyFiles", () -> null);
+ }
if (sjTlsFiles.length() > 0) {
main.addInitialProperty("camel.jbang.tlsFiles", sjTlsFiles.toString());
writeSettings("camel.jbang.tlsFiles", sjTlsFiles.toString());
@@ -1946,6 +1957,10 @@ public class Run extends CamelCommand {
return false;
}
+ private boolean isGroovyFile(String name) {
+ return name.endsWith(".groovy");
+ }
+
private boolean isScriptFile(String name) {
return name.endsWith(".sh");
}
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
index 272f554893d..cfc5b7225bb 100644
--- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
@@ -440,6 +440,12 @@ public class KameletMain extends MainCommandLineSupport {
pc.setPropertiesFunctionResolver(new DependencyDownloaderPropertiesFunctionResolver(answer, false));
}
+ // groovy scripts
+ String groovyFiles = getInitialProperties().getProperty(getInstanceType() + ".groovyFiles");
+ if (groovyFiles != null) {
+ configure().withGroovyScriptPattern(groovyFiles);
+ }
+
boolean prompt = "true".equals(getInitialProperties().get(getInstanceType() + ".prompt"));
if (prompt) {
answer.getPropertiesComponent().addPropertiesSource(new PromptPropertyPlaceholderSource());
(camel) 06/13: CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
Posted by da...@apache.org.
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 80cc26517abe1f8a3b991cebae6d0278af684d12
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 14 13:19:52 2025 +0200
CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
---
.../camel/language/groovy/DefaultGroovyScriptCompiler.java | 12 ++++++++----
1 file changed, 8 insertions(+), 4 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 23198c28a0f..51d09ad2aee 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
@@ -59,17 +59,17 @@ public class DefaultGroovyScriptCompiler extends ServiceSupport
this.camelContext = camelContext;
}
- @ManagedAttribute(description = "Total compilation time in millis")
+ @ManagedAttribute(description = "Total Groovy compilation time in millis")
public long getCompileTime() {
return elapsed;
}
- @ManagedAttribute(description = "Number of groovy sources that has been compiled")
+ @ManagedAttribute(description = "Number of Groovy sources that has been compiled")
public int getClassesSize() {
return classLoader.size();
}
- @ManagedAttribute(description = "Directories to scan for groovy source to be pre-compiled")
+ @ManagedAttribute(description = "Directories to scan for Groovy source to be pre-compiled")
@Override
public String getScriptPattern() {
return scriptPattern;
@@ -102,7 +102,7 @@ public class DefaultGroovyScriptCompiler extends ServiceSupport
if (scriptPattern != null) {
StopWatch watch = new StopWatch();
- LOG.info("Pre compiling groovy scripts from: {}", scriptPattern);
+ LOG.info("Pre-compiling Groovy source from: {}", scriptPattern);
ClassLoader cl = camelContext.getApplicationContextClassLoader();
if (cl == null) {
@@ -143,8 +143,12 @@ public class DefaultGroovyScriptCompiler extends ServiceSupport
// parse code into classes and add to classloader
for (String code : codes) {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Pre-compiling Groovy source:\n{}", code);
+ }
Class<?> clazz = shell.getClassLoader().parseClass(code);
if (clazz != null) {
+ LOG.debug("Pre-compiled Groovy class: {}", clazz.getName());
classLoader.addClass(clazz.getName(), clazz);
}
}
(camel) 07/13: CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
Posted by da...@apache.org.
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 f53880207a3fdc70a432c1f15b58ecf7fe2c4d17
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 14 13:56:34 2025 +0200
CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
---
.../apache/camel/main/DefaultConfigurationConfigurer.java | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
index 035ea76210d..01c95520170 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
@@ -97,6 +97,7 @@ import org.apache.camel.support.RouteWatcherReloadStrategy;
import org.apache.camel.support.ShortUuidGenerator;
import org.apache.camel.support.SimpleUuidGenerator;
import org.apache.camel.support.jsse.GlobalSSLContextParametersSupplier;
+import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.startup.BacklogStartupStepRecorder;
import org.apache.camel.support.startup.LoggingStartupStepRecorder;
import org.apache.camel.util.ObjectHelper;
@@ -288,12 +289,6 @@ public final class DefaultConfigurationConfigurer {
ContextReloadStrategy reloader = new DefaultContextReloadStrategy();
camelContext.addService(reloader);
}
- if (config.getGroovyScriptPattern() != null) {
- GroovyScriptCompiler gsc = camelContext.getCamelContextExtension().getContextPlugin(GroovyScriptCompiler.class);
- gsc.setScriptPattern(config.getGroovyScriptPattern());
- // force start compiler eager so Camel routes can load these pre-compiled classes
- camelContext.addService(gsc, true, true);
- }
if (camelContext.getManagementStrategy().getManagementAgent() != null) {
camelContext.getManagementStrategy().getManagementAgent()
@@ -357,6 +352,13 @@ public final class DefaultConfigurationConfigurer {
}
cs.setWorkDir(config.getCompileWorkDir());
}
+ if (config.getGroovyScriptPattern() != null) {
+ GroovyScriptCompiler gsc = camelContext.getCamelContextExtension().getContextPlugin(GroovyScriptCompiler.class);
+ gsc.setScriptPattern(config.getGroovyScriptPattern());
+ camelContext.addService(gsc);
+ // force start compiler eager so Camel routes can load these pre-compiled classes
+ ServiceHelper.startService(gsc);
+ }
if (config.getRouteFilterIncludePattern() != null || config.getRouteFilterExcludePattern() != null) {
camelContext.getCamelContextExtension().getContextPlugin(Model.class).setRouteFilterPattern(
(camel) 09/13: CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
Posted by da...@apache.org.
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 28d12d815831641a946b19a30e6aadaefd8c5fa4
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 14 14:41:04 2025 +0200
CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
---
.../camel/language/groovy/DefaultGroovyScriptCompiler.java | 13 +++++++++----
1 file changed, 9 insertions(+), 4 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 51d09ad2aee..5dffbc130e8 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
@@ -44,7 +44,7 @@ public class DefaultGroovyScriptCompiler extends ServiceSupport
private static final Logger LOG = LoggerFactory.getLogger(DefaultGroovyScriptCompiler.class);
- private long elapsed;
+ private long taken;
private GroovyScriptClassLoader classLoader;
private CamelContext camelContext;
private String scriptPattern;
@@ -61,7 +61,7 @@ public class DefaultGroovyScriptCompiler extends ServiceSupport
@ManagedAttribute(description = "Total Groovy compilation time in millis")
public long getCompileTime() {
- return elapsed;
+ return taken;
}
@ManagedAttribute(description = "Number of Groovy sources that has been compiled")
@@ -102,7 +102,7 @@ public class DefaultGroovyScriptCompiler extends ServiceSupport
if (scriptPattern != null) {
StopWatch watch = new StopWatch();
- LOG.info("Pre-compiling Groovy source from: {}", scriptPattern);
+ LOG.info("Pre-compiling Groovy sources from: {}", scriptPattern);
ClassLoader cl = camelContext.getApplicationContextClassLoader();
if (cl == null) {
@@ -152,13 +152,18 @@ public class DefaultGroovyScriptCompiler extends ServiceSupport
classLoader.addClass(clazz.getName(), clazz);
}
}
- elapsed = watch.taken();
+ taken += watch.taken();
}
}
@Override
protected void doStop() throws Exception {
super.doStop();
+
+ if (classLoader.size() > 0) {
+ LOG.info("Pre-compiled {} Groovy sources in {} millis", classLoader.size(), taken);
+ }
+
IOHelper.close(classLoader);
}
}
(camel) 08/13: CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
Posted by da...@apache.org.
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 16de38e18bcc6a12447c3660a395d5a5ebc88353
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 14 14:37:40 2025 +0200
CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
---
.../apache/camel/language/groovy/GroovyScriptClassLoader.java | 11 ++++++-----
.../main/resources/camel-main-known-dependencies.properties | 1 +
2 files changed, 7 insertions(+), 5 deletions(-)
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 094bbe0b215..f1149c526bb 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
@@ -42,13 +42,14 @@ public class GroovyScriptClassLoader extends ClassLoader implements Closeable {
return classes.size();
}
+ @Override
+ public Class<?> loadClass(String name) throws ClassNotFoundException {
+ return classes.get(name);
+ }
+
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
- Class<?> answer = classes.get(name);
- if (answer == null) {
- answer = super.findClass(name);
- }
- return answer;
+ return classes.get(name);
}
@Override
diff --git a/dsl/camel-kamelet-main/src/main/resources/camel-main-known-dependencies.properties b/dsl/camel-kamelet-main/src/main/resources/camel-main-known-dependencies.properties
index c6d588f5fc5..57fd5bdc773 100644
--- a/dsl/camel-kamelet-main/src/main/resources/camel-main-known-dependencies.properties
+++ b/dsl/camel-kamelet-main/src/main/resources/camel-main-known-dependencies.properties
@@ -35,6 +35,7 @@ META-INF/services/org/apache/camel/micrometer-prometheus = camel:micrometer-prom
META-INF/services/org/apache/camel/cron/cron-service = camel:quartz
META-INF/services/org/apache/camel/platform-http/jolokia = camel:camel-platform-http-jolokia
META-INF/services/org/apache/camel/jandex-class-resolver = camel:jandex
+META-INF/services/org/apache/camel/groovy-script-compiler = camel:groovy
org.apache.camel.component.activemq.ActiveMQComponent\:embedded\=true = org.apache.activemq:activemq-broker:5.19.0
org.apache.camel.component.activemq6.ActiveMQComponent\:embedded\=true = org.apache.activemq:activemq-broker:6.1.7
spring.datasource.url = org.springframework.boot:spring-boot-starter-jdbc:${spring-boot-version}
(camel) 02/13: CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
Posted by da...@apache.org.
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 9df42b19ee5fbab3d54c14843ef48b4bacc1db7c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 14 10:14:02 2025 +0200
CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
---
.../org/apache/camel/groovy-script-compiler | 2 +
...piler.java => DefaultGroovyScriptCompiler.java} | 44 ++++++++++++++++------
...lerTest.java => DefaultGroovyCompilerTest.java} | 8 ++--
.../processor/groovy/GroovyCompilerRouteTest.java | 6 +--
.../org/apache/camel/spi/GroovyScriptCompiler.java | 40 ++++++++++++++++++++
.../camel/impl/engine/AbstractCamelContext.java | 4 ++
.../camel/impl/engine/SimpleCamelContext.java | 15 ++++++++
.../MainConfigurationPropertiesConfigurer.java | 7 ++++
.../camel-main-configuration-metadata.json | 1 +
core/camel-main/src/main/docs/main.adoc | 3 +-
.../camel/main/DefaultConfigurationConfigurer.java | 6 +++
.../camel/main/DefaultConfigurationProperties.java | 38 +++++++++++++++----
12 files changed, 147 insertions(+), 27 deletions(-)
diff --git a/components/camel-groovy/src/generated/resources/META-INF/services/org/apache/camel/groovy-script-compiler b/components/camel-groovy/src/generated/resources/META-INF/services/org/apache/camel/groovy-script-compiler
new file mode 100644
index 00000000000..a6b3fe772ca
--- /dev/null
+++ b/components/camel-groovy/src/generated/resources/META-INF/services/org/apache/camel/groovy-script-compiler
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.language.groovy.DefaultGroovyScriptCompiler
diff --git a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyScriptCompiler.java b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/DefaultGroovyScriptCompiler.java
similarity index 69%
rename from components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyScriptCompiler.java
rename to components/camel-groovy/src/main/java/org/apache/camel/language/groovy/DefaultGroovyScriptCompiler.java
index 56c1b6190b4..659eb7f97ff 100644
--- a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyScriptCompiler.java
+++ b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/DefaultGroovyScriptCompiler.java
@@ -23,19 +23,29 @@ import java.util.List;
import groovy.lang.GroovyShell;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
+import org.apache.camel.StaticService;
+import org.apache.camel.api.management.ManagedAttribute;
+import org.apache.camel.api.management.ManagedResource;
+import org.apache.camel.spi.GroovyScriptCompiler;
+import org.apache.camel.spi.annotations.JdkService;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.StopWatch;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class GroovyScriptCompiler extends ServiceSupport implements CamelContextAware {
+@JdkService(GroovyScriptCompiler.FACTORY)
+@ManagedResource(description = "Managed GroovyScriptCompiler")
+public class DefaultGroovyScriptCompiler extends ServiceSupport
+ implements CamelContextAware, GroovyScriptCompiler, StaticService {
- private static final Logger LOG = LoggerFactory.getLogger(GroovyScriptCompiler.class);
+ private static final Logger LOG = LoggerFactory.getLogger(DefaultGroovyScriptCompiler.class);
+ private long elapsed;
private GroovyScriptClassLoader classLoader;
private CamelContext camelContext;
- private String folder;
+ private String scriptPattern;
@Override
public CamelContext getCamelContext() {
@@ -47,12 +57,20 @@ public class GroovyScriptCompiler extends ServiceSupport implements CamelContext
this.camelContext = camelContext;
}
- public String getFolder() {
- return folder;
+ @ManagedAttribute(description = "Total compilation time in millis")
+ public long getCompileTime() {
+ return elapsed;
}
- public void setFolder(String folder) {
- this.folder = folder;
+ @ManagedAttribute(description = "Directories to scan for groovy source to be pre-compiled")
+ @Override
+ public String getScriptPattern() {
+ return scriptPattern;
+ }
+
+ @Override
+ public void setScriptPattern(String scriptPattern) {
+ this.scriptPattern = scriptPattern;
}
/**
@@ -74,8 +92,11 @@ public class GroovyScriptCompiler extends ServiceSupport implements CamelContext
protected void doStart() throws Exception {
super.doStart();
- if (folder != null) {
- LOG.info("Pre compiling groovy scripts from: {}", folder);
+ if (scriptPattern != null) {
+ StopWatch watch = new StopWatch();
+
+ // TODO: ant path style
+ LOG.info("Pre compiling groovy scripts from: {}", scriptPattern);
ClassLoader cl = camelContext.getApplicationContextClassLoader();
if (cl == null) {
@@ -88,12 +109,12 @@ public class GroovyScriptCompiler extends ServiceSupport implements CamelContext
camelContext.getCamelContextExtension().addContextPlugin(GroovyScriptClassLoader.class, classLoader);
CompilerConfiguration cc = new CompilerConfiguration();
- cc.setClasspathList(List.of(folder));
+ cc.setClasspathList(List.of(scriptPattern));
GroovyShell shell = new GroovyShell(cl, cc);
// discover each class from the folder
- File[] files = new File(folder).listFiles();
+ File[] files = new File(scriptPattern).listFiles();
if (files != null) {
for (File f : files) {
String code = IOHelper.loadText(new FileInputStream(f));
@@ -103,6 +124,7 @@ public class GroovyScriptCompiler extends ServiceSupport implements CamelContext
}
}
}
+ elapsed = watch.taken();
}
}
diff --git a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerTest.java b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/DefaultGroovyCompilerTest.java
similarity index 84%
rename from components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerTest.java
rename to components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/DefaultGroovyCompilerTest.java
index d3f74cdc11b..dbbdab88a49 100644
--- a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerTest.java
+++ b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/DefaultGroovyCompilerTest.java
@@ -18,19 +18,19 @@ package org.apache.camel.processor.groovy;
import java.lang.reflect.Method;
-import org.apache.camel.language.groovy.GroovyScriptCompiler;
+import org.apache.camel.language.groovy.DefaultGroovyScriptCompiler;
import org.apache.camel.support.ObjectHelper;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-public class GroovyCompilerTest extends CamelTestSupport {
+public class DefaultGroovyCompilerTest extends CamelTestSupport {
@Test
public void testCompiler() throws Exception {
- GroovyScriptCompiler compiler = new GroovyScriptCompiler();
+ DefaultGroovyScriptCompiler compiler = new DefaultGroovyScriptCompiler();
compiler.setCamelContext(context);
- compiler.setFolder("src/test/resources/myscript");
+ compiler.setScriptPattern("src/test/resources/myscript");
compiler.start();
Class<?> clazz = compiler.loadClass("Dude");
diff --git a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java
index 7dcdb079522..e7a7a270d68 100644
--- a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java
+++ b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java
@@ -20,7 +20,7 @@ import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.language.groovy.GroovyScriptCompiler;
+import org.apache.camel.language.groovy.DefaultGroovyScriptCompiler;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.Test;
@@ -30,9 +30,9 @@ public class GroovyCompilerRouteTest extends CamelTestSupport {
protected CamelContext createCamelContext() throws Exception {
CamelContext context = super.createCamelContext();
- GroovyScriptCompiler compiler = new GroovyScriptCompiler();
+ DefaultGroovyScriptCompiler compiler = new DefaultGroovyScriptCompiler();
compiler.setCamelContext(context);
- compiler.setFolder("src/test/resources/myscript");
+ compiler.setScriptPattern("src/test/resources/myscript");
context.addService(compiler);
return context;
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/GroovyScriptCompiler.java b/core/camel-api/src/main/java/org/apache/camel/spi/GroovyScriptCompiler.java
new file mode 100644
index 00000000000..bcbfc261859
--- /dev/null
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/GroovyScriptCompiler.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.spi;
+
+/**
+ * To let camel-groovy pre-compile script files during bootstrap.
+ */
+public interface GroovyScriptCompiler {
+
+ /**
+ * Service factory key.
+ */
+ String FACTORY = "groovy-script-compiler";
+
+ /**
+ * Directories to scan for groovy source to be pre-compiled. The directories are using Ant-path style pattern, and
+ * multiple directories can be specified separated by comma.
+ */
+ void setScriptPattern(String scriptPattern);
+
+ /**
+ * Directories to scan for groovy source to be pre-compiled. The directories are using Ant-path style pattern, and
+ * multiple directories can be specified separated by comma.
+ */
+ String getScriptPattern();
+}
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 072d1775448..e696ca0e69a 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -120,6 +120,7 @@ import org.apache.camel.spi.ExchangeFactoryManager;
import org.apache.camel.spi.ExecutorServiceManager;
import org.apache.camel.spi.FactoryFinder;
import org.apache.camel.spi.FactoryFinderResolver;
+import org.apache.camel.spi.GroovyScriptCompiler;
import org.apache.camel.spi.HeadersMapFactory;
import org.apache.camel.spi.InflightRepository;
import org.apache.camel.spi.Injector;
@@ -387,6 +388,7 @@ public abstract class AbstractCamelContext extends BaseService
this::createAnnotationBasedProcessorFactory);
camelContextExtension.lazyAddContextPlugin(DumpRoutesStrategy.class, this::createDumpRoutesStrategy);
camelContextExtension.lazyAddContextPlugin(BackOffTimerFactory.class, this::createBackOffTimerFactory);
+ camelContextExtension.lazyAddContextPlugin(GroovyScriptCompiler.class, this::createGroovyScriptCompiler);
}
protected static <T> T lookup(CamelContext context, String ref, Class<T> type) {
@@ -4301,6 +4303,8 @@ public abstract class AbstractCamelContext extends BaseService
protected abstract HeadersMapFactory createHeadersMapFactory();
+ protected abstract GroovyScriptCompiler createGroovyScriptCompiler();
+
protected abstract BeanProxyFactory createBeanProxyFactory();
protected abstract AnnotationBasedProcessorFactory createAnnotationBasedProcessorFactory();
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
index 5c7237038bd..45fc96b0158 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
@@ -56,6 +56,7 @@ import org.apache.camel.spi.ExchangeFactoryManager;
import org.apache.camel.spi.ExecutorServiceManager;
import org.apache.camel.spi.FactoryFinder;
import org.apache.camel.spi.FactoryFinderResolver;
+import org.apache.camel.spi.GroovyScriptCompiler;
import org.apache.camel.spi.HeadersMapFactory;
import org.apache.camel.spi.InflightRepository;
import org.apache.camel.spi.Injector;
@@ -473,6 +474,20 @@ public class SimpleCamelContext extends AbstractCamelContext {
return result.orElseGet(DefaultHeadersMapFactory::new);
}
+ @Override
+ protected GroovyScriptCompiler createGroovyScriptCompiler() {
+ Optional<GroovyScriptCompiler> result = ResolverHelper.resolveService(
+ getCamelContextReference(),
+ getCamelContextExtension().getBootstrapFactoryFinder(),
+ GroovyScriptCompiler.FACTORY,
+ GroovyScriptCompiler.class);
+ if (result.isPresent()) {
+ return result.get();
+ } else {
+ throw new IllegalArgumentException("Cannot find GroovyScriptCompiler on classpath. Add camel-groovy to classpath.");
+ }
+ }
+
private CliConnectorFactory createCliConnectorFactory() {
// lookup in registry first
CliConnectorFactory ccf = getCamelContextReference().getRegistry().findSingleByType(CliConnectorFactory.class);
diff --git a/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java b/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
index 1a7ccf5dbe0..02687bf7ddf 100644
--- a/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
+++ b/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
@@ -67,6 +67,7 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp
map.put("ExtraShutdownTimeout", int.class);
map.put("FileConfigurations", java.lang.String.class);
map.put("GlobalOptions", java.util.Map.class);
+ map.put("GroovyScriptPattern", java.lang.String.class);
map.put("InflightRepositoryBrowseEnabled", boolean.class);
map.put("JavaRoutesExcludePattern", java.lang.String.class);
map.put("JavaRoutesIncludePattern", java.lang.String.class);
@@ -239,6 +240,8 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp
case "fileConfigurations": target.setFileConfigurations(property(camelContext, java.lang.String.class, value)); return true;
case "globaloptions":
case "globalOptions": target.setGlobalOptions(property(camelContext, java.util.Map.class, value)); return true;
+ case "groovyscriptpattern":
+ case "groovyScriptPattern": target.setGroovyScriptPattern(property(camelContext, java.lang.String.class, value)); return true;
case "inflightrepositorybrowseenabled":
case "inflightRepositoryBrowseEnabled": target.setInflightRepositoryBrowseEnabled(property(camelContext, boolean.class, value)); return true;
case "javaroutesexcludepattern":
@@ -489,6 +492,8 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp
case "fileConfigurations": return java.lang.String.class;
case "globaloptions":
case "globalOptions": return java.util.Map.class;
+ case "groovyscriptpattern":
+ case "groovyScriptPattern": return java.lang.String.class;
case "inflightrepositorybrowseenabled":
case "inflightRepositoryBrowseEnabled": return boolean.class;
case "javaroutesexcludepattern":
@@ -735,6 +740,8 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp
case "fileConfigurations": return target.getFileConfigurations();
case "globaloptions":
case "globalOptions": return target.getGlobalOptions();
+ case "groovyscriptpattern":
+ case "groovyScriptPattern": return target.getGroovyScriptPattern();
case "inflightrepositorybrowseenabled":
case "inflightRepositoryBrowseEnabled": return target.isInflightRepositoryBrowseEnabled();
case "javaroutesexcludepattern":
diff --git a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
index ecd123e7a0b..d01b20ce547 100644
--- a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
+++ b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
@@ -72,6 +72,7 @@
{ "name": "camel.main.extraShutdownTimeout", "description": "Extra timeout in seconds to graceful shutdown Camel. When Camel is shutting down then Camel first shutdown all the routes (shutdownTimeout). Then additional services is shutdown (extraShutdownTimeout).", "sourceType": "org.apache.camel.main.MainConfigurationProperties", "type": "integer", "javaType": "int", "defaultValue": 15 },
{ "name": "camel.main.fileConfigurations", "description": "Directory to load additional configuration files that contains configuration values that takes precedence over any other configuration. This can be used to refer to files that may have secret configuration that has been mounted on the file system for containers. You can specify a pattern to load from sub directories and a name pattern such as \/var\/app\/secret\/.properties, multiple directories can be separated by comma.", " [...]
{ "name": "camel.main.globalOptions", "description": "Sets global options that can be referenced in the camel context Important: This has nothing to do with property placeholders, and is just a plain set of key\/value pairs which are used to configure global options on CamelContext, such as a maximum debug logging length etc.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "object", "javaType": "java.util.Map" },
+ { "name": "camel.main.groovyScriptPattern", "description": "Directories to scan for groovy source to be pre-compiled. The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "string", "javaType": "java.lang.String" },
{ "name": "camel.main.inflightRepositoryBrowseEnabled", "description": "Sets whether the inflight repository should allow browsing each inflight exchange. This is by default disabled as there is a very slight performance overhead when enabled.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean", "defaultValue": "false" },
{ "name": "camel.main.javaRoutesExcludePattern", "description": "Used for exclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. For example to exclude all classes starting with Bar use: **\/Bar* To exclude all routes form a specific package use: com\/mycomp [...]
{ "name": "camel.main.javaRoutesIncludePattern", "description": "Used for inclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. Multiple patterns can be specified separated by comma. For example to include all classes starting with Foo use: **\/Foo To include a [...]
diff --git a/core/camel-main/src/main/docs/main.adoc b/core/camel-main/src/main/docs/main.adoc
index dde170b84a0..8c1452e51ee 100644
--- a/core/camel-main/src/main/docs/main.adoc
+++ b/core/camel-main/src/main/docs/main.adoc
@@ -19,7 +19,7 @@ The following tables lists all the options:
// main options: START
=== Camel Main configurations
-The camel.main supports 122 options, which are listed below.
+The camel.main supports 123 options, which are listed below.
[width="100%",cols="2,5,^1,2",options="header"]
|===
@@ -69,6 +69,7 @@ The camel.main supports 122 options, which are listed below.
| *camel.main.extraShutdown{zwsp}Timeout* | Extra timeout in seconds to graceful shutdown Camel. When Camel is shutting down then Camel first shutdown all the routes (shutdownTimeout). Then additional services is shutdown (extraShutdownTimeout). | 15 | int
| *camel.main.fileConfigurations* | Directory to load additional configuration files that contains configuration values that takes precedence over any other configuration. This can be used to refer to files that may have secret configuration that has been mounted on the file system for containers. You can specify a pattern to load from sub directories and a name pattern such as /var/app/secret/.properties, multiple directories can be separated by comma. | | String
| *camel.main.globalOptions* | Sets global options that can be referenced in the camel context Important: This has nothing to do with property placeholders, and is just a plain set of key/value pairs which are used to configure global options on CamelContext, such as a maximum debug logging length etc. | | Map
+| *camel.main.groovyScriptPattern* | Directories to scan for groovy source to be pre-compiled. The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma. | | String
| *camel.main.inflightRepository{zwsp}BrowseEnabled* | Sets whether the inflight repository should allow browsing each inflight exchange. This is by default disabled as there is a very slight performance overhead when enabled. | false | boolean
| *camel.main.javaRoutesExclude{zwsp}Pattern* | Used for exclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. For example to exclude all classes starting with Bar use: **/Bar* To exclude all routes form a specific package use: com/mycompany/bar/* To exclud [...]
| *camel.main.javaRoutesInclude{zwsp}Pattern* | Used for inclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. Multiple patterns can be specified separated by comma. For example to include all classes starting with Foo use: **/Foo To include all routes form a speci [...]
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
index d1dee0fb9dd..66381be9c6f 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
@@ -61,6 +61,7 @@ import org.apache.camel.spi.EventNotifier;
import org.apache.camel.spi.ExchangeFactory;
import org.apache.camel.spi.ExecutorServiceManager;
import org.apache.camel.spi.FactoryFinderResolver;
+import org.apache.camel.spi.GroovyScriptCompiler;
import org.apache.camel.spi.InflightRepository;
import org.apache.camel.spi.InterceptStrategy;
import org.apache.camel.spi.LifecycleStrategy;
@@ -287,6 +288,11 @@ public final class DefaultConfigurationConfigurer {
ContextReloadStrategy reloader = new DefaultContextReloadStrategy();
camelContext.addService(reloader);
}
+ if (config.getGroovyScriptPattern() != null) {
+ GroovyScriptCompiler gsc = camelContext.getCamelContextExtension().getContextPlugin(GroovyScriptCompiler.class);
+ gsc.setScriptPattern(config.getGroovyScriptPattern());
+ camelContext.addService(gsc);
+ }
if (camelContext.getManagementStrategy().getManagementAgent() != null) {
camelContext.getManagementStrategy().getManagementAgent()
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
index d3658ccef79..1228a4f3fd9 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
@@ -129,6 +129,7 @@ public abstract class DefaultConfigurationProperties<T> {
@Metadata(defaultValue = "true")
private boolean routesReloadRemoveAllRoutes = true;
private boolean routesReloadRestartDuration;
+ private String groovyScriptPattern;
@Metadata(defaultValue = "default", enums = "default,prototype,pooled")
private String exchangeFactory = "default";
private int exchangeFactoryCapacity = 100;
@@ -1394,6 +1395,18 @@ public abstract class DefaultConfigurationProperties<T> {
this.jmxUpdateRouteEnabled = jmxUpdateRouteEnabled;
}
+ public String getGroovyScriptPattern() {
+ return groovyScriptPattern;
+ }
+
+ /**
+ * Directories to scan for groovy source to be pre-compiled. The directories are using Ant-path style pattern, and
+ * multiple directories can be specified separated by comma.
+ */
+ public void setGroovyScriptPattern(String groovyScriptPattern) {
+ this.groovyScriptPattern = groovyScriptPattern;
+ }
+
public String getExchangeFactory() {
return exchangeFactory;
}
@@ -1634,6 +1647,18 @@ public abstract class DefaultConfigurationProperties<T> {
this.startupRecorderDir = startupRecorderDir;
}
+ public String getCloudPropertiesLocation() {
+ return cloudPropertiesLocation;
+ }
+
+ /**
+ * Sets the locations (comma separated values) where to find properties configuration as defined for cloud native
+ * environments such as Kubernetes. You should only scan text based mounted configuration.
+ */
+ public void setCloudPropertiesLocation(String cloudPropertiesLocation) {
+ this.cloudPropertiesLocation = cloudPropertiesLocation;
+ }
+
// fluent builders
// --------------------------------------------------------------
@@ -2756,16 +2781,13 @@ public abstract class DefaultConfigurationProperties<T> {
return (T) this;
}
- public String getCloudPropertiesLocation() {
- return cloudPropertiesLocation;
- }
-
/**
- * Sets the locations (comma separated values) where to find properties configuration as defined for cloud native
- * environments such as Kubernetes. You should only scan text based mounted configuration.
+ * Directories to scan for groovy source to be pre-compiled. The directories are using Ant-path style pattern, and
+ * multiple directories can be specified separated by comma.
*/
- public void setCloudPropertiesLocation(String cloudPropertiesLocation) {
- this.cloudPropertiesLocation = cloudPropertiesLocation;
+ public T withGroovyScriptPattern(String groovyScriptPattern) {
+ this.groovyScriptPattern = groovyScriptPattern;
+ return (T) this;
}
/**
(camel) 04/13: CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
Posted by da...@apache.org.
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 7f060889f6c79dc38bb356bf8a6d4dd597b1a95d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 14 12:07:53 2025 +0200
CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
---
.../main/camel-main-configuration-metadata.json | 1 +
.../src/main/docs/groovy-language.adoc | 32 ++++++++++++++-
.../groovy/DefaultGroovyScriptCompiler.java | 47 +++++++++++++++-------
.../groovy/DefaultGroovyCompilerTest.java | 2 +-
.../processor/groovy/GroovyCompilerRouteTest.java | 2 +-
.../groovy/MainGroovyCompilerRouteTest.java | 4 +-
.../camel-main-configuration-metadata.json | 2 +-
core/camel-main/src/main/docs/main.adoc | 2 +-
.../camel/main/DefaultConfigurationProperties.java | 14 ++++++-
9 files changed, 83 insertions(+), 23 deletions(-)
diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/main/camel-main-configuration-metadata.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/main/camel-main-configuration-metadata.json
index ecd123e7a0b..c857def438b 100644
--- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/main/camel-main-configuration-metadata.json
+++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/main/camel-main-configuration-metadata.json
@@ -72,6 +72,7 @@
{ "name": "camel.main.extraShutdownTimeout", "description": "Extra timeout in seconds to graceful shutdown Camel. When Camel is shutting down then Camel first shutdown all the routes (shutdownTimeout). Then additional services is shutdown (extraShutdownTimeout).", "sourceType": "org.apache.camel.main.MainConfigurationProperties", "type": "integer", "javaType": "int", "defaultValue": 15 },
{ "name": "camel.main.fileConfigurations", "description": "Directory to load additional configuration files that contains configuration values that takes precedence over any other configuration. This can be used to refer to files that may have secret configuration that has been mounted on the file system for containers. You can specify a pattern to load from sub directories and a name pattern such as \/var\/app\/secret\/.properties, multiple directories can be separated by comma.", " [...]
{ "name": "camel.main.globalOptions", "description": "Sets global options that can be referenced in the camel context Important: This has nothing to do with property placeholders, and is just a plain set of key\/value pairs which are used to configure global options on CamelContext, such as a maximum debug logging length etc.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "object", "javaType": "java.util.Map" },
+ { "name": "camel.main.groovyScriptPattern", "description": "Directories to scan for groovy source to be pre-compiled. For example: scripts\/.groovy will scan inside the classpath folder scripts for all groovy source files. By default, sources are scanned from the classpath, but you can prefix with file: to use file system. The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma.", "sourceType": "org.apache.camel.main.DefaultConfi [...]
{ "name": "camel.main.inflightRepositoryBrowseEnabled", "description": "Sets whether the inflight repository should allow browsing each inflight exchange. This is by default disabled as there is a very slight performance overhead when enabled.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean", "defaultValue": "false" },
{ "name": "camel.main.javaRoutesExcludePattern", "description": "Used for exclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. For example to exclude all classes starting with Bar use: **\/Bar* To exclude all routes form a specific package use: com\/mycomp [...]
{ "name": "camel.main.javaRoutesIncludePattern", "description": "Used for inclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. Multiple patterns can be specified separated by comma. For example to include all classes starting with Foo use: **\/Foo To include a [...]
diff --git a/components/camel-groovy/src/main/docs/groovy-language.adoc b/components/camel-groovy/src/main/docs/groovy-language.adoc
index bba44aa4d4c..8077a2bdcc8 100644
--- a/components/camel-groovy/src/main/docs/groovy-language.adoc
+++ b/components/camel-groovy/src/main/docs/groovy-language.adoc
@@ -163,7 +163,37 @@ XML DSL::
</filter>
</route>
----
-
====
+== Pre compiling shared groovy scripts
+
+*Preview* support level.
+
+In *Camel 4.14* we have added support for loading groovy source files and pre-compile
+on startup. This allows to have a common set of groovy classes and functions which can be
+used by Camel and Java.
+
+To enable this you need to configure this in `camel-main` via
+
+[source,properties]
+----
+camel.main.groovyScriptPattern = myscript/*.groovy
+----
+
+Then in the `src/main/resources/myscript` folder you can have groovy source files that Camel
+will pre-compile on startup, and make global available via a special `GroovyScriptClassLoader`.
+
+Because this class-loader is required to be in use for being able to load the groovy pre-compiled
+classes, then this feature will only work via Camel which has control of classloading when used
+with Camel features that would support this such as in the route DSL and elsewhere.
+
+However, there may be some features in Camel where this may not work (yet).
+
+IMPORTANT: This feature is only intended to include smaller groovy sources as small functions, DTOs
+that makes it easier to use together with Camel for low-code integrations. It is not
+intended to support Groovy as a general purpose programming language for Camel. For this kind
+then you can use groovy and Java together and follow best practices for this, such as
+using the joint-compilation via Maven / Gradle plugins during build.
+
+
include::spring-boot:partial$starter.adoc[]
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 2ec12ed6a79..23198c28a0f 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
@@ -16,8 +16,7 @@
*/
package org.apache.camel.language.groovy;
-import java.io.File;
-import java.io.FileInputStream;
+import java.util.ArrayList;
import java.util.List;
import groovy.lang.GroovyShell;
@@ -27,7 +26,10 @@ import org.apache.camel.StaticService;
import org.apache.camel.api.management.ManagedAttribute;
import org.apache.camel.api.management.ManagedResource;
import org.apache.camel.spi.GroovyScriptCompiler;
+import org.apache.camel.spi.PackageScanResourceResolver;
+import org.apache.camel.spi.Resource;
import org.apache.camel.spi.annotations.JdkService;
+import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StopWatch;
@@ -100,7 +102,6 @@ public class DefaultGroovyScriptCompiler extends ServiceSupport
if (scriptPattern != null) {
StopWatch watch = new StopWatch();
- // TODO: ant path style
LOG.info("Pre compiling groovy scripts from: {}", scriptPattern);
ClassLoader cl = camelContext.getApplicationContextClassLoader();
@@ -113,20 +114,38 @@ public class DefaultGroovyScriptCompiler extends ServiceSupport
// make classloader available for groovy language
camelContext.getCamelContextExtension().addContextPlugin(GroovyScriptClassLoader.class, classLoader);
- CompilerConfiguration cc = new CompilerConfiguration();
- cc.setClasspathList(List.of(scriptPattern));
+ // scan for groovy source files to include
+ List<String> cps = new ArrayList<>();
+ List<String> codes = new ArrayList<>();
+ PackageScanResourceResolver resolver = PluginHelper.getPackageScanResourceResolver(camelContext);
+ for (String pattern : scriptPattern.split(",")) {
+ for (Resource resource : resolver.findResources(pattern)) {
+ if (resource.exists()) {
+ String loc = null;
+ if ("classpath".equals(resource.getScheme())) {
+ loc = resource.getLocation();
+ } else if ("file".equals(resource.getScheme())) {
+ loc = resource.getLocation();
+ }
+ if (loc != null) {
+ cps.add(loc);
+ String code = IOHelper.loadText(resource.getInputStream());
+ codes.add(code);
+ }
+ }
+ }
+ }
+ // setup compiler via groovy shell
+ CompilerConfiguration cc = new CompilerConfiguration();
+ cc.setClasspathList(cps);
GroovyShell shell = new GroovyShell(cl, cc);
- // discover each class from the folder
- File[] files = new File(scriptPattern).listFiles();
- if (files != null) {
- for (File f : files) {
- String code = IOHelper.loadText(new FileInputStream(f));
- Class<?> clazz = shell.getClassLoader().parseClass(code);
- if (clazz != null) {
- classLoader.addClass(clazz.getName(), clazz);
- }
+ // parse code into classes and add to classloader
+ for (String code : codes) {
+ Class<?> clazz = shell.getClassLoader().parseClass(code);
+ if (clazz != null) {
+ classLoader.addClass(clazz.getName(), clazz);
}
}
elapsed = watch.taken();
diff --git a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/DefaultGroovyCompilerTest.java b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/DefaultGroovyCompilerTest.java
index dbbdab88a49..31350946601 100644
--- a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/DefaultGroovyCompilerTest.java
+++ b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/DefaultGroovyCompilerTest.java
@@ -30,7 +30,7 @@ public class DefaultGroovyCompilerTest extends CamelTestSupport {
public void testCompiler() throws Exception {
DefaultGroovyScriptCompiler compiler = new DefaultGroovyScriptCompiler();
compiler.setCamelContext(context);
- compiler.setScriptPattern("src/test/resources/myscript");
+ compiler.setScriptPattern("myscript/*.groovy");
compiler.start();
Class<?> clazz = compiler.loadClass("Dude");
diff --git a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java
index e7a7a270d68..f29a827801a 100644
--- a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java
+++ b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java
@@ -32,7 +32,7 @@ public class GroovyCompilerRouteTest extends CamelTestSupport {
DefaultGroovyScriptCompiler compiler = new DefaultGroovyScriptCompiler();
compiler.setCamelContext(context);
- compiler.setScriptPattern("src/test/resources/myscript");
+ compiler.setScriptPattern("file:src/test/resources/myscript/*.groovy");
context.addService(compiler);
return context;
diff --git a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/MainGroovyCompilerRouteTest.java b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/MainGroovyCompilerRouteTest.java
index d8c5b7a7291..d9319504930 100644
--- a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/MainGroovyCompilerRouteTest.java
+++ b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/MainGroovyCompilerRouteTest.java
@@ -31,7 +31,7 @@ public class MainGroovyCompilerRouteTest {
public void testCompilerRoute() throws Exception {
Main main = new Main();
main.configure().addRoutesBuilder(createRouteBuilder());
- main.configure().withGroovyScriptPattern("src/test/resources/myscript");
+ main.configure().withGroovyScriptPattern("myscript/*.groovy");
main.start();
CamelContext context = main.getCamelContext();
@@ -45,7 +45,7 @@ public class MainGroovyCompilerRouteTest {
DefaultGroovyScriptCompiler compiler = context.hasService(DefaultGroovyScriptCompiler.class);
Assertions.assertNotNull(compiler);
- Assertions.assertEquals("src/test/resources/myscript", compiler.getScriptPattern());
+ Assertions.assertEquals("myscript/*.groovy", compiler.getScriptPattern());
Assertions.assertEquals(2, compiler.getClassesSize());
Assertions.assertTrue(compiler.getCompileTime() > 0, "Should take time to compile, was: " + compiler.getCompileTime());
diff --git a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
index d01b20ce547..c857def438b 100644
--- a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
+++ b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
@@ -72,7 +72,7 @@
{ "name": "camel.main.extraShutdownTimeout", "description": "Extra timeout in seconds to graceful shutdown Camel. When Camel is shutting down then Camel first shutdown all the routes (shutdownTimeout). Then additional services is shutdown (extraShutdownTimeout).", "sourceType": "org.apache.camel.main.MainConfigurationProperties", "type": "integer", "javaType": "int", "defaultValue": 15 },
{ "name": "camel.main.fileConfigurations", "description": "Directory to load additional configuration files that contains configuration values that takes precedence over any other configuration. This can be used to refer to files that may have secret configuration that has been mounted on the file system for containers. You can specify a pattern to load from sub directories and a name pattern such as \/var\/app\/secret\/.properties, multiple directories can be separated by comma.", " [...]
{ "name": "camel.main.globalOptions", "description": "Sets global options that can be referenced in the camel context Important: This has nothing to do with property placeholders, and is just a plain set of key\/value pairs which are used to configure global options on CamelContext, such as a maximum debug logging length etc.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "object", "javaType": "java.util.Map" },
- { "name": "camel.main.groovyScriptPattern", "description": "Directories to scan for groovy source to be pre-compiled. The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "string", "javaType": "java.lang.String" },
+ { "name": "camel.main.groovyScriptPattern", "description": "Directories to scan for groovy source to be pre-compiled. For example: scripts\/.groovy will scan inside the classpath folder scripts for all groovy source files. By default, sources are scanned from the classpath, but you can prefix with file: to use file system. The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma.", "sourceType": "org.apache.camel.main.DefaultConfi [...]
{ "name": "camel.main.inflightRepositoryBrowseEnabled", "description": "Sets whether the inflight repository should allow browsing each inflight exchange. This is by default disabled as there is a very slight performance overhead when enabled.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean", "defaultValue": "false" },
{ "name": "camel.main.javaRoutesExcludePattern", "description": "Used for exclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. For example to exclude all classes starting with Bar use: **\/Bar* To exclude all routes form a specific package use: com\/mycomp [...]
{ "name": "camel.main.javaRoutesIncludePattern", "description": "Used for inclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. Multiple patterns can be specified separated by comma. For example to include all classes starting with Foo use: **\/Foo To include a [...]
diff --git a/core/camel-main/src/main/docs/main.adoc b/core/camel-main/src/main/docs/main.adoc
index 8c1452e51ee..dcbbafc5283 100644
--- a/core/camel-main/src/main/docs/main.adoc
+++ b/core/camel-main/src/main/docs/main.adoc
@@ -69,7 +69,7 @@ The camel.main supports 123 options, which are listed below.
| *camel.main.extraShutdown{zwsp}Timeout* | Extra timeout in seconds to graceful shutdown Camel. When Camel is shutting down then Camel first shutdown all the routes (shutdownTimeout). Then additional services is shutdown (extraShutdownTimeout). | 15 | int
| *camel.main.fileConfigurations* | Directory to load additional configuration files that contains configuration values that takes precedence over any other configuration. This can be used to refer to files that may have secret configuration that has been mounted on the file system for containers. You can specify a pattern to load from sub directories and a name pattern such as /var/app/secret/.properties, multiple directories can be separated by comma. | | String
| *camel.main.globalOptions* | Sets global options that can be referenced in the camel context Important: This has nothing to do with property placeholders, and is just a plain set of key/value pairs which are used to configure global options on CamelContext, such as a maximum debug logging length etc. | | Map
-| *camel.main.groovyScriptPattern* | Directories to scan for groovy source to be pre-compiled. The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma. | | String
+| *camel.main.groovyScriptPattern* | Directories to scan for groovy source to be pre-compiled. For example: scripts/.groovy will scan inside the classpath folder scripts for all groovy source files. By default, sources are scanned from the classpath, but you can prefix with file: to use file system. The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma. | | String
| *camel.main.inflightRepository{zwsp}BrowseEnabled* | Sets whether the inflight repository should allow browsing each inflight exchange. This is by default disabled as there is a very slight performance overhead when enabled. | false | boolean
| *camel.main.javaRoutesExclude{zwsp}Pattern* | Used for exclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. For example to exclude all classes starting with Bar use: **/Bar* To exclude all routes form a specific package use: com/mycompany/bar/* To exclud [...]
| *camel.main.javaRoutesInclude{zwsp}Pattern* | Used for inclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. Multiple patterns can be specified separated by comma. For example to include all classes starting with Foo use: **/Foo To include all routes form a speci [...]
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
index 1228a4f3fd9..986488440f4 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
@@ -1400,7 +1400,12 @@ public abstract class DefaultConfigurationProperties<T> {
}
/**
- * Directories to scan for groovy source to be pre-compiled. The directories are using Ant-path style pattern, and
+ * Directories to scan for groovy source to be pre-compiled.
+ * For example: scripts/*.groovy will scan inside the classpath folder scripts for all groovy source files.
+ *
+ * By default, sources are scanned from the classpath, but you can prefix with file: to use file system.
+ *
+ * The directories are using Ant-path style pattern, and
* multiple directories can be specified separated by comma.
*/
public void setGroovyScriptPattern(String groovyScriptPattern) {
@@ -2782,7 +2787,12 @@ public abstract class DefaultConfigurationProperties<T> {
}
/**
- * Directories to scan for groovy source to be pre-compiled. The directories are using Ant-path style pattern, and
+ * Directories to scan for groovy source to be pre-compiled.
+ * For example: scripts/*.groovy will scan inside the classpath folder scripts for all groovy source files.
+ *
+ * By default, sources are scanned from the classpath, but you can prefix with file: to use file system.
+ *
+ * The directories are using Ant-path style pattern, and
* multiple directories can be specified separated by comma.
*/
public T withGroovyScriptPattern(String groovyScriptPattern) {
(camel) 05/13: CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
Posted by da...@apache.org.
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 0a05ed3c13cec98d7029e9b934787b511ab7b160
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 14 13:17:28 2025 +0200
CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
---
.../apache/camel/main/DefaultConfigurationConfigurer.java | 3 ++-
.../apache/camel/main/DefaultConfigurationProperties.java | 14 ++++++--------
2 files changed, 8 insertions(+), 9 deletions(-)
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
index 66381be9c6f..035ea76210d 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
@@ -291,7 +291,8 @@ public final class DefaultConfigurationConfigurer {
if (config.getGroovyScriptPattern() != null) {
GroovyScriptCompiler gsc = camelContext.getCamelContextExtension().getContextPlugin(GroovyScriptCompiler.class);
gsc.setScriptPattern(config.getGroovyScriptPattern());
- camelContext.addService(gsc);
+ // force start compiler eager so Camel routes can load these pre-compiled classes
+ camelContext.addService(gsc, true, true);
}
if (camelContext.getManagementStrategy().getManagementAgent() != null) {
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
index 986488440f4..f09fca6bb2c 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
@@ -1400,13 +1400,12 @@ public abstract class DefaultConfigurationProperties<T> {
}
/**
- * Directories to scan for groovy source to be pre-compiled.
- * For example: scripts/*.groovy will scan inside the classpath folder scripts for all groovy source files.
+ * Directories to scan for groovy source to be pre-compiled. For example: scripts/*.groovy will scan inside the
+ * classpath folder scripts for all groovy source files.
*
* By default, sources are scanned from the classpath, but you can prefix with file: to use file system.
*
- * The directories are using Ant-path style pattern, and
- * multiple directories can be specified separated by comma.
+ * The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma.
*/
public void setGroovyScriptPattern(String groovyScriptPattern) {
this.groovyScriptPattern = groovyScriptPattern;
@@ -2787,13 +2786,12 @@ public abstract class DefaultConfigurationProperties<T> {
}
/**
- * Directories to scan for groovy source to be pre-compiled.
- * For example: scripts/*.groovy will scan inside the classpath folder scripts for all groovy source files.
+ * Directories to scan for groovy source to be pre-compiled. For example: scripts/*.groovy will scan inside the
+ * classpath folder scripts for all groovy source files.
*
* By default, sources are scanned from the classpath, but you can prefix with file: to use file system.
*
- * The directories are using Ant-path style pattern, and
- * multiple directories can be specified separated by comma.
+ * The directories are using Ant-path style pattern, and multiple directories can be specified separated by comma.
*/
public T withGroovyScriptPattern(String groovyScriptPattern) {
this.groovyScriptPattern = groovyScriptPattern;
(camel) 10/13: CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
Posted by da...@apache.org.
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 a84fddf6b99848d7bb28bef95706dfdd4dac1701
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 14 14:42:32 2025 +0200
CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
---
.../org/apache/camel/language/groovy/GroovyScriptClassLoader.java | 5 +++++
1 file changed, 5 insertions(+)
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 f1149c526bb..5b90a5722d8 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
@@ -31,6 +31,11 @@ public class GroovyScriptClassLoader extends ClassLoader implements Closeable {
super(parent);
}
+ @Override
+ public String getName() {
+ return "GroovyScriptClassLoader";
+ }
+
public void addClass(String name, Class<?> clazz) {
classes.put(name, clazz);
}
(camel) 01/13: CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
Posted by da...@apache.org.
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 376face1dfe73b683879cab2036767c0ee17b3d9
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 14 09:25:32 2025 +0200
CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
---
.../camel/language/groovy/GroovyExpression.java | 3 +-
.../camel/language/groovy/GroovyLanguage.java | 5 +-
.../language/groovy/GroovyScriptClassLoader.java | 55 ++++++++++
.../language/groovy/GroovyScriptCompiler.java | 114 +++++++++++++++++++++
.../processor/groovy/GroovyCompilerRouteTest.java | 64 ++++++++++++
.../camel/processor/groovy/GroovyCompilerTest.java | 45 ++++++++
.../src/test/resources/myscript/Cheese.groovy | 25 +++++
.../src/test/resources/myscript/Dude.groovy | 26 +++++
8 files changed, 334 insertions(+), 3 deletions(-)
diff --git a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
index f9c7a38a228..bdcf49f8e05 100644
--- a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
+++ b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
@@ -79,7 +79,8 @@ public class GroovyExpression extends ExpressionSupport {
final String key = fileName != null ? fileName + text : text;
Class<Script> scriptClass = language.getScriptFromCache(key);
if (scriptClass == null) {
- ClassLoader cl = exchange.getContext().getApplicationContextClassLoader();
+ // prefer to use classloader from groovy script compiler, and if not fallback to app context
+ ClassLoader cl = exchange.getContext().getCamelContextExtension().getContextPlugin(GroovyScriptClassLoader.class);
GroovyShell shell = shellFactory != null ? shellFactory.createGroovyShell(exchange)
: cl != null ? new GroovyShell(cl) : new GroovyShell();
scriptClass = fileName != null
diff --git a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
index 8e1faa20ff9..9160d612f79 100644
--- a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
+++ b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
@@ -143,8 +143,9 @@ public class GroovyLanguage extends TypedLanguageSupport implements ScriptingLan
}
Class<Script> clazz = getScriptFromCache(script);
if (clazz == null) {
- ClassLoader cl = getCamelContext().getApplicationContextClassLoader();
- GroovyShell shell = new GroovyShell(cl);
+ // prefer to use classloader from groovy script compiler, and if not fallback to app context
+ ClassLoader cl = getCamelContext().getCamelContextExtension().getContextPlugin(GroovyScriptClassLoader.class);
+ GroovyShell shell = cl != null ? new GroovyShell(cl) : new GroovyShell();
clazz = shell.getClassLoader().parseClass(script);
addScriptToCache(script, clazz);
}
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
new file mode 100644
index 00000000000..330af11659d
--- /dev/null
+++ b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyScriptClassLoader.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.language.groovy;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.codehaus.groovy.runtime.InvokerHelper;
+
+public class GroovyScriptClassLoader extends ClassLoader implements Closeable {
+
+ private final Map<String, Class<?>> classes = new HashMap<>();
+
+ public GroovyScriptClassLoader(ClassLoader parent) {
+ super(parent);
+ }
+
+ public void addClass(String name, Class<?> clazz) {
+ classes.put(name, clazz);
+ }
+
+ @Override
+ protected Class<?> findClass(String name) throws ClassNotFoundException {
+ Class<?> answer = classes.get(name);
+ if (answer == null) {
+ answer = super.findClass(name);
+ }
+ return answer;
+ }
+
+ @Override
+ public void close() throws IOException {
+ for (Class<?> clazz : classes.values()) {
+ InvokerHelper.removeClass(clazz);
+ }
+ classes.clear();
+ }
+
+}
diff --git a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyScriptCompiler.java b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyScriptCompiler.java
new file mode 100644
index 00000000000..56c1b6190b4
--- /dev/null
+++ b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyScriptCompiler.java
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.language.groovy;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.List;
+
+import groovy.lang.GroovyShell;
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.IOHelper;
+import org.codehaus.groovy.control.CompilerConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class GroovyScriptCompiler extends ServiceSupport implements CamelContextAware {
+
+ private static final Logger LOG = LoggerFactory.getLogger(GroovyScriptCompiler.class);
+
+ private GroovyScriptClassLoader classLoader;
+ private CamelContext camelContext;
+ private String folder;
+
+ @Override
+ public CamelContext getCamelContext() {
+ return camelContext;
+ }
+
+ @Override
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
+ public String getFolder() {
+ return folder;
+ }
+
+ public void setFolder(String folder) {
+ this.folder = folder;
+ }
+
+ /**
+ * Loads the given class
+ *
+ * @param name the FQN class name
+ * @return the loaded class
+ * @throws ClassNotFoundException is thrown if class is not found
+ */
+ public Class<?> loadClass(String name) throws ClassNotFoundException {
+ if (classLoader != null) {
+ return classLoader.findClass(name);
+ } else {
+ throw new ClassNotFoundException(name);
+ }
+ }
+
+ @Override
+ protected void doStart() throws Exception {
+ super.doStart();
+
+ if (folder != null) {
+ LOG.info("Pre compiling groovy scripts from: {}", folder);
+
+ 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);
+
+ CompilerConfiguration cc = new CompilerConfiguration();
+ cc.setClasspathList(List.of(folder));
+
+ GroovyShell shell = new GroovyShell(cl, cc);
+
+ // discover each class from the folder
+ File[] files = new File(folder).listFiles();
+ if (files != null) {
+ for (File f : files) {
+ String code = IOHelper.loadText(new FileInputStream(f));
+ Class<?> clazz = shell.getClassLoader().parseClass(code);
+ if (clazz != null) {
+ classLoader.addClass(clazz.getName(), clazz);
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ protected void doStop() throws Exception {
+ super.doStop();
+ IOHelper.close(classLoader);
+ }
+}
diff --git a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java
new file mode 100644
index 00000000000..7dcdb079522
--- /dev/null
+++ b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerRouteTest.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor.groovy;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.language.groovy.GroovyScriptCompiler;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+public class GroovyCompilerRouteTest extends CamelTestSupport {
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+
+ GroovyScriptCompiler compiler = new GroovyScriptCompiler();
+ compiler.setCamelContext(context);
+ compiler.setFolder("src/test/resources/myscript");
+ context.addService(compiler);
+
+ return context;
+ }
+
+ @Test
+ public void testCompilerRoute() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("I want to order 2 gauda");
+
+ template.sendBodyAndHeader("direct:start", "Hello World", "amount", 2);
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .setBody().groovy("""
+ Dude d = new Dude()
+ return d.order(header.amount)
+ """)
+ .to("mock:result");
+ }
+ };
+ }
+}
diff --git a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerTest.java b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerTest.java
new file mode 100644
index 00000000000..d3f74cdc11b
--- /dev/null
+++ b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/GroovyCompilerTest.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor.groovy;
+
+import java.lang.reflect.Method;
+
+import org.apache.camel.language.groovy.GroovyScriptCompiler;
+import org.apache.camel.support.ObjectHelper;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class GroovyCompilerTest extends CamelTestSupport {
+
+ @Test
+ public void testCompiler() throws Exception {
+ GroovyScriptCompiler compiler = new GroovyScriptCompiler();
+ compiler.setCamelContext(context);
+ compiler.setFolder("src/test/resources/myscript");
+ compiler.start();
+
+ Class<?> clazz = compiler.loadClass("Dude");
+ Object dude = ObjectHelper.newInstance(clazz);
+ Method m = clazz.getMethod("order", int.class);
+ Object o = ObjectHelper.newInstance(clazz);
+ Object out = m.invoke(o, 5);
+
+ Assertions.assertEquals("I want to order 5 gauda", out);
+ }
+
+}
diff --git a/components/camel-groovy/src/test/resources/myscript/Cheese.groovy b/components/camel-groovy/src/test/resources/myscript/Cheese.groovy
new file mode 100644
index 00000000000..722b9a035b3
--- /dev/null
+++ b/components/camel-groovy/src/test/resources/myscript/Cheese.groovy
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+class Cheese {
+
+ def name() {
+ return 'gauda'
+ }
+
+}
+
diff --git a/components/camel-groovy/src/test/resources/myscript/Dude.groovy b/components/camel-groovy/src/test/resources/myscript/Dude.groovy
new file mode 100644
index 00000000000..0a87932b16d
--- /dev/null
+++ b/components/camel-groovy/src/test/resources/myscript/Dude.groovy
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+class Dude {
+
+ def order(int a) {
+ def greeter = new Cheese()
+ return 'I want to order ' + a + ' ' + greeter.name()
+ }
+
+}
+
(camel) 03/13: CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
Posted by da...@apache.org.
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 b37dfbf0cab67ba70c2f3132f61e7a561d944146
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 14 10:22:19 2025 +0200
CAMEL-22214: camel-groovy - Allow to pre-load groovy source files for shared functions and DTOs
---
components/camel-groovy/pom.xml | 5 ++
.../groovy/DefaultGroovyScriptCompiler.java | 5 ++
.../language/groovy/GroovyScriptClassLoader.java | 7 +++
.../groovy/MainGroovyCompilerRouteTest.java | 69 ++++++++++++++++++++++
4 files changed, 86 insertions(+)
diff --git a/components/camel-groovy/pom.xml b/components/camel-groovy/pom.xml
index ed4abb2de48..6411e0cfff7 100644
--- a/components/camel-groovy/pom.xml
+++ b/components/camel-groovy/pom.xml
@@ -68,6 +68,11 @@
<type>test-jar</type>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-main</artifactId>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
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 659eb7f97ff..2ec12ed6a79 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
@@ -62,6 +62,11 @@ public class DefaultGroovyScriptCompiler extends ServiceSupport
return elapsed;
}
+ @ManagedAttribute(description = "Number of groovy sources that has been compiled")
+ public int getClassesSize() {
+ return classLoader.size();
+ }
+
@ManagedAttribute(description = "Directories to scan for groovy source to be pre-compiled")
@Override
public String getScriptPattern() {
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 330af11659d..094bbe0b215 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
@@ -35,6 +35,13 @@ public class GroovyScriptClassLoader extends ClassLoader implements Closeable {
classes.put(name, clazz);
}
+ /**
+ * Number of classes in this classloader
+ */
+ public int size() {
+ return classes.size();
+ }
+
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
Class<?> answer = classes.get(name);
diff --git a/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/MainGroovyCompilerRouteTest.java b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/MainGroovyCompilerRouteTest.java
new file mode 100644
index 00000000000..d8c5b7a7291
--- /dev/null
+++ b/components/camel-groovy/src/test/java/org/apache/camel/processor/groovy/MainGroovyCompilerRouteTest.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor.groovy;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.language.groovy.DefaultGroovyScriptCompiler;
+import org.apache.camel.main.Main;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class MainGroovyCompilerRouteTest {
+
+ @Test
+ public void testCompilerRoute() throws Exception {
+ Main main = new Main();
+ main.configure().addRoutesBuilder(createRouteBuilder());
+ main.configure().withGroovyScriptPattern("src/test/resources/myscript");
+ main.start();
+
+ CamelContext context = main.getCamelContext();
+
+ MockEndpoint mock = context.getEndpoint("mock:result", MockEndpoint.class);
+ mock.expectedBodiesReceived("I want to order 2 gauda");
+
+ context.createProducerTemplate().sendBodyAndHeader("direct:start", "Hello World", "amount", 2);
+
+ MockEndpoint.assertIsSatisfied(context);
+
+ DefaultGroovyScriptCompiler compiler = context.hasService(DefaultGroovyScriptCompiler.class);
+ Assertions.assertNotNull(compiler);
+ Assertions.assertEquals("src/test/resources/myscript", compiler.getScriptPattern());
+ Assertions.assertEquals(2, compiler.getClassesSize());
+ Assertions.assertTrue(compiler.getCompileTime() > 0, "Should take time to compile, was: " + compiler.getCompileTime());
+
+ main.stop();
+
+ }
+
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .setBody().groovy("""
+ Dude d = new Dude()
+ return d.order(header.amount)
+ """)
+ .to("mock:result");
+ }
+ };
+ }
+}