Search in sources :

Example 1 with LambdaRouteBuilder

use of org.apache.camel.builder.LambdaRouteBuilder in project camel-quarkus by apache.

the class CamelContextRecorder method addRoutes.

public void addRoutes(RuntimeValue<CamelContext> context, List<String> nonCdiRoutesBuilderClassNames) {
    List<RoutesBuilder> allRoutesBuilders = new ArrayList<>();
    try {
        for (String nonCdiRoutesBuilderClassName : nonCdiRoutesBuilderClassNames) {
            Class<RoutesBuilder> nonCdiRoutesBuilderClass = context.getValue().getClassResolver().resolveClass(nonCdiRoutesBuilderClassName, RoutesBuilder.class);
            allRoutesBuilders.add(context.getValue().getInjector().newInstance(nonCdiRoutesBuilderClass));
        }
        for (LambdaRouteBuilder builder : context.getValue().getRegistry().findByType(LambdaRouteBuilder.class)) {
            allRoutesBuilders.add(new RouteBuilder() {

                @Override
                public void configure() throws Exception {
                    builder.accept(this);
                }
            });
        }
        for (RoutesBuilder cdiRoutesBuilder : context.getValue().getRegistry().findByType(RoutesBuilder.class)) {
            allRoutesBuilders.add(cdiRoutesBuilder);
        }
        // Add RouteConfigurationsBuilders before RoutesBuilders
        for (RoutesBuilder routesBuilder : allRoutesBuilders) {
            if (routesBuilder instanceof RouteConfigurationsBuilder) {
                context.getValue().addRoutesConfigurations((RouteConfigurationsBuilder) routesBuilder);
            }
        }
        for (RoutesBuilder routesBuilder : allRoutesBuilders) {
            if (!(routesBuilder instanceof RouteConfigurationsBuilder)) {
                context.getValue().addRoutes(routesBuilder);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : RouteConfigurationsBuilder(org.apache.camel.RouteConfigurationsBuilder) LambdaRouteBuilder(org.apache.camel.builder.LambdaRouteBuilder) RouteBuilder(org.apache.camel.builder.RouteBuilder) ArrayList(java.util.ArrayList) LambdaRouteBuilder(org.apache.camel.builder.LambdaRouteBuilder) RoutesBuilder(org.apache.camel.RoutesBuilder)

Example 2 with LambdaRouteBuilder

use of org.apache.camel.builder.LambdaRouteBuilder in project camel-spring-boot by apache.

the class SpringBootRoutesCollector method collectRoutesFromRegistry.

@Override
public List<RoutesBuilder> collectRoutesFromRegistry(final CamelContext camelContext, final String excludePattern, final String includePattern) {
    final List<RoutesBuilder> routes = new ArrayList<>();
    Set<LambdaRouteBuilder> lrbs = camelContext.getRegistry().findByType(LambdaRouteBuilder.class);
    for (LambdaRouteBuilder lrb : lrbs) {
        RouteBuilder rb = new RouteBuilder() {

            @Override
            public void configure() throws Exception {
                lrb.accept(this);
            }
        };
        routes.add(rb);
    }
    final AntPathMatcher matcher = new AntPathMatcher();
    for (RoutesBuilder routesBuilder : applicationContext.getBeansOfType(RoutesBuilder.class, true, true).values()) {
        // filter out abstract classes
        boolean abs = Modifier.isAbstract(routesBuilder.getClass().getModifiers());
        if (!abs) {
            String name = routesBuilder.getClass().getName();
            // make name as path so we can use ant path matcher
            name = name.replace('.', '/');
            boolean match = !"false".equals(includePattern);
            // special support for testing with @ExcludeRoutes annotation with camel-test modules
            String exclude = camelContext.adapt(ExtendedCamelContext.class).getTestExcludeRoutes();
            // exclude take precedence over include
            if (match && ObjectHelper.isNotEmpty(exclude)) {
                // this property is a comma separated list of FQN class names, so we need to make
                // name as path so we can use ant patch matcher
                exclude = exclude.replace('.', '/');
                // there may be multiple separated by comma
                String[] parts = exclude.split(",");
                for (String part : parts) {
                    // must negate when excluding, and hence !
                    match = !matcher.match(part, name);
                    log.trace("Java RoutesBuilder: {} exclude filter: {} -> {}", name, part, match);
                    if (!match) {
                        break;
                    }
                }
            }
            // exclude take precedence over include
            if (match && ObjectHelper.isNotEmpty(excludePattern)) {
                // there may be multiple separated by comma
                String[] parts = excludePattern.split(",");
                for (String part : parts) {
                    // must negate when excluding, and hence !
                    match = !matcher.match(part, name);
                    log.trace("Java RoutesBuilder: {} exclude filter: {} -> {}", name, part, match);
                    if (!match) {
                        break;
                    }
                }
            }
            if (match && ObjectHelper.isNotEmpty(includePattern)) {
                // there may be multiple separated by comma
                String[] parts = includePattern.split(",");
                for (String part : parts) {
                    match = matcher.match(part, name);
                    log.trace("Java RoutesBuilder: {} include filter: {} -> {}", name, part, match);
                    if (match) {
                        break;
                    }
                }
            }
            log.debug("Java RoutesBuilder: {} accepted by include/exclude filter: {}", name, match);
            if (match) {
                routes.add(routesBuilder);
            }
        }
    }
    return routes;
}
Also used : LambdaRouteBuilder(org.apache.camel.builder.LambdaRouteBuilder) RouteBuilder(org.apache.camel.builder.RouteBuilder) ExtendedCamelContext(org.apache.camel.ExtendedCamelContext) ArrayList(java.util.ArrayList) LambdaRouteBuilder(org.apache.camel.builder.LambdaRouteBuilder) AntPathMatcher(org.apache.camel.util.AntPathMatcher) RoutesBuilder(org.apache.camel.RoutesBuilder)

Aggregations

ArrayList (java.util.ArrayList)2 RoutesBuilder (org.apache.camel.RoutesBuilder)2 LambdaRouteBuilder (org.apache.camel.builder.LambdaRouteBuilder)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 ExtendedCamelContext (org.apache.camel.ExtendedCamelContext)1 RouteConfigurationsBuilder (org.apache.camel.RouteConfigurationsBuilder)1 AntPathMatcher (org.apache.camel.util.AntPathMatcher)1