Search in sources :

Example 1 with Resource

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));
}
Also used : CamelContext(org.apache.camel.CamelContext) ExtendedCamelContext(org.apache.camel.ExtendedCamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) RouteBuilder(org.apache.camel.builder.RouteBuilder) ArrayList(java.util.ArrayList) Ordered(org.apache.camel.Ordered) Resource(org.apache.camel.spi.Resource) RouteTemplateDefinition(org.apache.camel.model.RouteTemplateDefinition) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContextCustomizerBuildItem(org.apache.camel.quarkus.core.deployment.spi.CamelContextCustomizerBuildItem) ExtendedCamelContext(org.apache.camel.ExtendedCamelContext) RoutesBuilder(org.apache.camel.RoutesBuilder) Record(io.quarkus.deployment.annotations.Record) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 2 with Resource

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);
                }
            }
        }
    }
}
Also used : CamelSupport(org.apache.camel.quarkus.core.deployment.util.CamelSupport) Resource(org.apache.camel.spi.Resource) Logger(org.jboss.logging.Logger) SAXParserFactory(javax.xml.parsers.SAXParserFactory) LanguageExpressionContentHandler(org.apache.camel.quarkus.core.deployment.LanguageExpressionContentHandler) FileNotFoundException(java.io.FileNotFoundException) BuildProducer(io.quarkus.deployment.annotations.BuildProducer) BuildStep(io.quarkus.deployment.annotations.BuildStep) Stream(java.util.stream.Stream) DefaultPackageScanResourceResolver(org.apache.camel.impl.engine.DefaultPackageScanResourceResolver) SAXParser(javax.xml.parsers.SAXParser) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) AntPathMatcher(org.apache.camel.util.AntPathMatcher) InputStream(java.io.InputStream) LanguageExpressionContentHandler(org.apache.camel.quarkus.core.deployment.LanguageExpressionContentHandler) InputStream(java.io.InputStream) DefaultPackageScanResourceResolver(org.apache.camel.impl.engine.DefaultPackageScanResourceResolver) Resource(org.apache.camel.spi.Resource) FileNotFoundException(java.io.FileNotFoundException) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) FileNotFoundException(java.io.FileNotFoundException) SAXParser(javax.xml.parsers.SAXParser) Stream(java.util.stream.Stream) InputStream(java.io.InputStream) SAXParserFactory(javax.xml.parsers.SAXParserFactory) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 3 with Resource

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);
    }
}
Also used : RuntimeCamelException(org.apache.camel.RuntimeCamelException) RouteDefinition(org.apache.camel.model.RouteDefinition) RoutesBuilder(org.apache.camel.RoutesBuilder) RouteTemplateDefinition(org.apache.camel.model.RouteTemplateDefinition) SourcesConfigurer(org.apache.camel.k.listener.SourcesConfigurer) Logger(org.slf4j.Logger) Resource(org.apache.camel.spi.Resource) AbstractPhaseListener(org.apache.camel.k.listener.AbstractPhaseListener) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) ExtendedCamelContext(org.apache.camel.ExtendedCamelContext) List(java.util.List) SourceDefinition(org.apache.camel.k.SourceDefinition) RouteBuilder(org.apache.camel.builder.RouteBuilder) Source(org.apache.camel.k.Source) RuntimeAware(org.apache.camel.k.RuntimeAware) Runtime(org.apache.camel.k.Runtime) ObjectHelper(org.apache.camel.util.ObjectHelper) RouteBuilderLifecycleStrategy(org.apache.camel.builder.RouteBuilderLifecycleStrategy) RuntimeAware(org.apache.camel.k.RuntimeAware) RouteBuilder(org.apache.camel.builder.RouteBuilder) Resource(org.apache.camel.spi.Resource) RouteTemplateDefinition(org.apache.camel.model.RouteTemplateDefinition) RuntimeCamelException(org.apache.camel.RuntimeCamelException) RouteDefinition(org.apache.camel.model.RouteDefinition) ExtendedCamelContext(org.apache.camel.ExtendedCamelContext) RouteBuilderLifecycleStrategy(org.apache.camel.builder.RouteBuilderLifecycleStrategy) RoutesBuilder(org.apache.camel.RoutesBuilder)

Aggregations

Resource (org.apache.camel.spi.Resource)3 BuildStep (io.quarkus.deployment.annotations.BuildStep)2 ExtendedCamelContext (org.apache.camel.ExtendedCamelContext)2 RoutesBuilder (org.apache.camel.RoutesBuilder)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 RouteTemplateDefinition (org.apache.camel.model.RouteTemplateDefinition)2 BuildProducer (io.quarkus.deployment.annotations.BuildProducer)1 Record (io.quarkus.deployment.annotations.Record)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Stream (java.util.stream.Stream)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 CamelContext (org.apache.camel.CamelContext)1 Ordered (org.apache.camel.Ordered)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1