Search in sources :

Example 1 with AddToOpenAPIDefinitionBuildItem

use of io.quarkus.smallrye.openapi.deployment.spi.AddToOpenAPIDefinitionBuildItem 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)1 BuildStep (io.quarkus.deployment.annotations.BuildStep)1 AddToOpenAPIDefinitionBuildItem (io.quarkus.smallrye.openapi.deployment.spi.AddToOpenAPIDefinitionBuildItem)1 ArrayList (java.util.ArrayList)1 CamelContext (org.apache.camel.CamelContext)1 RoutesBuilder (org.apache.camel.RoutesBuilder)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 DefaultRoutesCollector (org.apache.camel.main.DefaultRoutesCollector)1 RoutesConfigurer (org.apache.camel.main.RoutesConfigurer)1 CamelRoutesBuilderClassBuildItem (org.apache.camel.quarkus.core.deployment.spi.CamelRoutesBuilderClassBuildItem)1