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

commit dcb48602cac822360e1392ef551e8dbb32534a16
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Mar 28 12:29:07 2022 +0200

    CAMEL-17784: ExtendedRoutesBuilderLoader SPI which allows routes loader to 
load multiple routes in the same unit. In preparation for camel-java-joor-dsl 
to compile all java sources together.
---
 .../apache/camel/main/DefaultRoutesCollector.java  | 46 +++++++++++-----------
 .../org/apache/camel/support/ResourceSupport.java  |  7 +++-
 .../dsl/java/joor/JavaRoutesBuilderLoader.java     | 12 +++++-
 3 files changed, 40 insertions(+), 25 deletions(-)

diff --git 
a/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java
 
b/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java
index 3684825..4035e30 100644
--- 
a/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java
+++ 
b/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java
@@ -140,45 +140,45 @@ public class DefaultRoutesCollector implements 
RoutesCollector {
         final String[] includes = includePattern != null ? 
includePattern.split(",") : null;
         final String[] excludes = excludePattern != null ? 
excludePattern.split(",") : null;
 
-        if (includes == null) {
-            log.debug("Include patter is empty, no routes will be discovered 
from resources");
+        if (includes == null || ObjectHelper.equal("false", includePattern)) {
+            log.debug("Include pattern is empty/false, no routes will be 
discovered from resources");
             return answer;
         }
 
         StopWatch watch = new StopWatch();
-
-        if (ObjectHelper.equal("false", includePattern)) {
-            return answer;
-        }
-
+        Collection<Resource> accepted = new ArrayList<>();
         for (String include : includes) {
             log.debug("Loading additional RoutesBuilder from: {}", include);
             try {
                 for (Resource resource : resolver.findResources(include)) {
+                    // filter unwanted resources
                     if (!"false".equals(excludePattern) && 
AntPathMatcher.INSTANCE.anyMatch(excludes, resource.getLocation())) {
                         continue;
                     }
-
-                    Collection<RoutesBuilder> builders = 
ecc.getRoutesLoader().findRoutesBuilders(resource);
-                    if (builders.isEmpty()) {
-                        continue;
-                    }
-
-                    log.debug("Found {} route builder from location: {}", 
builders.size(), include);
-                    answer.addAll(builders);
+                    accepted.add(resource);
                 }
-            } catch (FileNotFoundException e) {
-                log.debug("No RoutesBuilder found in {}. Skipping detection.", 
include);
             } catch (Exception e) {
                 throw RuntimeCamelException.wrapRuntimeException(e);
             }
-            if (!answer.isEmpty()) {
-                log.debug("Loaded {} ({} millis) additional RoutesBuilder 
from: {}, pattern: {}", answer.size(), watch.taken(),
-                        include,
-                        includePattern);
-            } else {
-                log.debug("No additional RoutesBuilder discovered from: {}", 
includePattern);
+        }
+
+        try {
+            Collection<RoutesBuilder> builders = 
ecc.getRoutesLoader().findRoutesBuilders(accepted);
+            if (!builders.isEmpty()) {
+                log.debug("Found {} route builder from locations: {}", 
builders.size(), includes);
+                answer.addAll(builders);
             }
+        } catch (FileNotFoundException e) {
+            log.debug("No RoutesBuilder found in {}. Skipping detection.", 
includes);
+        } catch (Exception e) {
+            throw RuntimeCamelException.wrapRuntimeException(e);
+        }
+        if (!answer.isEmpty()) {
+            log.debug("Loaded {} ({} millis) additional RoutesBuilder from: 
{}, pattern: {}", answer.size(), watch.taken(),
+                    includes,
+                    includePattern);
+        } else {
+            log.debug("No additional RoutesBuilder discovered from: {}", 
includePattern);
         }
 
         return answer;
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/ResourceSupport.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/ResourceSupport.java
index cbab7dd..5d49b00 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/ResourceSupport.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/ResourceSupport.java
@@ -42,6 +42,11 @@ public abstract class ResourceSupport implements Resource {
 
     @Override
     public String toString() {
-        return "Resource[" + scheme + ":" + location + "]";
+        String prefix = scheme + ":";
+        if (location.startsWith(prefix)) {
+            return "Resource[" + location + "]";
+        } else {
+            return "Resource[" + prefix + location + "]";
+        }
     }
 }
diff --git 
a/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
 
b/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
index a5d1d26..5ab24d6 100644
--- 
a/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
+++ 
b/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
@@ -36,14 +36,19 @@ import org.apache.camel.support.ResourceHelper;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
 import org.joor.Reflect;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 @ManagedResource(description = "Managed JavaRoutesBuilderLoader")
 @RoutesLoader(JavaRoutesBuilderLoader.EXTENSION)
 public class JavaRoutesBuilderLoader extends ExtendedRouteBuilderLoaderSupport 
{
+
     public static final String EXTENSION = "java";
     public static final Pattern PACKAGE_PATTERN = Pattern.compile(
             "^\\s*package\\s+([a-zA-Z][\\.\\w]*)\\s*;.*$", Pattern.MULTILINE);
 
+    private static final Logger LOG = 
LoggerFactory.getLogger(JavaRoutesBuilderLoader.class);
+
     public JavaRoutesBuilderLoader() {
         super(EXTENSION);
     }
@@ -52,7 +57,10 @@ public class JavaRoutesBuilderLoader extends 
ExtendedRouteBuilderLoaderSupport {
     protected Collection<RoutesBuilder> 
doLoadRoutesBuilders(Collection<Resource> resources) throws Exception {
         Collection<RoutesBuilder> answer = new ArrayList<>();
 
-        // TODO: when joor supports compiling in one unit
+        LOG.debug("Loading .java resources from: {}", resources);
+
+        // CAMEL-17784: joor to support compiling in one unit, then we can 
compile all resources at once
+
         for (Resource resource : resources) {
             try (InputStream is = resource.getInputStream()) {
                 if (is == null) {
@@ -61,9 +69,11 @@ public class JavaRoutesBuilderLoader extends 
ExtendedRouteBuilderLoaderSupport {
                 String content = IOHelper.loadText(is);
                 String name = determineName(resource, content);
 
+                LOG.debug("Compiling: {}", name);
                 Reflect ref = Reflect.compile(name, content).create();
                 Class<?> clazz = ref.type();
                 Object obj = ref.get();
+                LOG.debug("Compiled: {} -> {}", name, obj);
 
                 // inject context and resource
                 CamelContextAware.trySetCamelContext(obj, getCamelContext());

Reply via email to