From 671116f0a8a18dd4405a80cf225cd6c3b2b6a903 Mon Sep 17 00:00:00 2001 From: Luca Burgazzoli Date: Wed, 17 Aug 2022 18:19:06 +0200 Subject: [PATCH] feat: remove template and error handler source types --- .../main/java/org/apache/camel/k/Source.java | 4 + .../org/apache/camel/k/SourceDefinition.java | 4 + .../camel/k/support/RuntimeSupport.java | 14 ++- .../camel/k/support/SourcesSupport.java | 105 +----------------- .../src/test/resources/routes.properties | 4 - .../src/test/resources/routes/set-body.yaml | 14 ++- .../src/test/resources/routes/to-upper.yaml | 18 +-- 7 files changed, 43 insertions(+), 120 deletions(-) diff --git a/camel-k-core/api/src/main/java/org/apache/camel/k/Source.java b/camel-k-core/api/src/main/java/org/apache/camel/k/Source.java index 609d61247..a11fb1612 100644 --- a/camel-k-core/api/src/main/java/org/apache/camel/k/Source.java +++ b/camel-k-core/api/src/main/java/org/apache/camel/k/Source.java @@ -31,9 +31,13 @@ public interface Source extends HasId { String getLocation(); String getName(); String getLanguage(); + + @Deprecated SourceType getType(); Optional getLoader(); List getInterceptors(); + + @Deprecated List getPropertyNames(); InputStream resolveAsInputStream(CamelContext ctx); diff --git a/camel-k-core/api/src/main/java/org/apache/camel/k/SourceDefinition.java b/camel-k-core/api/src/main/java/org/apache/camel/k/SourceDefinition.java index dbc8784d1..f302f3df2 100644 --- a/camel-k-core/api/src/main/java/org/apache/camel/k/SourceDefinition.java +++ b/camel-k-core/api/src/main/java/org/apache/camel/k/SourceDefinition.java @@ -92,6 +92,7 @@ public void setInterceptors(List interceptors) { this.interceptors = interceptors; } + @Deprecated public SourceType getType() { return type; } @@ -99,10 +100,12 @@ public SourceType getType() { /** * The {@link SourceType} of the source. */ + @Deprecated public void setType(SourceType type) { this.type = type; } + @Deprecated public List getPropertyNames() { return propertyNames; } @@ -110,6 +113,7 @@ public List getPropertyNames() { /** * The list of properties names the source requires (used only for templates). */ + @Deprecated public void setPropertyNames(List propertyNames) { this.propertyNames = propertyNames; } diff --git a/camel-k-core/support/src/main/java/org/apache/camel/k/support/RuntimeSupport.java b/camel-k-core/support/src/main/java/org/apache/camel/k/support/RuntimeSupport.java index e947eda9c..cb521460c 100644 --- a/camel-k-core/support/src/main/java/org/apache/camel/k/support/RuntimeSupport.java +++ b/camel-k-core/support/src/main/java/org/apache/camel/k/support/RuntimeSupport.java @@ -44,6 +44,8 @@ import org.apache.camel.ExtendedCamelContext; import org.apache.camel.builder.RouteBuilderLifecycleStrategy; import org.apache.camel.k.ContextCustomizer; +import org.apache.camel.k.Runtime; +import org.apache.camel.k.RuntimeAware; import org.apache.camel.k.Source; import org.apache.camel.spi.HasCamelContext; import org.apache.camel.util.IOHelper; @@ -177,8 +179,8 @@ public static Set lookupCustomizerIDs(CamelContext context) { // // ********************************* - public static List loadInterceptors(CamelContext context, Source source) { - ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class); + public static List loadInterceptors(Runtime runtime, Source source) { + ExtendedCamelContext ecc = runtime.getCamelContext().adapt(ExtendedCamelContext.class); List answer = new ArrayList<>(); for (String id : source.getInterceptors()) { @@ -198,8 +200,12 @@ public static List loadInterceptors(CamelContext LOGGER.debug("Found source loader interceptor {} from registry", id); } - PropertiesSupport.bindProperties(context, interceptor, Constants.LOADER_INTERCEPTOR_PREFIX + id + "."); - PropertiesSupport.bindProperties(context, interceptor, Constants.LOADER_INTERCEPTOR_PREFIX_FALLBACK + id + "."); + PropertiesSupport.bindProperties(ecc, interceptor, Constants.LOADER_INTERCEPTOR_PREFIX + id + "."); + PropertiesSupport.bindProperties(ecc, interceptor, Constants.LOADER_INTERCEPTOR_PREFIX_FALLBACK + id + "."); + + if (interceptor instanceof RuntimeAware) { + ((RuntimeAware) interceptor).setRuntime(runtime); + } answer.add(interceptor); } catch (Exception e) { diff --git a/camel-k-core/support/src/main/java/org/apache/camel/k/support/SourcesSupport.java b/camel-k-core/support/src/main/java/org/apache/camel/k/support/SourcesSupport.java index 80e9ad5bb..aefeaf6f6 100644 --- a/camel-k-core/support/src/main/java/org/apache/camel/k/support/SourcesSupport.java +++ b/camel-k-core/support/src/main/java/org/apache/camel/k/support/SourcesSupport.java @@ -16,6 +16,7 @@ */ package org.apache.camel.k.support; +import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -25,13 +26,9 @@ import org.apache.camel.builder.RouteBuilder; import org.apache.camel.builder.RouteBuilderLifecycleStrategy; import org.apache.camel.k.Runtime; -import org.apache.camel.k.RuntimeAware; import org.apache.camel.k.Source; import org.apache.camel.k.SourceDefinition; -import org.apache.camel.k.listener.AbstractPhaseListener; import org.apache.camel.k.listener.SourcesConfigurer; -import org.apache.camel.model.RouteDefinition; -import org.apache.camel.model.RouteTemplateDefinition; import org.apache.camel.spi.Resource; import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; @@ -43,24 +40,6 @@ public final class SourcesSupport { private SourcesSupport() { } - public static Runtime.Listener forRoutes(String... sources) { - return new AbstractPhaseListener(Runtime.Phase.ConfigureRoutes) { - @Override - protected void accept(Runtime runtime) { - loadSources(runtime, sources); - } - }; - } - - public static Runtime.Listener forRoutes(SourceDefinition... definitions) { - return new AbstractPhaseListener(Runtime.Phase.ConfigureRoutes) { - @Override - protected void accept(Runtime runtime) { - loadSources(runtime, definitions); - } - }; - } - public static void loadSources(Runtime runtime, String... routes) { for (String route : routes) { if (ObjectHelper.isEmpty(route)) { @@ -86,80 +65,11 @@ public static void loadSources(Runtime runtime, SourceDefinition... definitions) } public static void load(Runtime runtime, Source source) { - final List interceptors; - - switch (source.getType()) { - case source: - interceptors = RuntimeSupport.loadInterceptors(runtime.getCamelContext(), source); - interceptors.forEach(interceptor -> { - if (interceptor instanceof RuntimeAware) { - ((RuntimeAware) interceptor).setRuntime(runtime); - } - }); - - break; - case template: - if (!source.getInterceptors().isEmpty()) { - LOGGER.warn("Interceptors associated to the route template {} will be ignored", source.getName()); - } - - interceptors = List.of(new RouteBuilderLifecycleStrategy() { - @Override - public void afterConfigure(RouteBuilder builder) { - List routes = builder.getRouteCollection().getRoutes(); - List templates = builder.getRouteTemplateCollection().getRouteTemplates(); - - if (routes.size() != 1) { - throw new IllegalArgumentException( - "There should be a single route definition when configuring route templates, got " + routes.size()); - } - if (!templates.isEmpty()) { - throw new IllegalArgumentException( - "There should not be any template definition when configuring route templates, got " + templates.size()); - } - - // create a new template from the source - RouteTemplateDefinition templatesDefinition = builder.getRouteTemplateCollection().routeTemplate(source.getId()); - templatesDefinition.setRoute(routes.get(0)); - - source.getPropertyNames().forEach(templatesDefinition::templateParameter); - - // remove all routes definitions as they have been translated - // in the related route template - routes.clear(); - } - }); - break; - case errorHandler: - if (!source.getInterceptors().isEmpty()) { - LOGGER.warn("Interceptors associated to the route template {} will be ignored", source.getName()); - } - - interceptors = List.of(new RouteBuilderLifecycleStrategy() { - @Override - public void afterConfigure(RouteBuilder builder) { - List routes = builder.getRouteCollection().getRoutes(); - List templates = builder.getRouteTemplateCollection().getRouteTemplates(); - - if (!routes.isEmpty()) { - throw new IllegalArgumentException( - "There should be no route definition when configuring error handler, got " + routes.size()); - } - if (!templates.isEmpty()) { - throw new IllegalArgumentException( - "There should not be any template definition when configuring error handler, got " + templates.size()); - } - - LOGGER.debug("Setting default error handler builder factory as {}", builder.getErrorHandlerFactory()); - runtime.getCamelContext().adapt(ExtendedCamelContext.class).setErrorHandlerFactory(builder.getErrorHandlerFactory()); - } - }); - break; - default: - throw new IllegalArgumentException("Unknown source type: " + source.getType()); - } - try { + final List interceptors = new ArrayList<>(); + interceptors.addAll(RuntimeSupport.loadInterceptors(runtime, source)); + interceptors.addAll(runtime.getCamelContext().getRegistry().findByType(RouteBuilderLifecycleStrategy.class)); + final Resource resource = Sources.asResource(runtime.getCamelContext(), source); final ExtendedCamelContext ecc = runtime.getCamelContext(ExtendedCamelContext.class); final Collection builders = ecc.getRoutesLoader().findRoutesBuilders(resource); @@ -172,9 +82,4 @@ public void afterConfigure(RouteBuilder builder) { throw RuntimeCamelException.wrapRuntimeCamelException(e); } } - - public static void loadErrorHandlerSource(Runtime runtime, SourceDefinition errorHandlerSourceDefinition) { - LOGGER.info("Loading error handler from: {}", errorHandlerSourceDefinition); - load(runtime, Sources.fromDefinition(errorHandlerSourceDefinition)); - } } diff --git a/itests/camel-k-itests-kamelet/src/test/resources/routes.properties b/itests/camel-k-itests-kamelet/src/test/resources/routes.properties index 8ffa3bd53..6c811e301 100644 --- a/itests/camel-k-itests-kamelet/src/test/resources/routes.properties +++ b/itests/camel-k-itests-kamelet/src/test/resources/routes.properties @@ -20,11 +20,7 @@ # camel-k - sources (templates) # camel.k.sources[0].location = file:{{env:ROUTES_DIR}}/set-body.yaml -camel.k.sources[0].type = template -camel.k.sources[0].property-names[0] = bodyValue camel.k.sources[1].location = file:{{env:ROUTES_DIR}}/to-upper.yaml -camel.k.sources[1].type = template -camel.k.sources[1].property-names[0] = message # # camel-k - sources (routes) diff --git a/itests/camel-k-itests-kamelet/src/test/resources/routes/set-body.yaml b/itests/camel-k-itests-kamelet/src/test/resources/routes/set-body.yaml index c311da17c..fd8c6062d 100644 --- a/itests/camel-k-itests-kamelet/src/test/resources/routes/set-body.yaml +++ b/itests/camel-k-itests-kamelet/src/test/resources/routes/set-body.yaml @@ -15,8 +15,12 @@ # limitations under the License. # -- from: - uri: "kamelet:source" - steps: - - set-body: - constant: "{{bodyValue}}" \ No newline at end of file +- template: + id: "set-body" + parameters: + - name: "bodyValue" + from: + uri: "kamelet:source" + steps: + - set-body: + constant: "{{bodyValue}}" \ No newline at end of file diff --git a/itests/camel-k-itests-kamelet/src/test/resources/routes/to-upper.yaml b/itests/camel-k-itests-kamelet/src/test/resources/routes/to-upper.yaml index ba51838b7..b803b7132 100644 --- a/itests/camel-k-itests-kamelet/src/test/resources/routes/to-upper.yaml +++ b/itests/camel-k-itests-kamelet/src/test/resources/routes/to-upper.yaml @@ -15,10 +15,14 @@ # limitations under the License. # -- from: - uri: "kamelet:source" - steps: - - set-body: - constant: "{{message}}" - - set-body: - simple: "${in.body.toUpperCase()}" \ No newline at end of file +- template: + id: "to-upper" + parameters: + - name: "message" + from: + uri: "kamelet:source" + steps: + - set-body: + constant: "{{message}}" + - set-body: + simple: "${in.body.toUpperCase()}" \ No newline at end of file