use of org.apache.camel.spi.Resource in project camel-quarkus by apache.
the class KameletProcessor method configureTemplates.
@Record(ExecutionTime.STATIC_INIT)
@BuildStep
CamelContextCustomizerBuildItem configureTemplates(List<KameletResolverBuildItem> resolvers, KameletConfiguration configuration, KameletRecorder recorder) throws Exception {
List<RouteTemplateDefinition> definitions = new ArrayList<>();
List<KameletResolver> kameletResolvers = resolvers.stream().map(KameletResolverBuildItem::getResolver).sorted(Comparator.comparingInt(Ordered::getOrder)).collect(Collectors.toList());
CamelContext context = new DefaultCamelContext();
ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
for (String id : configuration.identifiers.orElse(Collections.emptyList())) {
for (KameletResolver resolver : kameletResolvers) {
final Optional<Resource> resource = resolver.resolve(id, ecc);
if (!resource.isPresent()) {
continue;
}
Collection<RoutesBuilder> rbs = ecc.getRoutesLoader().findRoutesBuilders(resource.get());
for (RoutesBuilder rb : rbs) {
RouteBuilder routeBuilder = (RouteBuilder) rb;
routeBuilder.configure();
if (routeBuilder.getRouteTemplateCollection().getRouteTemplates().size() == 0) {
throw new IllegalStateException("No kamelet template was created for " + "kamelet:" + id + ". It might be that the kamelet was malformed?");
} else if (routeBuilder.getRouteTemplateCollection().getRouteTemplates().size() > 1) {
throw new IllegalStateException("A kamelet is not supposed to create more than one route (" + "kamelet:" + id + "," + "routes: " + routeBuilder.getRouteTemplateCollection().getRouteTemplates().size() + ")");
}
definitions.add(routeBuilder.getRouteTemplateCollection().getRouteTemplates().get(0));
}
}
}
return new CamelContextCustomizerBuildItem(recorder.createTemplateLoaderCustomizer(definitions));
}
use of org.apache.camel.spi.Resource in project camel-quarkus by apache.
the class CSimpleXmlProcessor method collectCSimpleExpresions.
@BuildStep
void collectCSimpleExpresions(BuildProducer<CSimpleExpressionSourceBuildItem> csimpleExpressions) throws Exception {
final String[] includes = Stream.of("camel.main.routesIncludePattern", "camel.main.routes-include-pattern").map(prop -> CamelSupport.getOptionalConfigValue(prop, String[].class, new String[0])).flatMap(Stream::of).filter(path -> !path.equals("false")).toArray(String[]::new);
final String[] excludes = Stream.of("camel.main.routesExcludePattern", "camel.main.routes-exclude-pattern").map(prop -> CamelSupport.getOptionalConfigValue(prop, String[].class, new String[0])).flatMap(Stream::of).filter(path -> !path.equals("false")).toArray(String[]::new);
try (DefaultPackageScanResourceResolver resolver = new DefaultPackageScanResourceResolver()) {
resolver.setCamelContext(new DefaultCamelContext());
final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
final SAXParser saxParser = saxParserFactory.newSAXParser();
for (String include : includes) {
for (Resource resource : resolver.findResources(include)) {
if (AntPathMatcher.INSTANCE.anyMatch(excludes, resource.getLocation())) {
continue;
}
try (InputStream is = resource.getInputStream()) {
saxParser.parse(is, new LanguageExpressionContentHandler("csimple", (script, isPredicate) -> csimpleExpressions.produce(new CSimpleExpressionSourceBuildItem(script, isPredicate, "org.apache.camel.language.csimple.XmlRouteBuilder"))));
} catch (FileNotFoundException e) {
LOG.debugf("No XML routes found in %s. Skipping XML routes detection.", resource.getLocation());
} catch (Exception e) {
throw new RuntimeException("Could not analyze CSimple expressions in " + resource.getLocation(), e);
}
}
}
}
}
use of org.apache.camel.spi.Resource 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);
}
}
Aggregations