You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/07/01 19:35:07 UTC

[2/2] camel git commit: CAMEL-11490: camel-spring-boot - Make it easy to filter Java RoutesBuilder from properties

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

Branch: refs/heads/camel-2.19.x
Commit: 1cf3d699ba2722732a05bb3620d185eba78ca44a
Parents: 92a71ae
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Jul 1 21:24:32 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Jul 1 21:34:56 2017 +0200

----------------------------------------------------------------------
 .../boot/CamelConfigurationProperties.java      | 16 ++++++
 .../camel/spring/boot/RoutesCollector.java      | 19 +++++--
 .../camel/spring/boot/routefilter/BarRoute.java | 30 +++++++++++
 .../camel/spring/boot/routefilter/BarTest.java  | 54 ++++++++++++++++++++
 .../camel/spring/boot/routefilter/FooRoute.java | 30 +++++++++++
 .../camel/spring/boot/routefilter/FooTest.java  | 54 ++++++++++++++++++++
 6 files changed, 198 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1cf3d699/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 f66fa61..83db7a7 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,6 +87,14 @@ 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>
+     */
+    private String javaRoutesFilter = "*";
+
+    /**
      * Directory to scan for adding additional XML routes.
      * You can turn this off by setting the value to false.
      */
@@ -474,6 +482,14 @@ public class CamelConfigurationProperties {
         this.typeConversion = typeConversion;
     }
 
+    public String getJavaRoutesFilter() {
+        return javaRoutesFilter;
+    }
+
+    public void setJavaRoutesFilter(String javaRoutesFilter) {
+        this.javaRoutesFilter = javaRoutesFilter;
+    }
+
     public String getXmlRoutes() {
         return xmlRoutes;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/1cf3d699/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 ce78b01..60ea1da 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,6 +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.ServiceHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -88,11 +89,19 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven
                     // filter out abstract classes
                     boolean abs = Modifier.isAbstract(routesBuilder.getClass().getModifiers());
                     if (!abs) {
-                        try {
-                            LOG.debug("Injecting following route into the CamelContext: {}", routesBuilder);
-                            camelContext.addRoutes(routesBuilder);
-                        } catch (Exception e) {
-                            throw new CamelSpringBootInitializationException(e);
+                        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);
+                                }
+                            }
                         }
                     }
                 }

http://git-wip-us.apache.org/repos/asf/camel/blob/1cf3d699/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/BarRoute.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/BarRoute.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/BarRoute.java
new file mode 100644
index 0000000..4b83c99
--- /dev/null
+++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/BarRoute.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 BarRoute extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        from("direct:start")
+            .to("mock:bar");
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1cf3d699/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
new file mode 100644
index 0000000..75269a1
--- /dev/null
+++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/BarTest.java
@@ -0,0 +1,54 @@
+/**
+ * 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.ProducerTemplate;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.model.ModelCamelContext;
+import org.apache.camel.test.spring.CamelSpringBootRunner;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@RunWith(CamelSpringBootRunner.class)
+@SpringBootApplication
+@SpringBootTest(classes = BarTest.class,
+    properties = {"camel.springboot.javaRoutesFilter=Bar*"})
+public class BarTest {
+
+    @Autowired
+    ProducerTemplate producerTemplate;
+
+    @Autowired
+    ModelCamelContext camelContext;
+
+    @Test
+    public void shouldSendToBar() throws Exception {
+        // Given
+        MockEndpoint mock = camelContext.getEndpoint("mock:bar", MockEndpoint.class);
+        mock.expectedBodiesReceived("Hello Bar");
+
+        // When
+        producerTemplate.sendBody("direct:start", "Hello Bar");
+
+        // Then
+        mock.assertIsSatisfied();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1cf3d699/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooRoute.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooRoute.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooRoute.java
new file mode 100644
index 0000000..8914569
--- /dev/null
+++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooRoute.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 FooRoute extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        from("direct:start")
+            .to("mock:foo");
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1cf3d699/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
new file mode 100644
index 0000000..0deab70
--- /dev/null
+++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooTest.java
@@ -0,0 +1,54 @@
+/**
+ * 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.ProducerTemplate;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.model.ModelCamelContext;
+import org.apache.camel.test.spring.CamelSpringBootRunner;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@RunWith(CamelSpringBootRunner.class)
+@SpringBootApplication()
+@SpringBootTest(classes = FooTest.class,
+    properties = {"camel.springboot.javaRoutesFilter=Foo*"})
+public class FooTest {
+
+    @Autowired
+    ProducerTemplate producerTemplate;
+
+    @Autowired
+    ModelCamelContext camelContext;
+
+    @Test
+    public void shouldSendToFoo() throws Exception {
+        // Given
+        MockEndpoint mock = camelContext.getEndpoint("mock:foo", MockEndpoint.class);
+        mock.expectedBodiesReceived("Hello Foo");
+
+        // When
+        producerTemplate.sendBody("direct:start", "Hello Foo");
+
+        // Then
+        mock.assertIsSatisfied();
+    }
+
+}