Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove template and error handler source types #868

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions camel-k-core/api/src/main/java/org/apache/camel/k/Source.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ public interface Source extends HasId {
String getLocation();
String getName();
String getLanguage();

@Deprecated
SourceType getType();
Optional<String> getLoader();
List<String> getInterceptors();

@Deprecated
List<String> getPropertyNames();
InputStream resolveAsInputStream(CamelContext ctx);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,28 @@ public void setInterceptors(List<String> interceptors) {
this.interceptors = interceptors;
}

@Deprecated
public SourceType getType() {
return type;
}

/**
* The {@link SourceType} of the source.
*/
@Deprecated
public void setType(SourceType type) {
this.type = type;
}

@Deprecated
public List<String> getPropertyNames() {
return propertyNames;
}

/**
* The list of properties names the source requires (used only for templates).
*/
@Deprecated
public void setPropertyNames(List<String> propertyNames) {
this.propertyNames = propertyNames;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -177,8 +179,8 @@ public static Set<String> lookupCustomizerIDs(CamelContext context) {
//
// *********************************

public static List<RouteBuilderLifecycleStrategy> loadInterceptors(CamelContext context, Source source) {
ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
public static List<RouteBuilderLifecycleStrategy> loadInterceptors(Runtime runtime, Source source) {
ExtendedCamelContext ecc = runtime.getCamelContext().adapt(ExtendedCamelContext.class);
List<RouteBuilderLifecycleStrategy> answer = new ArrayList<>();

for (String id : source.getInterceptors()) {
Expand All @@ -198,8 +200,12 @@ public static List<RouteBuilderLifecycleStrategy> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.camel.k.support;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

Expand All @@ -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;
Expand All @@ -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)) {
Expand All @@ -86,80 +65,11 @@ public static void loadSources(Runtime runtime, SourceDefinition... definitions)
}

public static void load(Runtime runtime, Source source) {
final List<RouteBuilderLifecycleStrategy> 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<RouteDefinition> routes = builder.getRouteCollection().getRoutes();
List<RouteTemplateDefinition> 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<RouteDefinition> routes = builder.getRouteCollection().getRoutes();
List<RouteTemplateDefinition> 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<RouteBuilderLifecycleStrategy> 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<RoutesBuilder> builders = ecc.getRoutesLoader().findRoutesBuilders(resource);
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
# limitations under the License.
#

- from:
uri: "kamelet:source"
steps:
- set-body:
constant: "{{bodyValue}}"
- template:
id: "set-body"
parameters:
- name: "bodyValue"
from:
uri: "kamelet:source"
steps:
- set-body:
constant: "{{bodyValue}}"
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
# limitations under the License.
#

- from:
uri: "kamelet:source"
steps:
- set-body:
constant: "{{message}}"
- set-body:
simple: "${in.body.toUpperCase()}"
- template:
id: "to-upper"
parameters:
- name: "message"
from:
uri: "kamelet:source"
steps:
- set-body:
constant: "{{message}}"
- set-body:
simple: "${in.body.toUpperCase()}"