|
| 1 | +/* |
| 2 | + * Copyright 2024-2026 Firefly Software Solutions Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.fireflyframework.eda.config; |
| 18 | + |
| 19 | +import org.springframework.boot.SpringApplication; |
| 20 | +import org.springframework.boot.env.EnvironmentPostProcessor; |
| 21 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 22 | +import org.springframework.core.env.MapPropertySource; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.stream.Collectors; |
| 30 | + |
| 31 | +/** |
| 32 | + * Environment post-processor that conditionally excludes Spring Boot's |
| 33 | + * RabbitMQ and Kafka auto-configurations when the corresponding Firefly EDA |
| 34 | + * transports are not enabled. |
| 35 | + * |
| 36 | + * <p>This prevents Spring Boot from auto-creating connection factories and |
| 37 | + * health indicators for messaging systems that the application does not use, |
| 38 | + * avoiding startup errors like "Rabbit health check failed - Connection refused". |
| 39 | + * |
| 40 | + * <p><strong>Behavior:</strong> |
| 41 | + * <ul> |
| 42 | + * <li>If neither {@code firefly.eda.publishers.rabbitmq.default.enabled} nor |
| 43 | + * {@code firefly.eda.consumer.rabbitmq.default.enabled} is {@code true}, |
| 44 | + * and {@code spring.rabbitmq.host} is not explicitly set, |
| 45 | + * then {@code RabbitAutoConfiguration} is excluded.</li> |
| 46 | + * <li>If neither {@code firefly.eda.publishers.kafka.default.enabled} nor |
| 47 | + * {@code firefly.eda.consumer.kafka.default.enabled} is {@code true}, |
| 48 | + * and {@code spring.kafka.bootstrap-servers} is not explicitly set, |
| 49 | + * then {@code KafkaAutoConfiguration} is excluded.</li> |
| 50 | + * </ul> |
| 51 | + * |
| 52 | + * <p>Any existing {@code spring.autoconfigure.exclude} entries are preserved. |
| 53 | + */ |
| 54 | +public class FireflyEdaEnvironmentPostProcessor implements EnvironmentPostProcessor { |
| 55 | + |
| 56 | + private static final String EXCLUDE_PROPERTY = "spring.autoconfigure.exclude"; |
| 57 | + private static final String RABBIT_AUTO_CONFIG = "org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration"; |
| 58 | + private static final String KAFKA_AUTO_CONFIG = "org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration"; |
| 59 | + |
| 60 | + @Override |
| 61 | + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { |
| 62 | + List<String> excludes = new ArrayList<>(); |
| 63 | + |
| 64 | + if (shouldExcludeRabbit(environment)) { |
| 65 | + excludes.add(RABBIT_AUTO_CONFIG); |
| 66 | + } |
| 67 | + |
| 68 | + if (shouldExcludeKafka(environment)) { |
| 69 | + excludes.add(KAFKA_AUTO_CONFIG); |
| 70 | + } |
| 71 | + |
| 72 | + if (!excludes.isEmpty()) { |
| 73 | + applyExcludes(environment, excludes); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private boolean shouldExcludeRabbit(ConfigurableEnvironment env) { |
| 78 | + // Respect explicit Spring Boot native configuration |
| 79 | + if (env.containsProperty("spring.rabbitmq.host")) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + boolean publisherEnabled = env.getProperty( |
| 84 | + "firefly.eda.publishers.rabbitmq.default.enabled", Boolean.class, false); |
| 85 | + boolean consumerEnabled = env.getProperty( |
| 86 | + "firefly.eda.consumer.rabbitmq.default.enabled", Boolean.class, false); |
| 87 | + |
| 88 | + return !publisherEnabled && !consumerEnabled; |
| 89 | + } |
| 90 | + |
| 91 | + private boolean shouldExcludeKafka(ConfigurableEnvironment env) { |
| 92 | + // Respect explicit Spring Boot native configuration |
| 93 | + if (env.containsProperty("spring.kafka.bootstrap-servers")) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + boolean publisherEnabled = env.getProperty( |
| 98 | + "firefly.eda.publishers.kafka.default.enabled", Boolean.class, false); |
| 99 | + boolean consumerEnabled = env.getProperty( |
| 100 | + "firefly.eda.consumer.kafka.default.enabled", Boolean.class, false); |
| 101 | + |
| 102 | + return !publisherEnabled && !consumerEnabled; |
| 103 | + } |
| 104 | + |
| 105 | + private void applyExcludes(ConfigurableEnvironment environment, List<String> newExcludes) { |
| 106 | + // Read existing excludes to preserve them |
| 107 | + String existing = environment.getProperty(EXCLUDE_PROPERTY, ""); |
| 108 | + List<String> allExcludes = new ArrayList<>(); |
| 109 | + |
| 110 | + if (!existing.isEmpty()) { |
| 111 | + allExcludes.addAll( |
| 112 | + Arrays.stream(existing.split(",")) |
| 113 | + .map(String::trim) |
| 114 | + .filter(s -> !s.isEmpty()) |
| 115 | + .collect(Collectors.toList())); |
| 116 | + } |
| 117 | + |
| 118 | + // Add new excludes (avoiding duplicates) |
| 119 | + for (String exclude : newExcludes) { |
| 120 | + if (!allExcludes.contains(exclude)) { |
| 121 | + allExcludes.add(exclude); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + Map<String, Object> props = new HashMap<>(); |
| 126 | + props.put(EXCLUDE_PROPERTY, String.join(",", allExcludes)); |
| 127 | + |
| 128 | + environment.getPropertySources().addFirst( |
| 129 | + new MapPropertySource("fireflyEdaAutoConfigExcludes", props)); |
| 130 | + } |
| 131 | +} |
0 commit comments