|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 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 | + * https://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.springframework.kafka.test; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.StringReader; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Properties; |
| 25 | +import java.util.function.Function; |
| 26 | + |
| 27 | +import org.springframework.core.io.Resource; |
| 28 | +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
| 29 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 30 | +import org.springframework.util.StringUtils; |
| 31 | + |
| 32 | +/** |
| 33 | + * The factory to encapsulate an {@link EmbeddedKafkaBroker} creation logic. |
| 34 | + * |
| 35 | + * @author Artem Bilan |
| 36 | + * |
| 37 | + * @since 3.2.6 |
| 38 | + */ |
| 39 | +public final class EmbeddedKafkaBrokerFactory { |
| 40 | + |
| 41 | + private static final String TRANSACTION_STATE_LOG_REPLICATION_FACTOR = "transaction.state.log.replication.factor"; |
| 42 | + |
| 43 | + /** |
| 44 | + * Create an {@link EmbeddedKafkaBroker} based on the {@code EmbeddedKafka} annotation. |
| 45 | + * @param embeddedKafka the {@code EmbeddedKafka} annotation. |
| 46 | + * @return a new {@link EmbeddedKafkaBroker} instance. |
| 47 | + */ |
| 48 | + public static EmbeddedKafkaBroker create(EmbeddedKafka embeddedKafka) { |
| 49 | + return create(embeddedKafka, Function.identity()); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Create an {@link EmbeddedKafkaBroker} based on the {@code EmbeddedKafka} annotation. |
| 54 | + * @param embeddedKafka the {@code EmbeddedKafka} annotation. |
| 55 | + * @param propertyResolver the {@link Function} for placeholders in the annotation attributes. |
| 56 | + * @return a new {@link EmbeddedKafkaBroker} instance. |
| 57 | + */ |
| 58 | + @SuppressWarnings("unchecked") |
| 59 | + public static EmbeddedKafkaBroker create(EmbeddedKafka embeddedKafka, Function<String, String> propertyResolver) { |
| 60 | + String[] topics = |
| 61 | + Arrays.stream(embeddedKafka.topics()) |
| 62 | + .map(propertyResolver) |
| 63 | + .toArray(String[]::new); |
| 64 | + |
| 65 | + EmbeddedKafkaBroker embeddedKafkaBroker; |
| 66 | + if (embeddedKafka.kraft()) { |
| 67 | + embeddedKafkaBroker = kraftBroker(embeddedKafka, topics); |
| 68 | + } |
| 69 | + else { |
| 70 | + embeddedKafkaBroker = zkBroker(embeddedKafka, topics); |
| 71 | + } |
| 72 | + int[] ports = setupPorts(embeddedKafka); |
| 73 | + |
| 74 | + embeddedKafkaBroker.kafkaPorts(ports) |
| 75 | + .adminTimeout(embeddedKafka.adminTimeout()); |
| 76 | + |
| 77 | + Properties properties = new Properties(); |
| 78 | + |
| 79 | + for (String pair : embeddedKafka.brokerProperties()) { |
| 80 | + if (!StringUtils.hasText(pair)) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + try { |
| 84 | + properties.load(new StringReader(propertyResolver.apply(pair))); |
| 85 | + } |
| 86 | + catch (Exception ex) { |
| 87 | + throw new IllegalStateException("Failed to load broker property from [" + pair + "]", ex); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + String brokerPropertiesLocation = embeddedKafka.brokerPropertiesLocation(); |
| 92 | + if (StringUtils.hasText(brokerPropertiesLocation)) { |
| 93 | + String propertiesLocation = propertyResolver.apply(brokerPropertiesLocation); |
| 94 | + Resource propertiesResource = new PathMatchingResourcePatternResolver().getResource(propertiesLocation); |
| 95 | + if (!propertiesResource.exists()) { |
| 96 | + throw new IllegalStateException( |
| 97 | + "Failed to load broker properties from [" + propertiesResource + "]: resource does not exist."); |
| 98 | + } |
| 99 | + try (InputStream in = propertiesResource.getInputStream()) { |
| 100 | + Properties p = new Properties(); |
| 101 | + p.load(in); |
| 102 | + p.forEach((key, value) -> properties.putIfAbsent(key, propertyResolver.apply((String) value))); |
| 103 | + } |
| 104 | + catch (IOException ex) { |
| 105 | + throw new IllegalStateException("Failed to load broker properties from [" + propertiesResource + "]", ex); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + properties.putIfAbsent(TRANSACTION_STATE_LOG_REPLICATION_FACTOR, |
| 110 | + String.valueOf(Math.min(3, embeddedKafka.count()))); |
| 111 | + |
| 112 | + embeddedKafkaBroker.brokerProperties((Map<String, String>) (Map<?, ?>) properties); |
| 113 | + String bootstrapServersProperty = embeddedKafka.bootstrapServersProperty(); |
| 114 | + if (StringUtils.hasText(bootstrapServersProperty)) { |
| 115 | + embeddedKafkaBroker.brokerListProperty(bootstrapServersProperty); |
| 116 | + } |
| 117 | + |
| 118 | + // Safe to start an embedded broker eagerly before context refresh |
| 119 | + embeddedKafkaBroker.afterPropertiesSet(); |
| 120 | + |
| 121 | + return embeddedKafkaBroker; |
| 122 | + } |
| 123 | + |
| 124 | + private static int[] setupPorts(EmbeddedKafka embedded) { |
| 125 | + int[] ports = embedded.ports(); |
| 126 | + if (embedded.count() > 1 && ports.length == 1 && ports[0] == 0) { |
| 127 | + ports = new int[embedded.count()]; |
| 128 | + } |
| 129 | + return ports; |
| 130 | + } |
| 131 | + |
| 132 | + private static EmbeddedKafkaBroker kraftBroker(EmbeddedKafka embedded, String[] topics) { |
| 133 | + return new EmbeddedKafkaKraftBroker(embedded.count(), embedded.partitions(), topics); |
| 134 | + } |
| 135 | + |
| 136 | + private static EmbeddedKafkaBroker zkBroker(EmbeddedKafka embedded, String[] topics) { |
| 137 | + return new EmbeddedKafkaZKBroker(embedded.count(), embedded.controlledShutdown(), embedded.partitions(), topics) |
| 138 | + .zkPort(embedded.zookeeperPort()) |
| 139 | + .zkConnectionTimeout(embedded.zkConnectionTimeout()) |
| 140 | + .zkSessionTimeout(embedded.zkSessionTimeout()); |
| 141 | + } |
| 142 | + |
| 143 | + private EmbeddedKafkaBrokerFactory() { |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments