From 3b1709f7d3c26ce45a0ce328b56c6d5af2217290 Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Thu, 20 Dec 2018 12:56:41 +0100 Subject: [PATCH] CAMEL-13015: camel-spring-boot - xml routes/rests can now load from multiple paths separated by comma. --- .../src/main/docs/spring-boot.adoc | 4 +- .../boot/CamelConfigurationProperties.java | 12 ++++ .../camel/spring/boot/RoutesCollector.java | 50 ++++++++------- .../camel/spring/boot/CamelXmlRoutesTest.java | 62 +++++++++++++++++++ .../src/test/resources/routes/bar.xml | 25 ++++++++ .../src/test/resources/routes/foo.xml | 25 ++++++++ 6 files changed, 153 insertions(+), 25 deletions(-) create mode 100644 components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelXmlRoutesTest.java create mode 100644 components/camel-spring-boot/src/test/resources/routes/bar.xml create mode 100644 components/camel-spring-boot/src/test/resources/routes/foo.xml diff --git a/components/camel-spring-boot/src/main/docs/spring-boot.adoc b/components/camel-spring-boot/src/main/docs/spring-boot.adoc index 2b59a25ef332e..8b5c906c4104b 100644 --- a/components/camel-spring-boot/src/main/docs/spring-boot.adoc +++ b/components/camel-spring-boot/src/main/docs/spring-boot.adoc @@ -192,8 +192,8 @@ The component supports 139 options, which are listed below. | *camel.springboot.use-breadcrumb* | Set whether breadcrumb is enabled. The default value is true. | true | Boolean | *camel.springboot.use-data-type* | Whether to enable using data type on Camel messages. Data type are automatic turned on if one ore 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.xml-rests* | Directory to scan for adding additional XML rests. You can turn this off by setting the value to false. | classpath:camel-rest/*.xml | String -| *camel.springboot.xml-routes* | Directory to scan for adding additional XML routes. You can turn this off by setting the value to false. | classpath:camel/*.xml | String +| *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 a ANT pattern style paths, such as classpath:**/*camel*.xml Multiple directories can be specified and separated by comma, such as: file:/myapp/mycamel/*.xml,file:/myapp/myothercamel/*.xml | classpath:camel-rest/*.xml | String +| *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 a ANT pattern style paths, such as classpath:**/*camel*.xml Multiple directories can be specified and separated by comma, such as: file:/myapp/mycamel/*.xml,file:/myapp/myothercamel/*.xml | classpath:camel/*.xml | String | *camel.springboot.xml-routes-reload-directory* | To watch the directory for file changes which triggers a live reload of the Camel routes on-the-fly. For example configure this to point to the source code where the Camel XML files are located such as: src/main/resources/camel/ | | String | *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 diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelConfigurationProperties.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelConfigurationProperties.java index b69ff0b239c34..8be5484eecfac 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelConfigurationProperties.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelConfigurationProperties.java @@ -122,12 +122,24 @@ public class CamelConfigurationProperties { /** * 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 a ANT pattern style paths, such as classpath:**/*camel*.xml + * + * Multiple directories can be specified and separated by comma, such as: + * file:/myapp/mycamel/*.xml,file:/myapp/myothercamel/*.xml */ private String xmlRoutes = "classpath:camel/*.xml"; /** * 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 a ANT pattern style paths, such as classpath:**/*camel*.xml + * + * Multiple directories can be specified and separated by comma, such as: + * file:/myapp/mycamel/*.xml,file:/myapp/myothercamel/*.xml */ private String xmlRests = "classpath:camel-rest/*.xml"; diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java index 04f5866b1bb33..5e8ff21bed1c0 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java @@ -275,35 +275,39 @@ public int getOrder() { // Helpers private void loadXmlRoutes(ApplicationContext applicationContext, CamelContext camelContext, String directory) throws Exception { - LOG.info("Loading additional Camel XML routes from: {}", directory); - try { - Resource[] xmlRoutes = applicationContext.getResources(directory); - for (Resource xmlRoute : xmlRoutes) { - LOG.debug("Found XML route: {}", xmlRoute); - RoutesDefinition xmlDefinition = camelContext.adapt(ModelCamelContext.class).loadRoutesDefinition(xmlRoute.getInputStream()); - camelContext.adapt(ModelCamelContext.class).addRouteDefinitions(xmlDefinition.getRoutes()); + String[] parts = directory.split(","); + for (String part : parts) { + LOG.info("Loading additional Camel XML routes from: {}", part); + try { + Resource[] xmlRoutes = applicationContext.getResources(part); + for (Resource xmlRoute : xmlRoutes) { + LOG.debug("Found XML route: {}", xmlRoute); + RoutesDefinition xmlDefinition = camelContext.adapt(ModelCamelContext.class).loadRoutesDefinition(xmlRoute.getInputStream()); + camelContext.adapt(ModelCamelContext.class).addRouteDefinitions(xmlDefinition.getRoutes()); + } + } catch (FileNotFoundException e) { + LOG.debug("No XML routes found in {}. Skipping XML routes detection.", part); } - } catch (FileNotFoundException e) { - LOG.debug("No XML routes found in {}. Skipping XML routes detection.", directory); } } - private void loadXmlRests(ApplicationContext applicationContext, CamelContext camelContext, String directory) { - LOG.info("Loading additional Camel XML rests from: {}", directory); - try { - final Resource[] xmlRests = applicationContext.getResources(directory); - for (final Resource xmlRest : xmlRests) { - final RestsDefinition xmlDefinitions = camelContext.adapt(ModelCamelContext.class).loadRestsDefinition(xmlRest.getInputStream()); - camelContext.adapt(ModelCamelContext.class).addRestDefinitions(xmlDefinitions.getRests()); - for (final RestDefinition xmlDefinition : xmlDefinitions.getRests()) { - final List routeDefinitions = xmlDefinition.asRouteDefinition(camelContext); - camelContext.adapt(ModelCamelContext.class).addRouteDefinitions(routeDefinitions); + private void loadXmlRests(ApplicationContext applicationContext, CamelContext camelContext, String directory) throws Exception { + String[] parts = directory.split(","); + for (String part : parts) { + LOG.info("Loading additional Camel XML rests from: {}", part); + try { + final Resource[] xmlRests = applicationContext.getResources(part); + for (final Resource xmlRest : xmlRests) { + final RestsDefinition xmlDefinitions = camelContext.adapt(ModelCamelContext.class).loadRestsDefinition(xmlRest.getInputStream()); + camelContext.adapt(ModelCamelContext.class).addRestDefinitions(xmlDefinitions.getRests()); + for (final RestDefinition xmlDefinition : xmlDefinitions.getRests()) { + final List routeDefinitions = xmlDefinition.asRouteDefinition(camelContext); + camelContext.adapt(ModelCamelContext.class).addRouteDefinitions(routeDefinitions); + } } + } catch (FileNotFoundException e) { + LOG.debug("No XML rests found in {}. Skipping XML rests detection.", part); } - } catch (FileNotFoundException e) { - LOG.debug("No XML rests found in {}. Skipping XML rests detection.", directory); - } catch (Exception e) { - throw new RuntimeException(e); } } diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelXmlRoutesTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelXmlRoutesTest.java new file mode 100644 index 0000000000000..3ebff4e8762bf --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelXmlRoutesTest.java @@ -0,0 +1,62 @@ +/** + * 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.spring.boot; + +import org.apache.camel.CamelContext; +import org.apache.camel.Route; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +@DirtiesContext +@RunWith(SpringRunner.class) +@EnableAutoConfiguration +@SpringBootTest( + classes = { + CamelXmlRoutesTest.class, + RouteConfigWithCamelContextInjected.class }, + properties = { + "camel.springboot.xml-routes=file:src/test/resources/routes/foo.xml,file:src/test/resources/routes/bar.xml"} +) +public class CamelXmlRoutesTest extends Assert { + + // Collaborators fixtures + + @Autowired + CamelContext camelContext; + + @Test + public void shouldDetectRoutes() { + // When + Route route = camelContext.getRoute("foo"); + + // Then + assertNotNull(route); + + // When + route = camelContext.getRoute("bar"); + + // Then + assertNotNull(route); + } + +} \ No newline at end of file diff --git a/components/camel-spring-boot/src/test/resources/routes/bar.xml b/components/camel-spring-boot/src/test/resources/routes/bar.xml new file mode 100644 index 0000000000000..63ee6dffb625a --- /dev/null +++ b/components/camel-spring-boot/src/test/resources/routes/bar.xml @@ -0,0 +1,25 @@ + + + + + + + + diff --git a/components/camel-spring-boot/src/test/resources/routes/foo.xml b/components/camel-spring-boot/src/test/resources/routes/foo.xml new file mode 100644 index 0000000000000..99f798be442c6 --- /dev/null +++ b/components/camel-spring-boot/src/test/resources/routes/foo.xml @@ -0,0 +1,25 @@ + + + + + + + +