コード例 #1
0
  private void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager manager) {
    // The set of extra Camel CDI beans
    Set<SyntheticBean<?>> extraBeans = new HashSet<>();

    // Add beans from Camel XML resources
    for (ImportResource resource : resources) {
      XmlCdiBeanFactory factory = XmlCdiBeanFactory.with(manager, environment, this);
      for (String path : resource.value()) {
        try {
          extraBeans.addAll(factory.beansFrom(path));
        } catch (NoClassDefFoundError cause) {
          if (cause.getMessage().contains("AbstractCamelContextFactoryBean")) {
            logger.error(
                "Importing Camel XML requires to have the 'camel-core-xml' dependency in the classpath!");
          }
          throw cause;
        } catch (Exception cause) {
          abd.addDefinitionError(
              new InjectionException(
                  "Error while importing resource [" + getResource(path) + "]", cause));
        }
      }
    }

    // Camel contexts from the imported Camel XML
    concat(cdiBeans.stream(), extraBeans.stream())
        .filter(hasType(CamelContext.class))
        .map(Bean::getQualifiers)
        .forEach(contextQualifiers::addAll);

    // From the @ContextName qualifiers on RoutesBuilder and RouteContainer beans
    cdiBeans
        .stream()
        .filter(hasType(RoutesBuilder.class).or(hasType(RouteContainer.class)))
        .map(Bean::getQualifiers)
        .flatMap(Set::stream)
        .filter(isAnnotationType(ContextName.class))
        .filter(name -> !contextQualifiers.contains(name))
        .peek(contextQualifiers::add)
        .map(name -> camelContextBean(manager, ANY, name, APPLICATION_SCOPED))
        .forEach(extraBeans::add);

    Set<Bean<?>> allBeans = concat(cdiBeans.stream(), extraBeans.stream()).collect(toSet());
    Set<Bean<?>> contexts = allBeans.stream().filter(hasType(CamelContext.class)).collect(toSet());

    if (contexts.size() == 0 && shouldDeployDefaultCamelContext(allBeans)) {
      // Add @Default Camel context bean if any
      extraBeans.add(camelContextBean(manager, ANY, DEFAULT, APPLICATION_SCOPED));
    } else if (contexts.size() == 1) {
      // Add the @Default qualifier if there is only one Camel context bean
      Bean<?> context = contexts.iterator().next();
      if (!context.getQualifiers().contains(DEFAULT)) {
        // Only decorate if that's a programmatic bean
        if (context instanceof SyntheticBean) {
          ((SyntheticBean<?>) context).addQualifier(DEFAULT);
        }
      }
    }

    // Finally add the beans to the deployment
    extraBeans.forEach(abd::addBean);

    // Update the CDI Camel factory beans
    Set<Annotation> endpointQualifiers =
        cdiEventEndpoints
            .values()
            .stream()
            .map(CdiEventEndpoint::getQualifiers)
            .flatMap(Set::stream)
            .collect(toSet());
    Set<Annotation> templateQualifiers =
        contextQualifiers
            .stream()
            .filter(isAnnotationType(Default.class).or(isAnnotationType(Named.class)).negate())
            .collect(toSet());
    // TODO: would be more correct to add a bean for each Camel context bean
    producerBeans
        .entrySet()
        .stream()
        .map(
            producer ->
                new BeanDelegate<>(
                    producer.getValue(),
                    producerQualifiers.get(producer.getKey()),
                    CdiEventEndpoint.class.equals(producer.getKey().getReturnType())
                        ? endpointQualifiers
                        : templateQualifiers))
        .forEach(abd::addBean);

    // Add CDI event endpoint observer methods
    cdiEventEndpoints
        .values()
        .stream()
        .map(ForwardingObserverMethod::new)
        .forEach(abd::addObserverMethod);
  }