Search in sources :

Example 1 with CamelRoutesBuilderClassBuildItem

use of org.apache.camel.quarkus.core.deployment.spi.CamelRoutesBuilderClassBuildItem in project camel-quarkus by apache.

the class CSimpleProcessor method collectCSimpleExpressions.

@BuildStep
void collectCSimpleExpressions(CamelConfig config, List<CamelRoutesBuilderClassBuildItem> routesBuilderClasses, BuildProducer<CSimpleExpressionSourceBuildItem> csimpleExpressions) throws ClassNotFoundException {
    if (!routesBuilderClasses.isEmpty()) {
        final ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (!(loader instanceof QuarkusClassLoader)) {
            throw new IllegalStateException(QuarkusClassLoader.class.getSimpleName() + " expected as the context class loader");
        }
        final ExpressionCollector collector = new ExpressionCollector(loader);
        final CamelContext ctx = new DefaultCamelContext();
        for (CamelRoutesBuilderClassBuildItem routesBuilderClass : routesBuilderClasses) {
            final String className = routesBuilderClass.getDotName().toString();
            final Class<?> cl = loader.loadClass(className);
            if (!RouteBuilder.class.isAssignableFrom(cl)) {
                LOG.warnf("CSimple language expressions occurring in %s won't be compiled at build time", cl);
            } else {
                try {
                    final RouteBuilder rb = (RouteBuilder) cl.getDeclaredConstructor().newInstance();
                    rb.setCamelContext(ctx);
                    try {
                        rb.configure();
                        collector.collect("csimple", (script, isPredicate) -> csimpleExpressions.produce(new CSimpleExpressionSourceBuildItem(script, isPredicate, className)), rb.getRouteCollection(), rb.getRestCollection());
                    } catch (Exception e) {
                        switch(config.csimple.onBuildTimeAnalysisFailure) {
                            case fail:
                                throw new RuntimeException("Could not extract CSimple expressions from " + className + ". You may want to set quarkus.camel.csimple.on-build-time-analysis-failure to warn or ignore if you do not use CSimple language in your routes", e);
                            case warn:
                                LOG.warnf(e, "Could not extract CSimple language expressions from the route definition %s in class %s.", rb, cl);
                                break;
                            case ignore:
                                LOG.debugf(e, "Could not extract CSimple language expressions from the route definition %s in class %s", rb, cl);
                                break;
                            default:
                                throw new IllegalStateException("Unexpected " + FailureRemedy.class.getSimpleName() + ": " + config.csimple.onBuildTimeAnalysisFailure);
                        }
                    }
                } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
                    throw new RuntimeException("Could not instantiate " + className, e);
                }
            }
        }
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) CamelRoutesBuilderClassBuildItem(org.apache.camel.quarkus.core.deployment.spi.CamelRoutesBuilderClassBuildItem) RouteBuilder(org.apache.camel.builder.RouteBuilder) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) JAXBException(javax.xml.bind.JAXBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) FailureRemedy(org.apache.camel.quarkus.core.CamelConfig.FailureRemedy) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 2 with CamelRoutesBuilderClassBuildItem

use of org.apache.camel.quarkus.core.deployment.spi.CamelRoutesBuilderClassBuildItem in project camel-quarkus by apache.

the class CamelRestOASFilter method exposeOpenAPI.

@BuildStep(onlyIf = ExposeOpenApiEnabled.class)
void exposeOpenAPI(List<CamelRoutesBuilderClassBuildItem> routesBuilderClasses, BuildProducer<AddToOpenAPIDefinitionBuildItem> openAPI, Capabilities capabilities) throws Exception {
    if (capabilities.isPresent(Capability.SMALLRYE_OPENAPI)) {
        RoutesConfigurer configurer = new RoutesConfigurer();
        List<RoutesBuilder> routes = new ArrayList<>();
        configurer.setRoutesBuilders(routes);
        configurer.setRoutesCollector(new DefaultRoutesCollector());
        configurer.setRoutesIncludePattern(CamelSupport.getOptionalConfigValue("camel.main.routes-include-pattern", String.class, null));
        configurer.setRoutesExcludePattern(CamelSupport.getOptionalConfigValue("camel.main.routes-exclude-pattern", String.class, null));
        final CamelContext ctx = new DefaultCamelContext();
        if (!routesBuilderClasses.isEmpty()) {
            final ClassLoader loader = Thread.currentThread().getContextClassLoader();
            if (!(loader instanceof QuarkusClassLoader)) {
                throw new IllegalStateException(QuarkusClassLoader.class.getSimpleName() + " expected as the context class loader");
            }
            for (CamelRoutesBuilderClassBuildItem routesBuilderClass : routesBuilderClasses) {
                final String className = routesBuilderClass.getDotName().toString();
                final Class<?> cl = loader.loadClass(className);
                if (RouteBuilder.class.isAssignableFrom(cl)) {
                    try {
                        final RouteBuilder rb = (RouteBuilder) cl.getDeclaredConstructor().newInstance();
                        routes.add(rb);
                    } catch (InstantiationException | IllegalAccessException e) {
                        throw new RuntimeException("Could not instantiate " + className, e);
                    }
                }
            }
        }
        try {
            configurer.configureRoutes(ctx);
        } catch (Exception e) {
            // ignore
            LOGGER.warn("config routes failed with " + e);
        }
        openAPI.produce(new AddToOpenAPIDefinitionBuildItem(new CamelRestOASFilter(ctx)));
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) CamelRoutesBuilderClassBuildItem(org.apache.camel.quarkus.core.deployment.spi.CamelRoutesBuilderClassBuildItem) AddToOpenAPIDefinitionBuildItem(io.quarkus.smallrye.openapi.deployment.spi.AddToOpenAPIDefinitionBuildItem) RouteBuilder(org.apache.camel.builder.RouteBuilder) ArrayList(java.util.ArrayList) RoutesConfigurer(org.apache.camel.main.RoutesConfigurer) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) DefaultRoutesCollector(org.apache.camel.main.DefaultRoutesCollector) RoutesBuilder(org.apache.camel.RoutesBuilder) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

QuarkusClassLoader (io.quarkus.bootstrap.classloading.QuarkusClassLoader)2 BuildStep (io.quarkus.deployment.annotations.BuildStep)2 CamelContext (org.apache.camel.CamelContext)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 CamelRoutesBuilderClassBuildItem (org.apache.camel.quarkus.core.deployment.spi.CamelRoutesBuilderClassBuildItem)2 AddToOpenAPIDefinitionBuildItem (io.quarkus.smallrye.openapi.deployment.spi.AddToOpenAPIDefinitionBuildItem)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 JAXBException (javax.xml.bind.JAXBException)1 RoutesBuilder (org.apache.camel.RoutesBuilder)1 DefaultRoutesCollector (org.apache.camel.main.DefaultRoutesCollector)1 RoutesConfigurer (org.apache.camel.main.RoutesConfigurer)1 FailureRemedy (org.apache.camel.quarkus.core.CamelConfig.FailureRemedy)1