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

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


The following commit(s) were added to refs/heads/main by this push:
     new 1bba9f0dd94 CAMEL-22255: Only include camel-groovy if there are groovy 
source files
1bba9f0dd94 is described below

commit 1bba9f0dd94dd035db192902f3ab5aaea9df9119
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Jul 21 17:44:13 2025 +0200

    CAMEL-22255: Only include camel-groovy if there are groovy source files
---
 .../groovy/DefaultGroovyScriptCompiler.java        |  6 ++++--
 .../org/apache/camel/ExtendedCamelContext.java     |  8 +++++++
 .../org/apache/camel/spi/GroovyScriptCompiler.java | 25 ++++++++++++++++++++++
 .../java/org/apache/camel/spi/PluginManager.java   |  8 +++++++
 .../impl/engine/DefaultCamelContextExtension.java  |  5 +++++
 .../impl/engine/DefaultContextPluginManager.java   |  7 ++++++
 .../camel/main/DefaultConfigurationConfigurer.java | 18 ++++++++++------
 7 files changed, 68 insertions(+), 9 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 9ce556ef9a5..d87fa83d107 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
@@ -195,8 +195,10 @@ public class DefaultGroovyScriptCompiler extends 
ServiceSupport
         if (scriptPattern != null) {
             LOG.debug("Loading Groovy sources from: {}", scriptPattern);
             doCompile(scanForGroovySources(scriptPattern), true);
-            LOG.info("Loaded Groovy sources from: {} (pre-loaded:{} 
compiled:{} time:{}ms)", scriptPattern,
-                    preloadCounter, counter, taken);
+            if (preloadCounter > 0 || counter > 0) {
+                LOG.info("Loaded Groovy sources from: {} (pre-loaded:{} 
compiled:{} time:{}ms)", scriptPattern,
+                        preloadCounter, counter, taken);
+            }
         }
     }
 
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java 
b/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java
index 4a458c5b44c..fe5dbaaf226 100644
--- a/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java
+++ b/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java
@@ -535,6 +535,14 @@ public interface ExtendedCamelContext {
      */
     <T> T getContextPlugin(Class<T> type);
 
+    /**
+     * Whether a plugin of the given type is already in use
+     *
+     * @param  type the type of the extension
+     * @return      true if already in use, false otherwise
+     */
+    boolean isContextPluginInUse(Class<?> type);
+
     /**
      * Allows installation of custom plugins to the Camel 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
index c9910dbc164..c234a7339ca 100644
--- 
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
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.spi;
 
+import org.apache.camel.CamelContext;
+
 /**
  * To let camel-groovy pre-compile script files during bootstrap.
  */
@@ -57,4 +59,27 @@ public interface GroovyScriptCompiler {
      * @throws Exception is thrown if compilation error
      */
     void recompile(Resource resource) throws Exception;
+
+    /**
+     * Checks whether any groovy sources exists?
+     *
+     * @param  context       the camel context
+     * @param  scriptPattern directories to scan
+     * @return               true if any resources exists, false otherwise
+     */
+    static boolean existsSourceFiles(CamelContext context, String 
scriptPattern) throws Exception {
+        boolean exists = false;
+        PackageScanResourceResolver resolver
+                = 
context.getCamelContextExtension().getContextPlugin(PackageScanResourceResolver.class);
+        for (String pattern : scriptPattern.split(",")) {
+            // include all kind of resources
+            for (Resource resource : resolver.findResources(pattern, n -> 
true)) {
+                if (resource.exists()) {
+                    exists = true;
+                    break;
+                }
+            }
+        }
+        return exists;
+    }
 }
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/spi/PluginManager.java 
b/core/camel-api/src/main/java/org/apache/camel/spi/PluginManager.java
index 67d61596ca8..8cbd505a87b 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/PluginManager.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/PluginManager.java
@@ -31,6 +31,14 @@ public interface PluginManager {
      */
     <T> T getContextPlugin(Class<T> type);
 
+    /**
+     * Whether a plugin of the given type is already in use
+     *
+     * @param  type the type of the extension
+     * @return      true if already in use, false otherwise
+     */
+    boolean isContextPluginInUse(Class<?> type);
+
     /**
      * Allows installation of custom plugins to the Camel context.
      *
diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelContextExtension.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelContextExtension.java
index 631bfbd2d03..2ece8945036 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelContextExtension.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelContextExtension.java
@@ -1113,6 +1113,11 @@ class DefaultCamelContextExtension implements 
ExtendedCamelContext {
         return ret;
     }
 
+    @Override
+    public boolean isContextPluginInUse(Class<?> type) {
+        return pluginManager.isContextPluginInUse(type);
+    }
+
     @Override
     public <T> void addContextPlugin(Class<T> type, T module) {
         final T addedModule = 
camelContext.getInternalServiceManager().addService(camelContext, module);
diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextPluginManager.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextPluginManager.java
index 89998e9ecc1..e1d3181ba35 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextPluginManager.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextPluginManager.java
@@ -46,6 +46,13 @@ public class DefaultContextPluginManager implements 
PluginManager {
         return (T) extension;
     }
 
+    @Override
+    public boolean isContextPluginInUse(Class<?> type) {
+        Object extension = extensions.get(type);
+        // a lazy plugin should not be regarded as true
+        return extension != null && (!(extension instanceof Supplier));
+    }
+
     @Override
     public <T> void addContextPlugin(Class<T> type, T module) {
         if (module != null) {
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 cac1a5ec8f8..e9e994d41f7 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
@@ -353,13 +353,17 @@ public final class DefaultConfigurationConfigurer {
             cs.setWorkDir(config.getCompileWorkDir());
         }
         if (config.getGroovyScriptPattern() != null) {
-            GroovyScriptCompiler gsc = 
camelContext.getCamelContextExtension().getContextPlugin(GroovyScriptCompiler.class);
-            if (gsc != null) {
-                gsc.setScriptPattern(config.getGroovyScriptPattern());
-                gsc.setPreloadCompiled(config.isGroovyPreloadCompiled());
-                camelContext.addService(gsc);
-                // force start compiler eager so Camel routes can load these 
pre-compiled classes
-                ServiceHelper.startService(gsc);
+            // check if there is any groovy sources before demanding the 
GroovyScriptCompiler plugin (which is in camel-groovy JAR)
+            boolean exists = 
GroovyScriptCompiler.existsSourceFiles(camelContext, 
config.getGroovyScriptPattern());
+            if (exists || 
camelContext.getCamelContextExtension().isContextPluginInUse(GroovyScriptCompiler.class))
 {
+                GroovyScriptCompiler gsc = 
camelContext.getCamelContextExtension().getContextPlugin(GroovyScriptCompiler.class);
+                if (gsc != null) {
+                    gsc.setScriptPattern(config.getGroovyScriptPattern());
+                    gsc.setPreloadCompiled(config.isGroovyPreloadCompiled());
+                    camelContext.addService(gsc);
+                    // force start compiler eager so Camel routes can load 
these pre-compiled classes
+                    ServiceHelper.startService(gsc);
+                }
             }
         }
 

Reply via email to