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 <[email protected]> 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"); + } + }; + } +}
