use of org.apache.camel.builder.RouteBuilderLifecycleStrategy in project camel-k-runtime by apache.
the class SourcesSupport method load.
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, got " + routes.size());
}
if (!templates.isEmpty()) {
throw new IllegalArgumentException("There should not be any template, 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, got " + routes.size());
}
if (!templates.isEmpty()) {
throw new IllegalArgumentException("There should not be any template, got " + templates.size());
}
if (hasErrorHandlerBuilder(builder)) {
LOGGER.debug("Setting default error handler builder factory as {}", builder.getErrorHandlerBuilder());
runtime.getCamelContext().adapt(ExtendedCamelContext.class).setErrorHandlerFactory(builder.getErrorHandlerBuilder());
}
}
});
break;
default:
throw new IllegalArgumentException("Unknown source type: " + source.getType());
}
try {
final Resource resource = Sources.asResource(runtime.getCamelContext(), source);
final ExtendedCamelContext ecc = runtime.getCamelContext(ExtendedCamelContext.class);
final Collection<RoutesBuilder> builders = ecc.getRoutesLoader().findRoutesBuilders(resource);
builders.stream().map(RouteBuilder.class::cast).peek(b -> interceptors.forEach(b::addLifecycleInterceptor)).forEach(runtime::addRoutes);
} catch (Exception e) {
throw RuntimeCamelException.wrapRuntimeCamelException(e);
}
}
use of org.apache.camel.builder.RouteBuilderLifecycleStrategy in project camel-k-runtime by apache.
the class RuntimeSupport method loadInterceptors.
// *********************************
//
// Helpers - Interceptors
//
// *********************************
public static List<RouteBuilderLifecycleStrategy> loadInterceptors(CamelContext context, Source source) {
ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
List<RouteBuilderLifecycleStrategy> answer = new ArrayList<>();
for (String id : source.getInterceptors()) {
try {
// first check the registry
RouteBuilderLifecycleStrategy interceptor = ecc.getRegistry().lookupByNameAndType(id, RouteBuilderLifecycleStrategy.class);
if (interceptor == null) {
// then try with factory finder
interceptor = ecc.getFactoryFinder(Constants.SOURCE_LOADER_INTERCEPTOR_RESOURCE_PATH).newInstance(id, RouteBuilderLifecycleStrategy.class).orElseThrow(() -> new IllegalArgumentException("Unable to find source loader interceptor for: " + id));
LOGGER.debug("Found source loader interceptor {} from service definition", id);
} else {
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 + ".");
answer.add(interceptor);
} catch (Exception e) {
throw new IllegalArgumentException("Unable to find source loader interceptor for: " + id, e);
}
}
return answer;
}
Aggregations