# evidence 249 — messaging-startup-profile-validation
# revision: a24ece9cf797f7ea647e33bf846b115208ed1ba5
# cwd: /shared/codebase/clean-architecture-backend-template/src/messaging
# command: sed -n "1,45p" messaging-spring-boot-starter/src/main/java/dev/caskeleton/messaging/autoconfigure/StartupProfileValidation.java; echo; echo ---StartupProfileValidation bean sites---; grep -rn "StartupProfileValidation" --include=*.java */src/main
# ---- raw output ----
package dev.caskeleton.messaging.autoconfigure;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.springframework.beans.factory.InitializingBean;

/**
 * Applies one profile validator to every profile of its kind at startup.
 *
 * <p>The Kafka, RabbitMQ and security validators were all beans and none of them was injected
 * anywhere: the context published a validator per broker and validated nothing. A profile that
 * promises a guarantee its broker cannot give — an exactly-once claim on a non-transactional
 * producer, a quorum ack on a single replica, a plaintext credential on a production listener —
 * then boots cleanly and fails on the first message that depends on it, which is the wrong place to
 * find out.
 *
 * <p>Validation runs as {@code afterPropertiesSet} rather than on an application event, so the
 * failure happens while the context is still being built and the profile bean that caused it is
 * named in the stack.
 *
 * <p>The profiles arrive through a supplier rather than an {@code ObjectProvider} because there are
 * now two sources of them — beans an application declares and profiles compiled from {@code
 * app.messaging} — and a validator that saw only one of the two would leave the other half of a
 * deployment's configuration unchecked. Which half went unchecked would depend on how the
 * deployment happened to be written, which is the worst possible rule.
 *
 * @param <T> the profile type this instance validates
 */
final class StartupProfileValidation<T> implements InitializingBean {

  private final Supplier<List<T>> profiles;

  private final Consumer<T> validator;

  StartupProfileValidation(Supplier<List<T>> profiles, Consumer<T> validator) {
    this.profiles = Objects.requireNonNull(profiles, "profiles must not be null");
    this.validator = Objects.requireNonNull(validator, "validator must not be null");
  }

  @Override
  public void afterPropertiesSet() {
    profiles.get().forEach(validator);
  }

---StartupProfileValidation bean sites---
messaging-spring-boot-starter/src/main/java/dev/caskeleton/messaging/autoconfigure/KafkaMessagingAutoConfiguration.java:54:  public StartupProfileValidation<dev.caskeleton.messaging.kafka.KafkaBrokerProfile>
messaging-spring-boot-starter/src/main/java/dev/caskeleton/messaging/autoconfigure/KafkaMessagingAutoConfiguration.java:59:    return new StartupProfileValidation<>(
messaging-spring-boot-starter/src/main/java/dev/caskeleton/messaging/autoconfigure/MessagingCoreAutoConfiguration.java:216:  public StartupProfileValidation<dev.caskeleton.messaging.security.BrokerSecurityProfile>
messaging-spring-boot-starter/src/main/java/dev/caskeleton/messaging/autoconfigure/MessagingCoreAutoConfiguration.java:221:    return new StartupProfileValidation<>(
messaging-spring-boot-starter/src/main/java/dev/caskeleton/messaging/autoconfigure/RabbitMessagingAutoConfiguration.java:48:  public StartupProfileValidation<dev.caskeleton.messaging.rabbit.RabbitBrokerProfile>
messaging-spring-boot-starter/src/main/java/dev/caskeleton/messaging/autoconfigure/RabbitMessagingAutoConfiguration.java:55:    return new StartupProfileValidation<>(
messaging-spring-boot-starter/src/main/java/dev/caskeleton/messaging/autoconfigure/StartupProfileValidation.java:31:final class StartupProfileValidation<T> implements InitializingBean {
messaging-spring-boot-starter/src/main/java/dev/caskeleton/messaging/autoconfigure/StartupProfileValidation.java:37:  StartupProfileValidation(Supplier<List<T>> profiles, Consumer<T> validator) {
