in camel-k-core/support/src/main/java/org/apache/camel/k/support/SourcesSupport.java [91:178]
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 error handler {} 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());
}
if (hasErrorHandlerFactory(builder)){
LOGGER.debug("Setting default error handler builder factory as type {}", builder.getErrorHandlerFactory().getClass());
runtime.getExtendedCamelContext().setErrorHandlerFactory(builder.getErrorHandlerFactory());
}
}
});
break;
default:
throw new IllegalArgumentException("Unknown source type: " + source.getType());
}
try {
final Resource resource = Sources.asResource(runtime.getCamelContext(), source);
final ExtendedCamelContext ecc = runtime.getExtendedCamelContext();
final Collection<RoutesBuilder> builders = PluginHelper.getRoutesLoader(ecc).findRoutesBuilders(resource);
builders.stream()
.map(RouteBuilder.class::cast)
.peek(b -> interceptors.forEach(b::addLifecycleInterceptor))
.forEach(runtime::addRoutes);
} catch (Exception e) {
throw RuntimeCamelException.wrapRuntimeCamelException(e);
}
}