Skip to content

Commit

Permalink
CAMEL-13015: camel-spring-boot - xml routes/rests can now load from m…
Browse files Browse the repository at this point in the history
…ultiple paths separated by comma.
  • Loading branch information
davsclaus committed Dec 20, 2018
1 parent ed6cdb4 commit 3b1709f
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 25 deletions.
4 changes: 2 additions & 2 deletions components/camel-spring-boot/src/main/docs/spring-boot.adoc
Expand Up @@ -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-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-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.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-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. | classpath:camel/*.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.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.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* | The optional explicitly configured cipher suites for this configuration. | | CipherSuitesParameters
Expand Down
Expand Up @@ -122,12 +122,24 @@ public class CamelConfigurationProperties {
/** /**
* Directory to scan for adding additional XML routes. * Directory to scan for adding additional XML routes.
* You can turn this off by setting the value to false. * 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"; private String xmlRoutes = "classpath:camel/*.xml";


/** /**
* Directory to scan for adding additional XML rests. * Directory to scan for adding additional XML rests.
* You can turn this off by setting the value to false. * 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"; private String xmlRests = "classpath:camel-rest/*.xml";


Expand Down
Expand Up @@ -275,35 +275,39 @@ public int getOrder() {
// Helpers // Helpers


private void loadXmlRoutes(ApplicationContext applicationContext, CamelContext camelContext, String directory) throws Exception { private void loadXmlRoutes(ApplicationContext applicationContext, CamelContext camelContext, String directory) throws Exception {
LOG.info("Loading additional Camel XML routes from: {}", directory); String[] parts = directory.split(",");
try { for (String part : parts) {
Resource[] xmlRoutes = applicationContext.getResources(directory); LOG.info("Loading additional Camel XML routes from: {}", part);
for (Resource xmlRoute : xmlRoutes) { try {
LOG.debug("Found XML route: {}", xmlRoute); Resource[] xmlRoutes = applicationContext.getResources(part);
RoutesDefinition xmlDefinition = camelContext.adapt(ModelCamelContext.class).loadRoutesDefinition(xmlRoute.getInputStream()); for (Resource xmlRoute : xmlRoutes) {
camelContext.adapt(ModelCamelContext.class).addRouteDefinitions(xmlDefinition.getRoutes()); 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) { private void loadXmlRests(ApplicationContext applicationContext, CamelContext camelContext, String directory) throws Exception {
LOG.info("Loading additional Camel XML rests from: {}", directory); String[] parts = directory.split(",");
try { for (String part : parts) {
final Resource[] xmlRests = applicationContext.getResources(directory); LOG.info("Loading additional Camel XML rests from: {}", part);
for (final Resource xmlRest : xmlRests) { try {
final RestsDefinition xmlDefinitions = camelContext.adapt(ModelCamelContext.class).loadRestsDefinition(xmlRest.getInputStream()); final Resource[] xmlRests = applicationContext.getResources(part);
camelContext.adapt(ModelCamelContext.class).addRestDefinitions(xmlDefinitions.getRests()); for (final Resource xmlRest : xmlRests) {
for (final RestDefinition xmlDefinition : xmlDefinitions.getRests()) { final RestsDefinition xmlDefinitions = camelContext.adapt(ModelCamelContext.class).loadRestsDefinition(xmlRest.getInputStream());
final List<RouteDefinition> routeDefinitions = xmlDefinition.asRouteDefinition(camelContext); camelContext.adapt(ModelCamelContext.class).addRestDefinitions(xmlDefinitions.getRests());
camelContext.adapt(ModelCamelContext.class).addRouteDefinitions(routeDefinitions); for (final RestDefinition xmlDefinition : xmlDefinitions.getRests()) {
final List<RouteDefinition> 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);
} }
} }


Expand Down
@@ -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);
}

}
25 changes: 25 additions & 0 deletions 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>
25 changes: 25 additions & 0 deletions 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>

0 comments on commit 3b1709f

Please sign in to comment.