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 2019/06/04 08:18:51 UTC
[camel] 02/05: CAMEL-13608: Add route filter to model camel context
so you can filter out unwanted routes, such as from unit testing.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 7e017e5c5697299b33bd5a2787a721b7c59f768a
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jun 4 06:44:55 2019 +0200
CAMEL-13608: Add route filter to model camel context so you can filter out unwanted routes, such as from unit testing.
---
.../camel/spring/boot/CamelAutoConfiguration.java | 5 ++
.../spring/boot/CamelConfigurationProperties.java | 21 +++++++
.../camel/impl/AbstractModelCamelContext.java | 6 ++
.../java/org/apache/camel/impl/DefaultModel.java | 6 ++
.../camel/main/MainConfigurationProperties.java | 59 ++++++++++++++++++++
.../java/org/apache/camel/main/MainSupport.java | 6 ++
.../main/java/org/apache/camel/model/Model.java | 16 ++++++
.../java/org/apache/camel/model/RouteFilters.java | 64 ++++++++++++++++++++++
...rTest.java => ModelRouteFilterPatternTest.java} | 4 +-
9 files changed, 185 insertions(+), 2 deletions(-)
diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
index d610b89..4eeb98d 100644
--- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
@@ -41,6 +41,7 @@ import org.apache.camel.health.HealthCheckRepository;
import org.apache.camel.health.HealthCheckService;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.FileWatcherReloadStrategy;
+import org.apache.camel.model.Model;
import org.apache.camel.processor.interceptor.BacklogTracer;
import org.apache.camel.processor.interceptor.HandleFault;
import org.apache.camel.spi.AsyncProcessorAwaitManager;
@@ -231,6 +232,10 @@ public class CamelAutoConfiguration {
camelContext.getExecutorServiceManager().setThreadNamePattern(config.getThreadNamePattern());
}
+ if (config.getRouteFilterPattern() != null) {
+ camelContext.getExtension(Model.class).setRouteFilterPattern(config.getRouteFilterPattern());
+ }
+
// additional advanced configuration which is not configured using CamelConfigurationProperties
afterPropertiesSet(applicationContext, camelContext);
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 de03247..f88f4f0 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
@@ -165,6 +165,19 @@ public class CamelConfigurationProperties {
private String fileConfigurations;
/**
+ * Used for filtering routes to only include routes matching the given pattern, which follows the following rules:
+ *
+ * - Match by route id
+ * - Match by route input endpoint uri
+ *
+ * The matching is using exact match, by wildcard and regular expression.
+ *
+ * For example to only include routes which starts with foo in their route id's, use: foo*
+ * And to only include routes which starts from JMS endpoints, use: jms:*
+ */
+ private String routeFilterPattern;
+
+ /**
* Whether to use the main run controller to ensure the Spring-Boot application
* keeps running until being stopped or the JVM terminated.
* You typically only need this if you run Spring-Boot standalone.
@@ -842,6 +855,14 @@ public class CamelConfigurationProperties {
this.fileConfigurations = fileConfigurations;
}
+ public String getRouteFilterPattern() {
+ return routeFilterPattern;
+ }
+
+ public void setRouteFilterPattern(String routeFilterPattern) {
+ this.routeFilterPattern = routeFilterPattern;
+ }
+
public boolean isTraceFormatterShowBody() {
return traceFormatterShowBody;
}
diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/AbstractModelCamelContext.java b/core/camel-core/src/main/java/org/apache/camel/impl/AbstractModelCamelContext.java
index c103488..68a54d2 100644
--- a/core/camel-core/src/main/java/org/apache/camel/impl/AbstractModelCamelContext.java
+++ b/core/camel-core/src/main/java/org/apache/camel/impl/AbstractModelCamelContext.java
@@ -39,6 +39,7 @@ import org.apache.camel.model.Model;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.model.RouteFilters;
import org.apache.camel.model.cloud.ServiceCallConfigurationDefinition;
import org.apache.camel.model.rest.RestDefinition;
import org.apache.camel.model.transformer.TransformerDefinition;
@@ -232,6 +233,11 @@ public abstract class AbstractModelCamelContext extends AbstractCamelContext imp
}
@Override
+ public void setRouteFilterPattern(String pattern) {
+ model.setRouteFilterPattern(pattern);
+ }
+
+ @Override
public void setRouteFilter(Function<RouteDefinition, Boolean> filter) {
model.setRouteFilter(filter);
}
diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/DefaultModel.java b/core/camel-core/src/main/java/org/apache/camel/impl/DefaultModel.java
index 970937c..8fe68e9 100644
--- a/core/camel-core/src/main/java/org/apache/camel/impl/DefaultModel.java
+++ b/core/camel-core/src/main/java/org/apache/camel/impl/DefaultModel.java
@@ -41,6 +41,7 @@ import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.ProcessorDefinitionHelper;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.RouteDefinitionHelper;
+import org.apache.camel.model.RouteFilters;
import org.apache.camel.model.RoutesDefinition;
import org.apache.camel.model.cloud.ServiceCallConfigurationDefinition;
import org.apache.camel.model.rest.RestDefinition;
@@ -292,6 +293,11 @@ public class DefaultModel implements Model {
}
@Override
+ public void setRouteFilterPattern(String pattern) {
+ setRouteFilter(RouteFilters.filterByPattern(pattern));
+ }
+
+ @Override
public Function<RouteDefinition, Boolean> getRouteFilter() {
return routeFilter;
}
diff --git a/core/camel-core/src/main/java/org/apache/camel/main/MainConfigurationProperties.java b/core/camel-core/src/main/java/org/apache/camel/main/MainConfigurationProperties.java
index c45b33a..116e6fe 100644
--- a/core/camel-core/src/main/java/org/apache/camel/main/MainConfigurationProperties.java
+++ b/core/camel-core/src/main/java/org/apache/camel/main/MainConfigurationProperties.java
@@ -72,6 +72,7 @@ public class MainConfigurationProperties {
private String fileWatchDirectory;
private boolean fileWatchDirectoryRecursively;
private ReloadStrategy reloadStrategy;
+ private String routeFilterPattern;
// getter and setters
// --------------------------------------------------------------
@@ -714,6 +715,26 @@ public class MainConfigurationProperties {
this.reloadStrategy = reloadStrategy;
}
+ public String getRouteFilterPattern() {
+ return routeFilterPattern;
+ }
+
+ /**
+ * Used for filtering routes to only include routes matching the given pattern, which follows the following rules:
+ *
+ * - Match by route id
+ * - Match by route input endpoint uri
+ *
+ * The matching is using exact match, by wildcard and regular expression.
+ *
+ * For example to only include routes which starts with foo in their route id's, use: foo*
+ * And to only include routes which starts from JMS endpoints, use: jms:*
+ */
+ public void setRouteFilterPattern(String routeFilterPattern) {
+ this.routeFilterPattern = routeFilterPattern;
+ }
+
+
// fluent builders
// --------------------------------------------------------------
@@ -743,6 +764,29 @@ public class MainConfigurationProperties {
}
/**
+ * Whether autowiring components with properties that are of same type, which has been added to the Camel registry, as a singleton instance.
+ * This is used for convention over configuration to inject DataSource, AmazonLogin instances to the components.
+ * <p/>
+ * This option is default enabled.
+ */
+ public MainConfigurationProperties withAutowireComponentProperties(boolean autowireComponentProperties) {
+ this.autowireComponentProperties = autowireComponentProperties;
+ return this;
+ }
+
+ /**
+ * Whether autowiring components (with deep nesting by attempting to walk as deep down the object graph by creating new empty objects on the way if needed)
+ * with properties that are of same type, which has been added to the Camel registry, as a singleton instance.
+ * This is used for convention over configuration to inject DataSource, AmazonLogin instances to the components.
+ * <p/>
+ * This option is default disabled.
+ */
+ public MainConfigurationProperties withAutowireComponentPropertiesDeep(boolean autowireComponentPropertiesDeep) {
+ this.autowireComponentPropertiesDeep = autowireComponentPropertiesDeep;
+ return this;
+ }
+
+ /**
* Sets the name of the CamelContext.
*/
public MainConfigurationProperties withName(String name) {
@@ -1188,4 +1232,19 @@ public class MainConfigurationProperties {
return this;
}
+ /**
+ * Used for filtering routes to only include routes matching the given pattern, which follows the following rules:
+ *
+ * - Match by route id
+ * - Match by route input endpoint uri
+ *
+ * The matching is using exact match, by wildcard and regular expression.
+ *
+ * For example to only include routes which starts with foo in their route id's, use: foo*
+ * And to only include routes which starts from JMS endpoints, use: jms:*
+ */
+ public MainConfigurationProperties withRouteFilterPattern(String routeFilterPattern) {
+ this.routeFilterPattern = routeFilterPattern;
+ return this;
+ }
}
diff --git a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
index 7157fcc..37b9b58 100644
--- a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
+++ b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
@@ -51,7 +51,9 @@ import org.apache.camel.health.HealthCheckService;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.FileWatcherReloadStrategy;
import org.apache.camel.model.Model;
+import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.model.RouteFilters;
import org.apache.camel.processor.interceptor.BacklogTracer;
import org.apache.camel.processor.interceptor.HandleFault;
import org.apache.camel.spi.AsyncProcessorAwaitManager;
@@ -975,6 +977,10 @@ public abstract class MainSupport extends ServiceSupport {
camelContext.getExecutorServiceManager().setThreadNamePattern(config.getThreadNamePattern());
}
+ if (config.getRouteFilterPattern() != null) {
+ camelContext.getExtension(Model.class).setRouteFilterPattern(config.getRouteFilterPattern());
+ }
+
// additional advanced configuration which is not configured using CamelConfigurationProperties
afterPropertiesSet(camelContext.getRegistry(), camelContext);
diff --git a/core/camel-core/src/main/java/org/apache/camel/model/Model.java b/core/camel-core/src/main/java/org/apache/camel/model/Model.java
index d1292df..5bd09c5 100644
--- a/core/camel-core/src/main/java/org/apache/camel/model/Model.java
+++ b/core/camel-core/src/main/java/org/apache/camel/model/Model.java
@@ -27,6 +27,7 @@ import org.apache.camel.model.cloud.ServiceCallConfigurationDefinition;
import org.apache.camel.model.rest.RestDefinition;
import org.apache.camel.model.transformer.TransformerDefinition;
import org.apache.camel.model.validator.ValidatorDefinition;
+import org.apache.camel.support.PatternHelper;
/**
* Model interface
@@ -263,6 +264,21 @@ public interface Model {
void startRouteDefinitions() throws Exception;
/**
+ * Used for filtering routes to only include routes matching the given pattern, which follows the following rules:
+ *
+ * - Match by route id
+ * - Match by route input endpoint uri
+ *
+ * The matching is using exact match, by wildcard and regular expression as documented by {@link PatternHelper#matchPattern(String, String)}.
+ *
+ * For example to only include routes which starts with foo in their route id's, use: foo*
+ * And to only include routes which starts from JMS endpoints, use: jms:*
+ *
+ * @param pattern the pattern
+ */
+ void setRouteFilterPattern(String pattern);
+
+ /**
* Sets a custom route filter to use for filtering unwanted routes when routes are added.
*
* @param filter the filter
diff --git a/core/camel-core/src/main/java/org/apache/camel/model/RouteFilters.java b/core/camel-core/src/main/java/org/apache/camel/model/RouteFilters.java
new file mode 100644
index 0000000..21e5652
--- /dev/null
+++ b/core/camel-core/src/main/java/org/apache/camel/model/RouteFilters.java
@@ -0,0 +1,64 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.model;
+
+import java.util.function.Function;
+
+import org.apache.camel.support.PatternHelper;
+
+/**
+ * Used for filtering routes to only include routes matching a function.
+ */
+public class RouteFilters implements Function<RouteDefinition, Boolean> {
+
+ public final String pattern;
+
+ /**
+ * Used for filtering routes to only include routes matching the given pattern, which follows the following rules:
+ *
+ * - Match by route id
+ * - Match by route input endpoint uri
+ *
+ * The matching is using exact match, by wildcard and regular expression as documented by {@link PatternHelper#matchPattern(String, String)}.
+ *
+ * For example to only include routes which starts with foo in their route id's, use: foo*
+ * And to only include routes which starts from JMS endpoints, use: jms:*
+ */
+ public static RouteFilters filterByPattern(String pattern) {
+ return new RouteFilters(pattern);
+ }
+
+ private RouteFilters(String pattern) {
+ this.pattern = pattern;
+ }
+
+ @Override
+ public Boolean apply(RouteDefinition route) {
+ boolean match = false;
+
+ String id = route.getId();
+ if (id != null) {
+ match = PatternHelper.matchPattern(id, pattern);
+ }
+ if (!match && route.getInput() != null) {
+ String uri = route.getInput().getEndpointUri();
+ match = PatternHelper.matchPattern(uri, pattern);
+ }
+ return match;
+ }
+
+}
diff --git a/core/camel-core/src/test/java/org/apache/camel/model/ModelRouteFilterTest.java b/core/camel-core/src/test/java/org/apache/camel/model/ModelRouteFilterPatternTest.java
similarity index 92%
rename from core/camel-core/src/test/java/org/apache/camel/model/ModelRouteFilterTest.java
rename to core/camel-core/src/test/java/org/apache/camel/model/ModelRouteFilterPatternTest.java
index f6a637d..014444a 100644
--- a/core/camel-core/src/test/java/org/apache/camel/model/ModelRouteFilterTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/model/ModelRouteFilterPatternTest.java
@@ -21,13 +21,13 @@ import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.junit.Test;
-public class ModelRouteFilterTest extends ContextTestSupport {
+public class ModelRouteFilterPatternTest extends ContextTestSupport {
@Override
protected CamelContext createCamelContext() throws Exception {
CamelContext context = super.createCamelContext();
// filter to only include foo route
- context.getExtension(ModelCamelContext.class).setRouteFilter((r) -> r.getId().equals("foo") );
+ context.getExtension(Model.class).setRouteFilterPattern("foo*");
return context;
}