You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2021/02/03 16:45:24 UTC

[camel-spring-boot] branch master updated: CAMEL-15560: generic route loader

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

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git


The following commit(s) were added to refs/heads/master by this push:
     new ac4c74c  CAMEL-15560: generic route loader
ac4c74c is described below

commit ac4c74c77bf023d9d013d64b3056b40f44d762ed
Author: Luca Burgazzoli <lb...@gmail.com>
AuthorDate: Wed Feb 3 17:42:07 2021 +0100

    CAMEL-15560: generic route loader
---
 .../src/main/docs/spring-boot.adoc                 |  7 +-
 .../boot/CamelSpringBootApplicationListener.java   | 34 ++++----
 .../spring/boot/SpringBootRoutesCollector.java     | 92 ----------------------
 .../camel/spring/boot/CamelXmlRoutesTest.java      |  2 +-
 .../boot/SupervisingRouteControllerTest.java       |  3 +-
 docs/modules/ROOT/pages/spring-boot.adoc           |  7 +-
 6 files changed, 27 insertions(+), 118 deletions(-)

diff --git a/core/camel-spring-boot/src/main/docs/spring-boot.adoc b/core/camel-spring-boot/src/main/docs/spring-boot.adoc
index 03eb4ad..375e2d9 100644
--- a/core/camel-spring-boot/src/main/docs/spring-boot.adoc
+++ b/core/camel-spring-boot/src/main/docs/spring-boot.adoc
@@ -92,7 +92,7 @@ When using spring-boot with Spring Boot make sure to use the following Maven dep
 ----
 
 
-The component supports 166 options, which are listed below.
+The component supports 165 options, which are listed below.
 
 
 
@@ -194,6 +194,8 @@ The component supports 166 options, which are listed below.
 | *camel.springboot.route-filter-exclude-pattern* | Used for filtering routes routes matching the given pattern, which follows the following rules: - Match by route id - Match by route input endpoint uri The matching is using exact match, by wildcard and regular expression. For example to only include routes which starts with foo in their route id's, use: include=foo&#42; And to exclude routes which starts from JMS endpoints, use: exclude=jms:&#42; Multiple patterns can be separated by c [...]
 | *camel.springboot.route-filter-include-pattern* | Used for filtering routes matching the given pattern, which follows the following rules: - Match by route id - Match by route input endpoint uri The matching is using exact match, by wildcard and regular expression. For example to only include routes which starts with foo in their route id's, use: include=foo&#42; And to exclude routes which starts from JMS endpoints, use: exclude=jms:&#42; Multiple patterns can be separated by comma, f [...]
 | *camel.springboot.routes-collector-enabled* | Whether the routes collector is enabled or not. When enabled Camel will auto-discover routes (RouteBuilder instances from the registry and also load additional XML routes from the file system. The routes collector is default enabled. | true | Boolean
+| *camel.springboot.routes-exclude-pattern* |  |  | String
+| *camel.springboot.routes-include-pattern* |  |  | String
 | *camel.springboot.shutdown-log-inflight-exchanges-on-timeout* | Sets whether to log information about the inflight Exchanges which are still running during a shutdown which didn't complete without the given timeout. This requires to enable the option inflightRepositoryExchangeEnabled. | true | Boolean
 | *camel.springboot.shutdown-now-on-timeout* | Sets whether to force shutdown of all consumers when a timeout occurred and thus not all consumers was shutdown within that period. You should have good reasons to set this option to false as it means that the routes keep running and is halted abruptly when CamelContext has been shutdown. | true | Boolean
 | *camel.springboot.shutdown-routes-in-reverse-order* | Sets whether routes should be shutdown in reverse or the same order as they where started. | true | Boolean
@@ -223,9 +225,6 @@ The component supports 166 options, which are listed below.
 | *camel.springboot.use-data-type* | Whether to enable using data type on Camel messages. Data type are automatic turned on if one or more routes has been explicit configured with input and output types. Otherwise data type is default off. | false | Boolean
 | *camel.springboot.use-mdc-logging* | To turn on MDC logging | false | Boolean
 | *camel.springboot.warn-on-early-shutdown* | Whether to log a WARN if Camel on Spring Boot was immediately shutdown after starting which very likely is because there is no JVM thread to keep the application running. | true | Boolean
-| *camel.springboot.xml-rests* | Directory to scan for adding additional XML rests. You can turn this off by setting the value to false. Files can be loaded from either classpath or file by prefixing with classpath: or file: Wildcards is supported using an ANT pattern style paths, such as classpath:&#42;&#42;/&#42;camel&#42;.xml Multiple directories can be specified and separated by comma, such as: file:/myapp/mycamel/&#42;.xml,file:/myapp/myothercamel/&#42;.xml | classpath:camel-rest/*. [...]
-| *camel.springboot.xml-route-templates* | Directory to scan for adding additional XML route templates. You can turn this off by setting the value to false. Files can be loaded from either classpath or file by prefixing with classpath: or file: Wildcards is supported using an ANT pattern style paths, such as classpath:&#42;&#42;/&#42;template-&#42;.xml Notice when using wildcards, then there is additional overhead as the classpath is scanned, where as if you specific the exact name for e [...]
-| *camel.springboot.xml-routes* | Directory to scan for adding additional XML routes. You can turn this off by setting the value to false. Files can be loaded from either classpath or file by prefixing with classpath: or file: Wildcards is supported using an ANT pattern style paths, such as classpath:&#42;&#42;/&#42;camel&#42;.xml Multiple directories can be specified and separated by comma, such as: file:/myapp/mycamel/&#42;.xml,file:/myapp/myothercamel/&#42;.xml | classpath:camel/*.xml [...]
 | *camel.ssl.cert-alias* | An optional certificate alias to use. This is useful when the keystore has multiple certificates. |  | String
 | *camel.ssl.cipher-suites* | The optional explicitly configured cipher suites for this configuration. |  | CipherSuitesParameters
 | *camel.ssl.cipher-suites-filter* | The optional cipher suite filter configuration for this configuration. |  | FilterParameters
diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java
index ba06952..155b58b 100644
--- a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java
+++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java
@@ -26,6 +26,7 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.StartupListener;
 import org.apache.camel.main.MainDurationEventNotifier;
 import org.apache.camel.main.MainShutdownStrategy;
@@ -90,23 +91,26 @@ public class CamelSpringBootApplicationListener implements ApplicationListener<C
             && camelContext.getStatus().isStopped()) {
             LOG.debug("Post-processing CamelContext bean: {}", camelContext.getName());
 
-            // we can use the default routes configurer
-            RoutesConfigurer configurer = new RoutesConfigurer(springBootRoutesCollector);
-            // if camel context is NOT from spring boot then its from camel-spring XML DSL
-            // and if so then it has its own special handling of collected rests
-            // that are added later as routes so turn this off
-            boolean addRestsToRoutes = camelContext instanceof SpringBootCamelContext;
-            if (!addRestsToRoutes) {
-                configurer.setAddRestsToRoutes(false);
-            }
-            configurer.configureRoutes(camelContext, configurationProperties);
+            try {
+                // we can use the default routes configurer
+                RoutesConfigurer configurer = new RoutesConfigurer();
 
-            for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
-                LOG.debug("CamelContextConfiguration found. Invoking beforeApplicationStart: {}", camelContextConfiguration);
-                camelContextConfiguration.beforeApplicationStart(camelContext);
-            }
+                if (configurationProperties.isRoutesCollectorEnabled()) {
+                    configurer.setRoutesCollector(springBootRoutesCollector);
+                }
+
+                configurer.setBeanPostProcessor(camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor());
+                configurer.setJavaRoutesExcludePattern(configurationProperties.getJavaRoutesExcludePattern());
+                configurer.setJavaRoutesIncludePattern(configurationProperties.getJavaRoutesIncludePattern());
+                configurer.setRoutesExcludePattern(configurationProperties.getRoutesExcludePattern());
+                configurer.setRoutesIncludePattern(configurationProperties.getRoutesIncludePattern());
+                configurer.configureRoutes(camelContext);
+
+                for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
+                    LOG.debug("CamelContextConfiguration found. Invoking beforeApplicationStart: {}", camelContextConfiguration);
+                    camelContextConfiguration.beforeApplicationStart(camelContext);
+                }
 
-            try {
                 if (configurationProperties.isMainRunController()) {
                     CamelMainRunController controller = new CamelMainRunController(applicationContext, camelContext);
 
diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootRoutesCollector.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootRoutesCollector.java
index 982c1fd..12ce738 100644
--- a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootRoutesCollector.java
+++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootRoutesCollector.java
@@ -16,27 +16,19 @@
  */
 package org.apache.camel.spring.boot;
 
-import java.io.FileNotFoundException;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.RoutesBuilder;
-import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.builder.LambdaRouteBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.main.DefaultRoutesCollector;
-import org.apache.camel.model.RouteTemplatesDefinition;
-import org.apache.camel.model.RoutesDefinition;
-import org.apache.camel.model.rest.RestsDefinition;
-import org.apache.camel.spi.XMLRoutesDefinitionLoader;
 import org.apache.camel.util.AntPathMatcher;
 import org.apache.camel.util.ObjectHelper;
 import org.springframework.context.ApplicationContext;
-import org.springframework.core.io.Resource;
 
 /**
  * Spring Boot {@link org.apache.camel.main.RoutesCollector}.
@@ -125,88 +117,4 @@ public class SpringBootRoutesCollector extends DefaultRoutesCollector {
 
         return routes;
     }
-
-    @Override
-    public List<RoutesDefinition> collectXmlRoutesFromDirectory(CamelContext camelContext, String directory) {
-        List<RoutesDefinition> answer = new ArrayList<>();
-
-        String[] parts = directory.split(",");
-        for (String part : parts) {
-            log.debug("Loading additional Camel XML routes from: {}", part);
-            try {
-                Resource[] xmlRoutes = applicationContext.getResources(part);
-                for (Resource xmlRoute : xmlRoutes) {
-                    log.debug("Found XML route: {}", xmlRoute);
-                    ExtendedCamelContext extendedCamelContext = camelContext.adapt(ExtendedCamelContext.class);
-                    XMLRoutesDefinitionLoader xmlLoader = extendedCamelContext.getXMLRoutesDefinitionLoader();
-                    RoutesDefinition routes = (RoutesDefinition) xmlLoader.loadRoutesDefinition(camelContext, xmlRoute.getInputStream());
-                    if (routes != null) {
-                        answer.add(routes);
-                    }
-                }
-            } catch (FileNotFoundException e) {
-                log.debug("No XML routes found in {}. Skipping XML routes detection.", part);
-            } catch (Exception e) {
-                throw RuntimeCamelException.wrapRuntimeException(e);
-            }
-        }
-
-        return answer;
-    }
-
-    @Override
-    public List<RouteTemplatesDefinition> collectXmlRouteTemplatesFromDirectory(CamelContext camelContext, String directory) throws Exception {
-        List<RouteTemplatesDefinition> answer = new ArrayList<>();
-
-        String[] parts = directory.split(",");
-        for (String part : parts) {
-            log.debug("Loading additional Camel XML route templates from: {}", part);
-            try {
-                Resource[] xmlRouteTemplates = applicationContext.getResources(part);
-                for (Resource xmlRoute : xmlRouteTemplates) {
-                    log.debug("Found XML route template: {}", xmlRoute);
-                    ExtendedCamelContext extendedCamelContext = camelContext.adapt(ExtendedCamelContext.class);
-                    XMLRoutesDefinitionLoader xmlLoader = extendedCamelContext.getXMLRoutesDefinitionLoader();
-                    RouteTemplatesDefinition templates = (RouteTemplatesDefinition) xmlLoader.loadRouteTemplatesDefinition(camelContext, xmlRoute.getInputStream());
-                    if (templates != null) {
-                        answer.add(templates);
-                    }
-                }
-            } catch (FileNotFoundException e) {
-                log.debug("No XML route templates found in {}. Skipping XML route templates detection.", part);
-            } catch (Exception e) {
-                throw RuntimeCamelException.wrapRuntimeException(e);
-            }
-        }
-
-        return answer;
-    }
-
-    @Override
-    public List<RestsDefinition> collectXmlRestsFromDirectory(CamelContext camelContext, String directory) {
-        List<RestsDefinition> answer = new ArrayList<>();
-
-        String[] parts = directory.split(",");
-        for (String part : parts) {
-            log.debug("Loading additional Camel XML rests from: {}", part);
-            try {
-                final Resource[] xmlRests = applicationContext.getResources(part);
-                for (final Resource xmlRest : xmlRests) {
-                    ExtendedCamelContext extendedCamelContext = camelContext.adapt(ExtendedCamelContext.class);
-                    XMLRoutesDefinitionLoader xmlLoader = extendedCamelContext.getXMLRoutesDefinitionLoader();
-                    RestsDefinition rests = (RestsDefinition) xmlLoader.loadRestsDefinition(camelContext, xmlRest.getInputStream());
-                    if (rests != null) {
-                        answer.add(rests);
-                    }
-                }
-            } catch (FileNotFoundException e) {
-                log.debug("No XML rests found in {}. Skipping XML rests detection.", part);
-            } catch (Exception e) {
-                throw RuntimeCamelException.wrapRuntimeException(e);
-            }
-        }
-
-        return answer;
-    }
-
 }
diff --git a/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelXmlRoutesTest.java b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelXmlRoutesTest.java
index 3f3f1c6..558a00c 100644
--- a/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelXmlRoutesTest.java
+++ b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelXmlRoutesTest.java
@@ -35,7 +35,7 @@ import org.springframework.test.context.junit4.SpringRunner;
         CamelXmlRoutesTest.class,
         RouteConfigWithCamelContextInjected.class },
     properties = {
-        "camel.springboot.xml-routes=file:src/test/resources/routes/foo.xml,file:src/test/resources/routes/bar.xml"}
+        "camel.springboot.routes-include-pattern=file:src/test/resources/routes/foo.xml,file:src/test/resources/routes/bar.xml"}
 )
 public class CamelXmlRoutesTest extends Assert {
 
diff --git a/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SupervisingRouteControllerTest.java b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SupervisingRouteControllerTest.java
index 38594ed..f1fb9a9 100644
--- a/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SupervisingRouteControllerTest.java
+++ b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SupervisingRouteControllerTest.java
@@ -43,8 +43,7 @@ import static org.awaitility.Awaitility.await;
         SupervisingRouteControllerTest.TestConfiguration.class
     },
     properties = {
-        "camel.springboot.xml-routes = false",
-        "camel.springboot.xml-rests = false",
+        "camel.springboot.routes-include-pattern = false",
         "camel.springboot.main-run-controller = true",
         "camel.springboot.routeControllerSuperviseEnabled = true",
         "camel.springboot.routeControllerInitialDelay = 500",
diff --git a/docs/modules/ROOT/pages/spring-boot.adoc b/docs/modules/ROOT/pages/spring-boot.adoc
index 03eb4ad..375e2d9 100644
--- a/docs/modules/ROOT/pages/spring-boot.adoc
+++ b/docs/modules/ROOT/pages/spring-boot.adoc
@@ -92,7 +92,7 @@ When using spring-boot with Spring Boot make sure to use the following Maven dep
 ----
 
 
-The component supports 166 options, which are listed below.
+The component supports 165 options, which are listed below.
 
 
 
@@ -194,6 +194,8 @@ The component supports 166 options, which are listed below.
 | *camel.springboot.route-filter-exclude-pattern* | Used for filtering routes routes matching the given pattern, which follows the following rules: - Match by route id - Match by route input endpoint uri The matching is using exact match, by wildcard and regular expression. For example to only include routes which starts with foo in their route id's, use: include=foo&#42; And to exclude routes which starts from JMS endpoints, use: exclude=jms:&#42; Multiple patterns can be separated by c [...]
 | *camel.springboot.route-filter-include-pattern* | Used for filtering routes matching the given pattern, which follows the following rules: - Match by route id - Match by route input endpoint uri The matching is using exact match, by wildcard and regular expression. For example to only include routes which starts with foo in their route id's, use: include=foo&#42; And to exclude routes which starts from JMS endpoints, use: exclude=jms:&#42; Multiple patterns can be separated by comma, f [...]
 | *camel.springboot.routes-collector-enabled* | Whether the routes collector is enabled or not. When enabled Camel will auto-discover routes (RouteBuilder instances from the registry and also load additional XML routes from the file system. The routes collector is default enabled. | true | Boolean
+| *camel.springboot.routes-exclude-pattern* |  |  | String
+| *camel.springboot.routes-include-pattern* |  |  | String
 | *camel.springboot.shutdown-log-inflight-exchanges-on-timeout* | Sets whether to log information about the inflight Exchanges which are still running during a shutdown which didn't complete without the given timeout. This requires to enable the option inflightRepositoryExchangeEnabled. | true | Boolean
 | *camel.springboot.shutdown-now-on-timeout* | Sets whether to force shutdown of all consumers when a timeout occurred and thus not all consumers was shutdown within that period. You should have good reasons to set this option to false as it means that the routes keep running and is halted abruptly when CamelContext has been shutdown. | true | Boolean
 | *camel.springboot.shutdown-routes-in-reverse-order* | Sets whether routes should be shutdown in reverse or the same order as they where started. | true | Boolean
@@ -223,9 +225,6 @@ The component supports 166 options, which are listed below.
 | *camel.springboot.use-data-type* | Whether to enable using data type on Camel messages. Data type are automatic turned on if one or more routes has been explicit configured with input and output types. Otherwise data type is default off. | false | Boolean
 | *camel.springboot.use-mdc-logging* | To turn on MDC logging | false | Boolean
 | *camel.springboot.warn-on-early-shutdown* | Whether to log a WARN if Camel on Spring Boot was immediately shutdown after starting which very likely is because there is no JVM thread to keep the application running. | true | Boolean
-| *camel.springboot.xml-rests* | Directory to scan for adding additional XML rests. You can turn this off by setting the value to false. Files can be loaded from either classpath or file by prefixing with classpath: or file: Wildcards is supported using an ANT pattern style paths, such as classpath:&#42;&#42;/&#42;camel&#42;.xml Multiple directories can be specified and separated by comma, such as: file:/myapp/mycamel/&#42;.xml,file:/myapp/myothercamel/&#42;.xml | classpath:camel-rest/*. [...]
-| *camel.springboot.xml-route-templates* | Directory to scan for adding additional XML route templates. You can turn this off by setting the value to false. Files can be loaded from either classpath or file by prefixing with classpath: or file: Wildcards is supported using an ANT pattern style paths, such as classpath:&#42;&#42;/&#42;template-&#42;.xml Notice when using wildcards, then there is additional overhead as the classpath is scanned, where as if you specific the exact name for e [...]
-| *camel.springboot.xml-routes* | Directory to scan for adding additional XML routes. You can turn this off by setting the value to false. Files can be loaded from either classpath or file by prefixing with classpath: or file: Wildcards is supported using an ANT pattern style paths, such as classpath:&#42;&#42;/&#42;camel&#42;.xml Multiple directories can be specified and separated by comma, such as: file:/myapp/mycamel/&#42;.xml,file:/myapp/myothercamel/&#42;.xml | classpath:camel/*.xml [...]
 | *camel.ssl.cert-alias* | An optional certificate alias to use. This is useful when the keystore has multiple certificates. |  | String
 | *camel.ssl.cipher-suites* | The optional explicitly configured cipher suites for this configuration. |  | CipherSuitesParameters
 | *camel.ssl.cipher-suites-filter* | The optional cipher suite filter configuration for this configuration. |  | FilterParameters