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 2021/08/03 12:33:40 UTC
[camel] 10/22: CAMEL-16757: camel-endpointdsl to have route
configuration builder also.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch CAMEL-16757b
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 4bd662a01bb647a84c07c6bcbcba0935d01aadb5
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Aug 2 15:29:09 2021 +0200
CAMEL-16757: camel-endpointdsl to have route configuration builder also.
---
.../apache/camel/test/junit5/CamelTestSupport.java | 14 +++-
.../apache/camel/test/junit4/CamelTestSupport.java | 14 +++-
.../EndpointRouteConfigurationBuilder.java | 91 ++++++++++++++++++++++
.../endpoint/EndpointRoutesConfigurationTest.java | 57 ++++++++++++++
4 files changed, 172 insertions(+), 4 deletions(-)
diff --git a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
index 0a93950..1a38a28 100644
--- a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
+++ b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
@@ -40,6 +40,7 @@ import org.apache.camel.NoSuchEndpointException;
import org.apache.camel.Predicate;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
+import org.apache.camel.RouteConfigurationsBuilder;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.Service;
@@ -506,9 +507,18 @@ public abstract class CamelTestSupport
if (isUseRouteBuilder()) {
RoutesBuilder[] builders = createRouteBuilders();
+ // add configuration before routes
for (RoutesBuilder builder : builders) {
- LOG.debug("Using created route builder: {}", builder);
- context.addRoutes(builder);
+ if (builder instanceof RouteConfigurationsBuilder) {
+ LOG.debug("Using created route configuration: {}", builder);
+ context.addRoutesConfigurations((RouteConfigurationsBuilder) builder);
+ }
+ }
+ for (RoutesBuilder builder : builders) {
+ if (!(builder instanceof RouteConfigurationsBuilder)) {
+ LOG.debug("Using created route builder: {}", builder);
+ context.addRoutes(builder);
+ }
}
replaceFromEndpoints();
boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
diff --git a/components/camel-test/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java b/components/camel-test/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
index fd83f71..e7a8a79 100644
--- a/components/camel-test/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
+++ b/components/camel-test/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
@@ -56,6 +56,7 @@ import org.apache.camel.Predicate;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.Route;
+import org.apache.camel.RouteConfigurationsBuilder;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.Service;
@@ -444,9 +445,18 @@ public abstract class CamelTestSupport extends TestSupport {
if (isUseRouteBuilder()) {
RoutesBuilder[] builders = createRouteBuilders();
+ // add configuration before routes
for (RoutesBuilder builder : builders) {
- LOG.debug("Using created route builder: {}", builder);
- context.addRoutes(builder);
+ if (builder instanceof RouteConfigurationsBuilder) {
+ log.debug("Using created route configuration: {}", builder);
+ context.addRoutesConfigurations((RouteConfigurationsBuilder) builder);
+ }
+ }
+ for (RoutesBuilder builder : builders) {
+ if (!(builder instanceof RouteConfigurationsBuilder)) {
+ log.debug("Using created route builder: {}", builder);
+ context.addRoutes(builder);
+ }
}
replaceFromEndpoints();
boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteConfigurationBuilder.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteConfigurationBuilder.java
new file mode 100644
index 0000000..7122067
--- /dev/null
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteConfigurationBuilder.java
@@ -0,0 +1,91 @@
+/*
+ * 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.builder.endpoint;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RouteConfigurationsBuilder;
+import org.apache.camel.model.Model;
+import org.apache.camel.model.RouteConfigurationDefinition;
+import org.apache.camel.model.RouteConfigurationsDefinition;
+
+/**
+ * A <a href="http://camel.apache.org/dsl.html">Java DSL</a> which is used for building route configuration(s).
+ */
+public abstract class EndpointRouteConfigurationBuilder extends EndpointRouteBuilder implements RouteConfigurationsBuilder {
+
+ private final AtomicBoolean initializedConfiguration = new AtomicBoolean();
+ private RouteConfigurationsDefinition routeConfigurationCollection = new RouteConfigurationsDefinition();
+
+ @Override
+ public void configure() throws Exception {
+ // noop
+ }
+
+ public abstract void configuration() throws Exception;
+
+ public RouteConfigurationsDefinition getRouteConfigurationCollection() {
+ return routeConfigurationCollection;
+ }
+
+ public void setRouteConfigurationCollection(RouteConfigurationsDefinition routeConfigurationCollection) {
+ this.routeConfigurationCollection = routeConfigurationCollection;
+ }
+
+ /**
+ * Creates a new route configuration
+ *
+ * @return the builder
+ */
+ public RouteConfigurationDefinition routeConfiguration() {
+ return routeConfiguration(null);
+ }
+
+ /**
+ * Creates a new route configuration
+ *
+ * @return the builder
+ */
+ public RouteConfigurationDefinition routeConfiguration(String id) {
+ getRouteConfigurationCollection().setCamelContext(getCamelContext());
+ RouteConfigurationDefinition answer = getRouteConfigurationCollection().routeConfiguration(id);
+ configureRouteConfiguration(answer);
+ return answer;
+ }
+
+ @Override
+ public void addRouteConfigurationsToCamelContext(CamelContext context) throws Exception {
+ setCamelContext(context);
+ routeConfigurationCollection.setCamelContext(context);
+ if (initializedConfiguration.compareAndSet(false, true)) {
+ configuration();
+ }
+ populateRoutesConfiguration();
+ }
+
+ protected void populateRoutesConfiguration() throws Exception {
+ CamelContext camelContext = getContext();
+ if (camelContext == null) {
+ throw new IllegalArgumentException("CamelContext has not been injected!");
+ }
+ getRouteConfigurationCollection().setCamelContext(camelContext);
+ camelContext.getExtension(Model.class)
+ .addRouteConfigurations(getRouteConfigurationCollection().getRouteConfigurations());
+ }
+
+}
diff --git a/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/EndpointRoutesConfigurationTest.java b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/EndpointRoutesConfigurationTest.java
new file mode 100644
index 0000000..a67e5a7
--- /dev/null
+++ b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/EndpointRoutesConfigurationTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.builder.endpoint;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+public class EndpointRoutesConfigurationTest extends BaseEndpointDslTest {
+
+ @Test
+ public void testEndpointRoutesConfiguration() throws Exception {
+ getMockEndpoint("mock:error").expectedBodiesReceived("Hello World", "Bye World");
+
+ template.sendBody("direct:start", "Hello World");
+ template.sendBody("direct:start2", "Bye World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder[] createRouteBuilders() throws Exception {
+ return new RouteBuilder[] {
+ new EndpointRouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from(direct("start"))
+ .throwException(new IllegalArgumentException("Foo"));
+
+ from(direct("start2"))
+ .throwException(new IllegalArgumentException("Foo2"));
+ }
+ },
+ new EndpointRouteConfigurationBuilder() {
+ @Override
+ public void configuration() throws Exception {
+ // global routes configuration
+ routeConfiguration().onException(Exception.class).handled(true).to(mock("error"));
+ }
+ }
+ };
+ }
+
+}