This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-2.22.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-2.22.x by this push: new b28b3ff CAMEL-13015: camel-spring-boot - xml routes/rests can now load from multiple paths separated by comma. b28b3ff is described below commit b28b3ffda90caa3e2784d8cb273d7fa8181ccea6 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Dec 20 12:56:41 2018 +0100 CAMEL-13015: camel-spring-boot - xml routes/rests can now load from multiple paths separated by comma. --- .../spring/boot/CamelConfigurationProperties.java | 12 +++++ .../apache/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 +++++++++ 5 files changed, 151 insertions(+), 23 deletions(-) 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 d00f597..039ee0d 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 @@ -128,12 +128,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 54f2064..ea8bfe4 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 @@ -273,35 +273,39 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven // 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.loadRoutesDefinition(xmlRoute.getInputStream()); - camelContext.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.loadRoutesDefinition(xmlRoute.getInputStream()); + camelContext.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.loadRestsDefinition(xmlRest.getInputStream()); - camelContext.addRestDefinitions(xmlDefinitions.getRests()); - for (final RestDefinition xmlDefinition : xmlDefinitions.getRests()) { - final List<RouteDefinition> routeDefinitions = xmlDefinition.asRouteDefinition(camelContext); - camelContext.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.loadRestsDefinition(xmlRest.getInputStream()); + camelContext.addRestDefinitions(xmlDefinitions.getRests()); + for (final RestDefinition xmlDefinition : xmlDefinitions.getRests()) { + final List<RouteDefinition> routeDefinitions = xmlDefinition.asRouteDefinition(camelContext); + camelContext.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 0000000..3ebff4e --- /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 0000000..63ee6df --- /dev/null +++ b/components/camel-spring-boot/src/test/resources/routes/bar.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<routes xmlns="http://camel.apache.org/schema/spring"> + <route id="bar"> + <from uri="direct:bar"/> + <to uri="mock:bar"/> + </route> +</routes> 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 0000000..99f798b --- /dev/null +++ b/components/camel-spring-boot/src/test/resources/routes/foo.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<routes xmlns="http://camel.apache.org/schema/spring"> + <route id="foo"> + <from uri="direct:foo"/> + <to uri="mock:foo"/> + </route> +</routes>