private boolean addRouteToContext(
     Bean<?> routeBean, Bean<?> contextBean, BeanManager manager, AfterDeploymentValidation adv) {
   try {
     CamelContext context = getReference(manager, CamelContext.class, contextBean);
     try {
       Object route = getReference(manager, Object.class, routeBean);
       if (route instanceof RoutesBuilder) {
         context.addRoutes((RoutesBuilder) route);
       } else if (route instanceof RouteContainer) {
         context.addRouteDefinitions(((RouteContainer) route).getRoutes());
       } else {
         throw new IllegalArgumentException(
             "Invalid routes type ["
                 + routeBean.getBeanClass().getName()
                 + "], "
                 + "must be either of type RoutesBuilder or RouteContainer!");
       }
       return true;
     } catch (Exception cause) {
       adv.addDeploymentProblem(
           new InjectionException(
               "Error adding routes of type ["
                   + routeBean.getBeanClass().getName()
                   + "] "
                   + "to Camel context ["
                   + context.getName()
                   + "]",
               cause));
     }
   } catch (Exception exception) {
     adv.addDeploymentProblem(exception);
   }
   return false;
 }
  private void afterDeploymentValidation(
      @Observes AfterDeploymentValidation adv, BeanManager manager) {
    Collection<CamelContext> contexts = new ArrayList<>();
    for (Bean<?> context : manager.getBeans(CamelContext.class, ANY)) {
      contexts.add(getReference(manager, CamelContext.class, context));
    }

    // Add type converters to Camel contexts
    CdiTypeConverterLoader loader = new CdiTypeConverterLoader();
    for (Class<?> converter : converters) {
      for (CamelContext context : contexts) {
        loader.loadConverterMethods(context.getTypeConverterRegistry(), converter);
      }
    }

    // Add routes to Camel contexts
    boolean deploymentException = false;
    Set<Bean<?>> routes = new HashSet<>(manager.getBeans(RoutesBuilder.class, ANY));
    routes.addAll(manager.getBeans(RouteContainer.class, ANY));
    for (Bean<?> context : manager.getBeans(CamelContext.class, ANY)) {
      for (Bean<?> route : routes) {
        Set<Annotation> qualifiers = new HashSet<>(context.getQualifiers());
        qualifiers.retainAll(route.getQualifiers());
        if (qualifiers.size() > 1) {
          deploymentException |= !addRouteToContext(route, context, manager, adv);
        }
      }
    }
    // Let's return to avoid starting misconfigured contexts
    if (deploymentException) {
      return;
    }

    // Trigger eager beans instantiation (calling toString is necessary to force
    // the initialization of normal-scoped beans).
    // FIXME: This does not work with OpenWebBeans for bean whose bean type is an
    // interface as the Object methods does not get forwarded to the bean instances!
    eagerBeans.forEach(type -> getReferencesByType(manager, type.getJavaClass(), ANY).toString());
    manager
        .getBeans(Object.class, ANY, STARTUP)
        .stream()
        .forEach(bean -> getReference(manager, bean.getBeanClass(), bean).toString());

    // Start Camel contexts
    for (CamelContext context : contexts) {
      if (ServiceStatus.Started.equals(context.getStatus())) {
        continue;
      }
      logger.info("Camel CDI is starting Camel context [{}]", context.getName());
      try {
        context.start();
      } catch (Exception exception) {
        adv.addDeploymentProblem(exception);
      }
    }

    // Clean-up
    Stream.of(converters, camelBeans, eagerBeans, cdiBeans).forEach(Set::clear);
    Stream.of(producerBeans, producerQualifiers).forEach(Map::clear);
  }
  void cleanup(@Observes AfterDeploymentValidation event) {
    // Defensively clear maps to help with GC
    this.genericBeanObserverMethods.clear();
    this.genericBeanProducerFields.clear();
    this.genericBeanProducerMethods.clear();
    this.genericBeans.clear();
    this.genericConfigurationPoints.clear();

    this.genericInjectionTargets.clear();
    for (String s : errors) {
      event.addDeploymentProblem(new Exception(s));
    }
  }