camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-11490: camel-spring-boot - Make it easy to filter Java RoutesBuilder from properties
Date Sun, 02 Jul 2017 08:23:35 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.19.x 1cf3d699b -> 5fbdc0e61


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/5fbdc0e6
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5fbdc0e6
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5fbdc0e6

Branch: refs/heads/camel-2.19.x
Commit: 5fbdc0e61e8cb3cb4d72689f467867f0a4379a10
Parents: 1cf3d69
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sun Jul 2 10:16:01 2017 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sun Jul 2 10:23:12 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/5fbdc0e6/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 83db7a7..b26365a 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
@@ -87,12 +87,34 @@ public class CamelConfigurationProperties {
     private boolean typeConversion = 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, <tt>Foo*</tt> or use
-     * a regular expression to match all Foo or Bar routes with <tt>(Foo|Bar).*</tt>
+     * 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.
+     * <p/>
+     * Multiple patterns can be specified separated by comma.
+     * For example to include all classes starting with Foo use <tt>&#42;&#42;/Foo*</tt>.
+     * To include all routes form a specific package use, <tt>com/mycompany/foo/*</tt>
+     * To include all routes form a specific package and its sub-packages use double wildcards,
<tt>com/mycompany/foo/**</tt>
+     * And to include all routes from two specific packages use, <tt>com/mycompany/foo/*,com/mycompany/stuff/*</tt>
+     *
+     * @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.
+     * <p/>
+     * For example to exclude all classes starting with Bar use <tt>&#42;&#42;/Bar*</tt>.
+     * To exclude all routes form a specific package use, <tt>com/mycompany/bar/*</tt>
+     * To exclude all routes form a specific package and its sub-packages use double wildcards,
<tt>com/mycompany/bar/**</tt>
+     * And to exclude all routes from two specific packages use, <tt>com/mycompany/bar/*,com/mycompany/stuff/*</tt>
+     *
+     * @see org.springframework.util.AntPathMatcher
+     */
+    private String javaRoutesExcludePattern;
 
     /**
      * Directory to scan for adding additional XML routes.
@@ -482,12 +504,20 @@ public class CamelConfigurationProperties {
         this.typeConversion = typeConversion;
     }
 
-    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/5fbdc0e6/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 60ea1da..ccf8b5c 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;
@@ -43,6 +43,7 @@ import org.springframework.context.ApplicationListener;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.context.event.ContextRefreshedEvent;
 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
@@ -85,24 +86,53 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven
             // only add and start Camel if its stopped (initial state)
             if (camelContext.getStatus().isStopped()) {
                 LOG.debug("Post-processing CamelContext bean: {}", camelContext.getName());
+
+                final AntPathMatcher matcher = new AntPathMatcher();
                 for (RoutesBuilder routesBuilder : applicationContext.getBeansOfType(RoutesBuilder.class,
configurationProperties.isIncludeNonSingletons(), true).values()) {
                     // filter out abstract classes
                     boolean abs = Modifier.isAbstract(routesBuilder.getClass().getModifiers());
                     if (!abs) {
-                        String filter = configurationProperties.getJavaRoutesFilter();
-                        if (!"false".equals(filter)) {
-                            String name = routesBuilder.getClass().getSimpleName();
-                            boolean result = "*".equals(filter) || EndpointHelper.matchPattern(name,
filter);
-                            LOG.debug("Java RoutesBuilder: {} apply filter: {} -> {}",
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/5fbdc0e6/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/5fbdc0e6/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/5fbdc0e6/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


Mime
View raw message