Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 78C7E200CB9 for ; Sun, 2 Jul 2017 10:16:14 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 6750C160BEE; Sun, 2 Jul 2017 08:16:14 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 4D1DF160BD7 for ; Sun, 2 Jul 2017 10:16:13 +0200 (CEST) Received: (qmail 44858 invoked by uid 500); 2 Jul 2017 08:16:12 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 44847 invoked by uid 99); 2 Jul 2017 08:16:12 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 02 Jul 2017 08:16:12 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 55DBADFA3B; Sun, 2 Jul 2017 08:16:11 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@camel.apache.org Message-Id: <9b8d719ff1af4fb2bc6e70c3e96aa041@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: camel git commit: CAMEL-11490: camel-spring-boot - Make it easy to filter Java RoutesBuilder from properties Date: Sun, 2 Jul 2017 08:16:11 +0000 (UTC) archived-at: Sun, 02 Jul 2017 08:16:14 -0000 Repository: camel Updated Branches: refs/heads/master f40641995 -> 36d6a6603 CAMEL-11490: camel-spring-boot - Make it easy to filter Java RoutesBuilder from properties Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/36d6a660 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/36d6a660 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/36d6a660 Branch: refs/heads/master Commit: 36d6a66039cec01227238a712a33f2fa3a74f610 Parents: f406419 Author: Claus Ibsen Authored: Sun Jul 2 10:16:01 2017 +0200 Committer: Claus Ibsen Committed: Sun Jul 2 10:16:01 2017 +0200 ---------------------------------------------------------------------- .../boot/CamelConfigurationProperties.java | 48 +++++++++++++---- .../camel/spring/boot/RoutesCollector.java | 54 +++++++++++++++----- .../camel/spring/boot/routefilter/BarTest.java | 2 +- .../spring/boot/routefilter/DrinkRoute.java | 30 +++++++++++ .../camel/spring/boot/routefilter/FooTest.java | 4 +- 5 files changed, 114 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/36d6a660/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelConfigurationProperties.java ---------------------------------------------------------------------- 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 b8db8db..1739625 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 @@ -96,12 +96,34 @@ public class CamelConfigurationProperties { private boolean loadTypeConverters = true; /** - * To filter component scanning of RouteBuilder classes with @Component annotation. - * You can use a regular expression to match which class names (simple name) to match. - * For example to match all classes starting with Foo, Foo* or use - * a regular expression to match all Foo or Bar routes with (Foo|Bar).* + * Used for inclusive filtering component scanning of RouteBuilder classes with @Component annotation. + * The exclusive filtering takes precedence over inclusive filtering. + * The pattern is using Ant-path style pattern. + *

+ * Multiple patterns can be specified separated by comma. + * For example to include all classes starting with Foo use **/Foo*. + * To include all routes form a specific package use, com/mycompany/foo/* + * To include all routes form a specific package and its sub-packages use double wildcards, com/mycompany/foo/** + * And to include all routes from two specific packages use, com/mycompany/foo/*,com/mycompany/stuff/* + * + * @see org.springframework.util.AntPathMatcher */ - private String javaRoutesFilter = "*"; + private String javaRoutesIncludePattern; + + /** + * Used for exclusive filtering component scanning of RouteBuilder classes with @Component annotation. + * The exclusive filtering takes precedence over inclusive filtering. + * The pattern is using Ant-path style pattern. + * Multiple patterns can be specified separated by comma. + *

+ * For example to exclude all classes starting with Bar use **/Bar*. + * To exclude all routes form a specific package use, com/mycompany/bar/* + * To exclude all routes form a specific package and its sub-packages use double wildcards, com/mycompany/bar/** + * And to exclude all routes from two specific packages use, com/mycompany/bar/*,com/mycompany/stuff/* + * + * @see org.springframework.util.AntPathMatcher + */ + private String javaRoutesExcludePattern; /** * Directory to scan for adding additional XML routes. @@ -517,12 +539,20 @@ public class CamelConfigurationProperties { this.loadTypeConverters = loadTypeConverters; } - public String getJavaRoutesFilter() { - return javaRoutesFilter; + public String getJavaRoutesIncludePattern() { + return javaRoutesIncludePattern; + } + + public void setJavaRoutesIncludePattern(String javaRoutesIncludePattern) { + this.javaRoutesIncludePattern = javaRoutesIncludePattern; + } + + public String getJavaRoutesExcludePattern() { + return javaRoutesExcludePattern; } - public void setJavaRoutesFilter(String javaRoutesFilter) { - this.javaRoutesFilter = javaRoutesFilter; + public void setJavaRoutesExcludePattern(String javaRoutesExcludePattern) { + this.javaRoutesExcludePattern = javaRoutesExcludePattern; } public String getXmlRoutes() { http://git-wip-us.apache.org/repos/asf/camel/blob/36d6a660/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java ---------------------------------------------------------------------- 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 1085039..b1edd8c 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 @@ -34,7 +34,7 @@ import org.apache.camel.model.RoutesDefinition; import org.apache.camel.model.rest.RestDefinition; import org.apache.camel.model.rest.RestsDefinition; import org.apache.camel.spi.EventNotifier; -import org.apache.camel.util.EndpointHelper; +import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.ServiceHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,6 +44,7 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.core.Ordered; import org.springframework.core.io.Resource; +import org.springframework.util.AntPathMatcher; /** * Collects routes and rests from the various sources (like Spring application context beans registry or opinionated @@ -81,24 +82,53 @@ public class RoutesCollector implements ApplicationListener {}", name, filter, result); - if (result) { - try { - LOG.debug("Injecting following route into the CamelContext: {}", routesBuilder); - camelContext.addRoutes(routesBuilder); - } catch (Exception e) { - throw new CamelSpringBootInitializationException(e); + String name = routesBuilder.getClass().getName(); + // make name as path so we can use ant path matcher + name = name.replace('.', '/'); + + String exclude = configurationProperties.getJavaRoutesExcludePattern(); + String include = configurationProperties.getJavaRoutesIncludePattern(); + + boolean match = !"false".equals(include); + // exclude take precedence over include + if (match && ObjectHelper.isNotEmpty(exclude)) { + // there may be multiple separated by comma + String[] parts = exclude.split(","); + for (String part : parts) { + // must negate when excluding, and hence ! + match = !matcher.match(part, name); + LOG.trace("Java RoutesBuilder: {} exclude filter: {} -> {}", name, part, match); + if (!match) { + break; + } + } + } + if (match && ObjectHelper.isNotEmpty(include)) { + // there may be multiple separated by comma + String[] parts = include.split(","); + for (String part : parts) { + match = matcher.match(part, name); + LOG.trace("Java RoutesBuilder: {} include filter: {} -> {}", name, part, match); + if (match) { + break; } } } + LOG.debug("Java RoutesBuilder: {} accepted by include/exclude filter: {}", name, match); + if (match) { + try { + LOG.debug("Injecting following route into the CamelContext: {}", routesBuilder); + camelContext.addRoutes(routesBuilder); + } catch (Exception e) { + throw new CamelSpringBootInitializationException(e); + } + } } } http://git-wip-us.apache.org/repos/asf/camel/blob/36d6a660/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/BarTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/BarTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/BarTest.java index 75269a1..ca4a168 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/BarTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/BarTest.java @@ -29,7 +29,7 @@ import org.springframework.boot.test.context.SpringBootTest; @RunWith(CamelSpringBootRunner.class) @SpringBootApplication @SpringBootTest(classes = BarTest.class, - properties = {"camel.springboot.javaRoutesFilter=Bar*"}) + properties = {"camel.springboot.java-routes-include-pattern=**/Bar*"}) public class BarTest { @Autowired http://git-wip-us.apache.org/repos/asf/camel/blob/36d6a660/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/DrinkRoute.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/DrinkRoute.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/DrinkRoute.java new file mode 100644 index 0000000..824a123 --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/DrinkRoute.java @@ -0,0 +1,30 @@ +/** + * 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.routefilter; + +import org.apache.camel.builder.RouteBuilder; +import org.springframework.stereotype.Component; + +@Component +public class DrinkRoute extends RouteBuilder { + + @Override + public void configure() throws Exception { + from("direct:start") + .to("mock:foo"); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/36d6a660/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooTest.java index 0deab70..ccc6640 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooTest.java @@ -28,8 +28,8 @@ import org.springframework.boot.test.context.SpringBootTest; @RunWith(CamelSpringBootRunner.class) @SpringBootApplication() -@SpringBootTest(classes = FooTest.class, - properties = {"camel.springboot.javaRoutesFilter=Foo*"}) +@SpringBootTest(classes = BarTest.class, + properties = {"camel.springboot.java-routes-exclude-pattern=**/Bar*,**/Drink*"}) public class FooTest { @Autowired