This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch kamelet2 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 43e1648fe3607a41d0542735aeea41675293defe Author: Claus Ibsen <[email protected]> AuthorDate: Mon Jan 27 10:21:12 2025 +0100 CAMEL-21599: camel-kamelet - Rework error handler for kamelets to be more standard Camel. WIP --- .../camel/component/kamelet/KameletComponent.java | 12 ++--------- .../main/java/org/apache/camel/CamelContext.java | 18 ++++++++++++++++ .../camel/impl/engine/SimpleCamelContext.java | 8 +++++++ .../org/apache/camel/impl/DefaultCamelContext.java | 22 +++++++++++-------- .../java/org/apache/camel/impl/DefaultModel.java | 25 ++++++++++++++++++---- .../main/java/org/apache/camel/model/Model.java | 18 ++++++++++++++++ 6 files changed, 80 insertions(+), 23 deletions(-) diff --git a/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java b/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java index 7335b58e999..97711779607 100644 --- a/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java +++ b/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java @@ -54,8 +54,6 @@ import static org.apache.camel.component.kamelet.Kamelet.PARAM_LOCATION; import static org.apache.camel.component.kamelet.Kamelet.PARAM_ROUTE_ID; import static org.apache.camel.component.kamelet.Kamelet.PARAM_TEMPLATE_ID; import static org.apache.camel.component.kamelet.Kamelet.PARAM_UUID; -import static org.apache.camel.component.kamelet.Kamelet.PARENT_PROCESSOR_ID; -import static org.apache.camel.component.kamelet.Kamelet.PARENT_ROUTE_ID; /** * Materialize route templates @@ -468,14 +466,8 @@ public class KameletComponent extends DefaultComponent { LOG.debug("Creating route from template={} and id={}", templateId, routeId); try { - // TODO: Nicer API - if (parentRouteId != null) { - endpoint.getKameletProperties().put(PARENT_ROUTE_ID, parentRouteId); - } - if (parentProcessorId != null) { - endpoint.getKameletProperties().put(PARENT_PROCESSOR_ID, parentProcessorId); - } - String id = context.addRouteFromTemplate(routeId, templateId, uuid, endpoint.getKameletProperties()); + String id = context.addRouteFromKamelet(routeId, templateId, uuid, parentRouteId, parentProcessorId, + endpoint.getKameletProperties()); RouteDefinition def = context.getRouteDefinition(id); // start the route if not already started 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 16103a4ff5f..c67f56a7a81 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 @@ -646,6 +646,24 @@ public interface CamelContext extends CamelContextLifecycle, RuntimeConfiguratio String routeId, String routeTemplateId, String prefixId, RouteTemplateContext routeTemplateContext) throws Exception; + /** + * Adds a new route from a given kamelet + * + * @param routeId the id of the new route to add (optional) + * @param routeTemplateId the id of the kamelet route template (mandatory) + * @param prefixId prefix to use for all node ids (not route id). Use null for no prefix. (optional) + * @param parentRouteId the id of the route which is using the kamelet (such as from / to) + * @param parentProcessorId the id of the processor which is using the kamelet (such as to) + * @param parameters parameters to use for the route template when creating the new route + * @return the id of the route added (for example when an id was auto assigned) + * @throws Exception is thrown if error creating and adding the new route + */ + String addRouteFromKamelet( + String routeId, String routeTemplateId, String prefixId, + String parentRouteId, String parentProcessorId, + Map<String, Object> parameters) + throws Exception; + /** * Removes the route templates matching the pattern * diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java index 77cb3275d65..8cdc1ecf083 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java @@ -773,6 +773,14 @@ public class SimpleCamelContext extends AbstractCamelContext { throw new UnsupportedOperationException(); } + @Override + public String addRouteFromKamelet( + String routeId, String routeTemplateId, String prefixId, String parentRouteId, String parentProcessorId, + Map<String, Object> parameters) + throws Exception { + throw new UnsupportedOperationException(); + } + @Override public void removeRouteTemplates(String pattern) throws Exception { throw new UnsupportedOperationException(); diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java index 9c4a6c73c7e..6b381c80369 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java @@ -316,13 +316,11 @@ public class DefaultCamelContext extends SimpleCamelContext implements ModelCame @Override public void addRouteTemplateDefinition(RouteTemplateDefinition routeTemplateDefinition) throws Exception { - model.addRouteTemplateDefinition(routeTemplateDefinition); } @Override public void removeRouteTemplateDefinitions(Collection<RouteTemplateDefinition> routeTemplateDefinitions) throws Exception { - if (!isLockModel()) { model.removeRouteTemplateDefinitions(routeTemplateDefinitions); } @@ -344,21 +342,18 @@ public class DefaultCamelContext extends SimpleCamelContext implements ModelCame @Override public void addRouteTemplateDefinitionConverter(String templateIdPattern, RouteTemplateDefinition.Converter converter) { - model.addRouteTemplateDefinitionConverter(templateIdPattern, converter); } @Override public String addRouteFromTemplate(String routeId, String routeTemplateId, Map<String, Object> parameters) throws Exception { - return model.addRouteFromTemplate(routeId, routeTemplateId, parameters); } @Override public String addRouteFromTemplate(String routeId, String routeTemplateId, String prefixId, Map<String, Object> parameters) throws Exception { - return model.addRouteFromTemplate(routeId, routeTemplateId, prefixId, parameters); } @@ -366,20 +361,30 @@ public class DefaultCamelContext extends SimpleCamelContext implements ModelCame public String addRouteFromTemplate( String routeId, String routeTemplateId, String prefixId, RouteTemplateContext routeTemplateContext) throws Exception { - return model.addRouteFromTemplate(routeId, routeTemplateId, prefixId, routeTemplateContext); } @Override - public void addRouteFromTemplatedRoute(TemplatedRouteDefinition templatedRouteDefinition) + public String addRouteFromKamelet( + String routeId, String routeTemplateId, String prefixId, + String parentRouteId, String parentProcessorId, Map<String, Object> parameters) throws Exception { + return model.addRouteFromKamelet(routeId, routeTemplateId, prefixId, parentRouteId, parentProcessorId, parameters); + } + @Override + public void addRouteFromTemplatedRoutes(Collection<TemplatedRouteDefinition> templatedRouteDefinitions) throws Exception { + model.addRouteFromTemplatedRoutes(templatedRouteDefinitions); + } + + @Override + public void addRouteFromTemplatedRoute(TemplatedRouteDefinition templatedRouteDefinition) + throws Exception { model.addRouteFromTemplatedRoute(templatedRouteDefinition); } @Override public void removeRouteTemplates(String pattern) throws Exception { - if (!isLockModel()) { model.removeRouteTemplateDefinitions(pattern); } @@ -856,7 +861,6 @@ public class DefaultCamelContext extends SimpleCamelContext implements ModelCame this.getCamelContextExtension().getBootstrapFactoryFinder(), ModelReifierFactory.FACTORY, ModelReifierFactory.class); - return result.orElseGet(DefaultModelReifierFactory::new); } diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java index ab43c9370e8..7da3b7d416e 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java @@ -428,9 +428,26 @@ public class DefaultModel implements Model { String routeId, String routeTemplateId, String prefixId, RouteTemplateContext routeTemplateContext) throws Exception { + return doAddRouteFromTemplate(routeId, routeTemplateId, prefixId, null, null, routeTemplateContext); + } - String parentRouteId = (String) routeTemplateContext.getParameters().remove("_parentRouteId"); - String parentProcessorId = (String) routeTemplateContext.getParameters().remove("_parentProcessorId"); + @Override + public String addRouteFromKamelet( + String routeId, String routeTemplateId, String prefixId, + String parentRouteId, String parentProcessorId, Map<String, Object> parameters) + throws Exception { + RouteTemplateContext rtc = new DefaultRouteTemplateContext(camelContext); + if (parameters != null) { + parameters.forEach(rtc::setParameter); + } + return doAddRouteFromTemplate(routeId, routeTemplateId, prefixId, parentRouteId, parentProcessorId, rtc); + } + + protected String doAddRouteFromTemplate( + String routeId, String routeTemplateId, String prefixId, + String parentRouteId, String parentProcessorId, + RouteTemplateContext routeTemplateContext) + throws Exception { RouteTemplateDefinition target = null; for (RouteTemplateDefinition def : routeTemplateDefinitions) { @@ -519,10 +536,10 @@ public class DefaultModel implements Model { } if (parentRouteId != null) { - prop.put("parentRouteId", parentRouteId); + addProperty(prop, "parentRouteId", parentRouteId); } if (parentProcessorId != null) { - prop.put("parentProcessorId", parentProcessorId); + addProperty(prop, "parentProcessorId", parentProcessorId); } RouteDefinition def = converter.apply(target, prop); if (routeId != null) { diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/Model.java b/core/camel-core-model/src/main/java/org/apache/camel/model/Model.java index efbf693be2a..e225426a324 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/Model.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/Model.java @@ -253,6 +253,24 @@ public interface Model { RouteTemplateContext routeTemplateContext) throws Exception; + /** + * Adds a new route from a given kamelet + * + * @param routeId the id of the new route to add (optional) + * @param routeTemplateId the id of the kamelet route template (mandatory) + * @param prefixId prefix to use when assigning route and node IDs (optional) + * @param parentRouteId the id of the route which is using the kamelet (such as from / to) + * @param parentProcessorId the id of the processor which is using the kamelet (such as to) + * @param parameters parameters to use for the route template when creating the new route + * @return the id of the route added (for example when an id was auto assigned) + * @throws Exception is thrown if error creating and adding the new route + */ + String addRouteFromKamelet( + String routeId, String routeTemplateId, String prefixId, + String parentRouteId, String parentProcessorId, + Map<String, Object> parameters) + throws Exception; + /** * Adds a new route from a given templated route definition *
