This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-16757 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 3490f00066be14134aea85fb24379680ae7d481a Author: Claus Ibsen <[email protected]> AuthorDate: Wed Jul 7 12:27:19 2021 +0200 CAMEL-16757: camel-core - Global error handling, interceptor in all DSL --- .../main/java/org/apache/camel/CamelContext.java | 8 ++++++ .../apache/camel/RoutesConfigurationsBuilder.java | 33 ++++++++++++++++++++++ .../camel/impl/engine/AbstractCamelContext.java | 10 +++++++ .../org/apache/camel/builder/RouteBuilder.java | 25 ++++++++++++++-- .../camel/builder/RoutesConfigurationsBuilder.java | 32 +++++++++++++++++++++ .../org/apache/camel/main/RoutesConfigurer.java | 12 ++++++-- 6 files changed, 115 insertions(+), 5 deletions(-) diff --git a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java index 387c071..7a82c74 100644 --- a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java +++ b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java @@ -573,6 +573,14 @@ public interface CamelContext extends CamelContextLifecycle, RuntimeConfiguratio void addRoutes(RoutesBuilder builder) throws Exception; /** + * Adds the routes configurations (global configuration for all routes) from the routes builder. + * + * @param builder the builder which has routes configurations + * @throws Exception if the routes configurations could not be created for whatever reason + */ + void addRoutesConfigurations(RoutesConfigurationsBuilder builder) throws Exception; + + /** * Removes the given route (the route <b>must</b> be stopped before it can be removed). * <p/> * A route which is removed will be unregistered from JMX, have its services stopped/shutdown and the route diff --git a/core/camel-api/src/main/java/org/apache/camel/RoutesConfigurationsBuilder.java b/core/camel-api/src/main/java/org/apache/camel/RoutesConfigurationsBuilder.java new file mode 100644 index 0000000..0ff22ed --- /dev/null +++ b/core/camel-api/src/main/java/org/apache/camel/RoutesConfigurationsBuilder.java @@ -0,0 +1,33 @@ +/* + * 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; + +/** + * A routes configurations builder is capable of building routes configurations (global configuration for all routes) + * using the builder and model classes. + */ +public interface RoutesConfigurationsBuilder { + + /** + * Adds the routes configurations from this Routes Configurations Builder to the CamelContext. + * + * @param context the Camel context + * @throws Exception is thrown if initialization of routes configurations failed + */ + void addRoutesConfigurationsToCamelContext(CamelContext context) throws Exception; + +} diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java index dbc792c..805a238 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java @@ -65,6 +65,7 @@ import org.apache.camel.ResolveEndpointFailedException; import org.apache.camel.Route; import org.apache.camel.RouteAware; import org.apache.camel.RoutesBuilder; +import org.apache.camel.RoutesConfigurationsBuilder; import org.apache.camel.RuntimeCamelException; import org.apache.camel.Service; import org.apache.camel.ServiceStatus; @@ -1151,6 +1152,15 @@ public abstract class AbstractCamelContext extends BaseService } } + @Override + public void addRoutesConfigurations(RoutesConfigurationsBuilder builder) throws Exception { + try (LifecycleHelper helper = new LifecycleHelper()) { + build(); + LOG.debug("Adding route configurations from builder: {}", builder); + builder.addRoutesConfigurationsToCamelContext(AbstractCamelContext.this); + } + } + public ServiceStatus getRouteStatus(String key) { RouteService routeService = routeServices.get(key); if (routeService != null) { diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteBuilder.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteBuilder.java index 622b225..90ae70e 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteBuilder.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteBuilder.java @@ -29,6 +29,7 @@ import org.apache.camel.ExtendedCamelContext; import org.apache.camel.Ordered; import org.apache.camel.Route; import org.apache.camel.RoutesBuilder; +import org.apache.camel.RoutesConfigurationsBuilder; import org.apache.camel.model.FromDefinition; import org.apache.camel.model.InterceptDefinition; import org.apache.camel.model.InterceptFromDefinition; @@ -61,10 +62,11 @@ import org.slf4j.LoggerFactory; * A <a href="http://camel.apache.org/dsl.html">Java DSL</a> which is used to build {@link Route} instances in a * {@link CamelContext} for smart routing. */ -public abstract class RouteBuilder extends BuilderSupport implements RoutesBuilder, Ordered { +public abstract class RouteBuilder extends BuilderSupport implements RoutesBuilder, RoutesConfigurationsBuilder, Ordered { protected Logger log = LoggerFactory.getLogger(getClass()); private final AtomicBoolean initialized = new AtomicBoolean(); + private final AtomicBoolean initializedConfiguration = new AtomicBoolean(); private final List<RouteBuilderLifecycleStrategy> lifecycleInterceptors = new ArrayList<>(); private final List<TransformerBuilder> transformerBuilders = new ArrayList<>(); private final List<ValidatorBuilder> validatorBuilders = new ArrayList<>(); @@ -169,6 +171,16 @@ public abstract class RouteBuilder extends BuilderSupport implements RoutesBuild public abstract void configure() throws Exception; /** + * <b>Called on initialization to build routes configuration (global routes configurations) using the fluent builder + * syntax.</b> + * + * @throws Exception can be thrown during configuration + */ + public void configuration() throws Exception { + // noop + } + + /** * Binds the bean to the repository (if possible). * * @param id the id of the bean @@ -477,7 +489,6 @@ public abstract class RouteBuilder extends BuilderSupport implements RoutesBuild configureRests(context); // but populate rests before routes, as we want to turn rests into routes - populateRoutesConfiguration(); populateRests(); populateTransformers(); populateValidators(); @@ -489,6 +500,16 @@ public abstract class RouteBuilder extends BuilderSupport implements RoutesBuild } } + @Override + public void addRoutesConfigurationsToCamelContext(CamelContext context) throws Exception { + setCamelContext(context); + routeCollection.setCamelContext(context); + if (initializedConfiguration.compareAndSet(false, true)) { + configuration(); + } + populateRoutesConfiguration(); + } + /** * Configures the routes * diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/RoutesConfigurationsBuilder.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/RoutesConfigurationsBuilder.java new file mode 100644 index 0000000..a31275a --- /dev/null +++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/RoutesConfigurationsBuilder.java @@ -0,0 +1,32 @@ +/* + * 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; + +/** + * A <a href="http://camel.apache.org/dsl.html">Java DSL</a> which is used for defining routes configuration (global + * routes configuration) + */ +public abstract class RoutesConfigurationsBuilder extends RouteBuilder { + + @Override + public void configure() throws Exception { + // noop + } + + public abstract void configuration() throws Exception; + +} diff --git a/core/camel-main/src/main/java/org/apache/camel/main/RoutesConfigurer.java b/core/camel-main/src/main/java/org/apache/camel/main/RoutesConfigurer.java index 4d59e2f..4ed77f3 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/RoutesConfigurer.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/RoutesConfigurer.java @@ -24,6 +24,7 @@ import java.util.Set; import org.apache.camel.CamelContext; import org.apache.camel.ExtendedCamelContext; import org.apache.camel.RoutesBuilder; +import org.apache.camel.RoutesConfigurationsBuilder; import org.apache.camel.RuntimeCamelException; import org.apache.camel.spi.CamelBeanPostProcessor; import org.apache.camel.support.OrderedComparator; @@ -205,9 +206,14 @@ public class RoutesConfigurer { // sort routes according to ordered routes.sort(OrderedComparator.get()); - // TODO: 1st-pass for RoutesConfiguration - // TODO: 2nd-pass for the routes - + // first add the routes configurations as they are globally for all routes + for (RoutesBuilder builder : routes) { + if (builder instanceof RoutesConfigurationsBuilder) { + RoutesConfigurationsBuilder rcb = (RoutesConfigurationsBuilder) builder; + LOG.debug("Adding routes configurations into CamelContext from RoutesConfigurationsBuilder: {}", rcb); + camelContext.addRoutesConfigurations(rcb); + } + } // then add the routes for (RoutesBuilder builder : routes) { LOG.debug("Adding routes into CamelContext from RoutesBuilder: {}", builder);
