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);
}
}
}
}
}
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)));
}
}
Aggregations