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 2022/03/30 07:40:52 UTC
[camel] branch main updated: CAMEL-17863: camel-main - Is loading routes from directory twice due to early modeline detection
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 7017aab CAMEL-17863: camel-main - Is loading routes from directory twice due to early modeline detection
7017aab is described below
commit 7017aab1e9d4dd1b2b2dfa9678948fc2a6dd936e
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Mar 30 09:39:30 2022 +0200
CAMEL-17863: camel-main - Is loading routes from directory twice due to early modeline detection
---
.../apache/camel/main/DefaultRoutesCollector.java | 58 +++++++++++++---------
.../org/apache/camel/main/RoutesCollector.java | 12 +++++
.../org/apache/camel/main/RoutesConfigurer.java | 40 ++++++---------
3 files changed, 61 insertions(+), 49 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 4035e30..e1d1c4a 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
@@ -135,33 +135,11 @@ public class DefaultRoutesCollector implements RoutesCollector {
String includePattern) {
final ExtendedCamelContext ecc = camelContext.adapt(ExtendedCamelContext.class);
- final PackageScanResourceResolver resolver = ecc.getPackageScanResourceResolver();
final List<RoutesBuilder> answer = new ArrayList<>();
final String[] includes = includePattern != null ? includePattern.split(",") : null;
- final String[] excludes = excludePattern != null ? excludePattern.split(",") : null;
-
- 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();
- 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;
- }
- accepted.add(resource);
- }
- } catch (Exception e) {
- throw RuntimeCamelException.wrapRuntimeException(e);
- }
- }
-
+ Collection<Resource> accepted = findRouteResourcesFromDirectory(camelContext, excludePattern, includePattern);
try {
Collection<RoutesBuilder> builders = ecc.getRoutesLoader().findRoutesBuilders(accepted);
if (!builders.isEmpty()) {
@@ -183,4 +161,38 @@ public class DefaultRoutesCollector implements RoutesCollector {
return answer;
}
+
+ @Override
+ public Collection<Resource> findRouteResourcesFromDirectory(
+ CamelContext camelContext,
+ String excludePattern,
+ String includePattern) {
+ final ExtendedCamelContext ecc = camelContext.adapt(ExtendedCamelContext.class);
+ final PackageScanResourceResolver resolver = ecc.getPackageScanResourceResolver();
+ final String[] includes = includePattern != null ? includePattern.split(",") : null;
+ final String[] excludes = excludePattern != null ? excludePattern.split(",") : null;
+
+ if (includes == null || ObjectHelper.equal("false", includePattern)) {
+ log.debug("Include pattern is empty/false, no routes will be discovered from resources");
+ return new ArrayList<>();
+ }
+
+ Collection<Resource> accepted = new ArrayList<>();
+ for (String include : includes) {
+ log.debug("Finding additional routes from: {}", include);
+ try {
+ for (Resource resource : resolver.findResources(include)) {
+ // filter unwanted resources
+ if (!"false".equals(excludePattern) && AntPathMatcher.INSTANCE.anyMatch(excludes, resource.getLocation())) {
+ continue;
+ }
+ accepted.add(resource);
+ }
+ } catch (Exception e) {
+ throw RuntimeCamelException.wrapRuntimeException(e);
+ }
+ }
+
+ return accepted;
+ }
}
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/RoutesCollector.java b/core/camel-main/src/main/java/org/apache/camel/main/RoutesCollector.java
index 3d20e97..e7954d6 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/RoutesCollector.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/RoutesCollector.java
@@ -20,6 +20,7 @@ import java.util.Collection;
import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder;
+import org.apache.camel.spi.Resource;
/**
* Collects routes and rests from the various sources (like registry or opinionated classpath locations) and adds these
@@ -50,4 +51,15 @@ public interface RoutesCollector {
Collection<RoutesBuilder> collectRoutesFromDirectory(
CamelContext camelContext, String excludePattern, String includePattern);
+ /**
+ * Finds all routes as {@link Resource} from the given directory.
+ *
+ * @param camelContext the Camel Context
+ * @param excludePattern exclude pattern (see routesExcludePattern option)
+ * @param includePattern include pattern (see routesIncludePattern option)
+ * @return the discovered routes as {@link Resource} or an empty collection
+ */
+ Collection<Resource> findRouteResourcesFromDirectory(
+ CamelContext camelContext, String excludePattern, String includePattern);
+
}
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/RoutesConfigurer.java b/core/camel-main/src/main/java/org/apache/camel/main/RoutesConfigurer.java
index e05a0af..430dbab 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/RoutesConfigurer.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/RoutesConfigurer.java
@@ -26,7 +26,6 @@ import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.RouteConfigurationsBuilder;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.RuntimeCamelException;
-import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spi.CamelBeanPostProcessor;
import org.apache.camel.spi.ModelineFactory;
import org.apache.camel.spi.Resource;
@@ -246,38 +245,27 @@ public class RoutesConfigurer {
* @param camelContext the Camel context
*/
public void configureModeline(CamelContext camelContext) throws Exception {
- final List<RoutesBuilder> routes = new ArrayList<>();
+ if (getRoutesCollector() == null) {
+ return;
+ }
- if (getRoutesCollector() != null) {
- try {
- LOG.debug("RoutesCollectorEnabled: {}", getRoutesCollector());
+ Collection<Resource> resources;
+ try {
+ LOG.debug("RoutesCollectorEnabled: {}", getRoutesCollector());
- // we can only scan for modeline for routes that we can load from directory as modelines
- // are comments in the source files
- Collection<RoutesBuilder> routesFromDirectory = getRoutesCollector().collectRoutesFromDirectory(
- camelContext,
- getRoutesExcludePattern(),
- getRoutesIncludePattern());
- routes.addAll(routesFromDirectory);
+ // we can only scan for modeline for routes that we can load from directory as modelines
+ // are comments in the source files
+ resources = getRoutesCollector().findRouteResourcesFromDirectory(
+ camelContext,
+ getRoutesExcludePattern(),
+ getRoutesIncludePattern());
- } catch (Exception e) {
- throw RuntimeCamelException.wrapRuntimeException(e);
- }
+ } catch (Exception e) {
+ throw RuntimeCamelException.wrapRuntimeException(e);
}
- // sort routes according to ordered
- routes.sort(OrderedComparator.get());
-
ExtendedCamelContext ecc = camelContext.adapt(ExtendedCamelContext.class);
ModelineFactory factory = ecc.getModelineFactory();
- List<Resource> resources = new ArrayList<>();
- // gather resources for modeline
- for (RoutesBuilder builder : routes) {
- if (builder instanceof RouteBuilder) {
- resources.add(((RouteBuilder) builder).getResource());
- }
- }
- LOG.debug("Discovered {} resources with potential modeline", resources.size());
for (Resource resource : resources) {
LOG.debug("Parsing modeline: {}", resource);