|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 IBM Corporation and others |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * You may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.microshed.testing.jupiter; |
| 20 | + |
| 21 | +import java.lang.reflect.Field; |
| 22 | +import java.lang.reflect.ParameterizedType; |
| 23 | +import java.lang.reflect.Type; |
| 24 | +import java.nio.ByteBuffer; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Properties; |
| 28 | +import java.util.UUID; |
| 29 | + |
| 30 | +import org.junit.jupiter.api.extension.ExtensionConfigurationException; |
| 31 | +import org.microshed.testing.kafka.KafkaConsumerConfig; |
| 32 | +import org.microshed.testing.kafka.KafkaProducerConfig; |
| 33 | + |
| 34 | +class KafkaConfigAnnotationProcessor { |
| 35 | + |
| 36 | + private final Map<Class<?>, String> defaultSerailizers = new HashMap<>(); |
| 37 | + private final Map<Class<?>, String> defaultDeserailizers = new HashMap<>(); |
| 38 | + private final String globalBootstrapServers = System.getProperty("org.microshed.kafka.bootstrap.servers"); |
| 39 | + |
| 40 | + KafkaConfigAnnotationProcessor() { |
| 41 | + defaultSerailizers.put(byte[].class, "org.apache.kafka.common.serialization.ByteArraySerializer"); |
| 42 | + defaultSerailizers.put(ByteBuffer.class, "org.apache.kafka.common.serialization.ByteBufferSerializer"); |
| 43 | + defaultSerailizers.put(Double.class, "org.apache.kafka.common.serialization.DoubleSerializer"); |
| 44 | + defaultSerailizers.put(Float.class, "org.apache.kafka.common.serialization.FloatSerializer"); |
| 45 | + defaultSerailizers.put(Integer.class, "org.apache.kafka.common.serialization.IntegerSerializer"); |
| 46 | + defaultSerailizers.put(Long.class, "org.apache.kafka.common.serialization.LongSerializer"); |
| 47 | + defaultSerailizers.put(Short.class, "org.apache.kafka.common.serialization.ShortSerializer"); |
| 48 | + defaultSerailizers.put(String.class, "org.apache.kafka.common.serialization.StringSerializer"); |
| 49 | + defaultSerailizers.put(UUID.class, "org.apache.kafka.common.serialization.UUIDSerializer"); |
| 50 | + |
| 51 | + defaultDeserailizers.put(byte[].class, "org.apache.kafka.common.serialization.ByteArrayDeserializer"); |
| 52 | + defaultDeserailizers.put(ByteBuffer.class, "org.apache.kafka.common.serialization.ByteBufferDeserializer"); |
| 53 | + defaultDeserailizers.put(Double.class, "org.apache.kafka.common.serialization.DoubleDeserializer"); |
| 54 | + defaultDeserailizers.put(Float.class, "org.apache.kafka.common.serialization.FloatDeserializer"); |
| 55 | + defaultDeserailizers.put(Integer.class, "org.apache.kafka.common.serialization.IntegerDeserializer"); |
| 56 | + defaultDeserailizers.put(Long.class, "org.apache.kafka.common.serialization.LongDeserializer"); |
| 57 | + defaultDeserailizers.put(Short.class, "org.apache.kafka.common.serialization.ShortDeserializer"); |
| 58 | + defaultDeserailizers.put(String.class, "org.apache.kafka.common.serialization.StringDeserializer"); |
| 59 | + defaultDeserailizers.put(UUID.class, "org.apache.kafka.common.serialization.UUIDDeserializer"); |
| 60 | + } |
| 61 | + |
| 62 | + Properties getProducerProperties(Field producerField) { |
| 63 | + KafkaProducerConfig producerConfig = producerField.getAnnotation(KafkaProducerConfig.class); |
| 64 | + Properties properties = new Properties(); |
| 65 | + String bootstrapServers = producerConfig.bootstrapServers().isEmpty() ? globalBootstrapServers : producerConfig.bootstrapServers(); |
| 66 | + if (bootstrapServers.isEmpty()) |
| 67 | + throw new ExtensionConfigurationException("To use @KafkaProducerConfig on a KafkaProducer a bootstrap server must be " + |
| 68 | + "defined in the @KafkaProducerConfig annotation or using the " + |
| 69 | + "'org.microshed.kafka.bootstrap.servers' system property"); |
| 70 | + properties.put("bootstrap.servers", bootstrapServers); |
| 71 | + if (isClassPropertySet(producerConfig.keySerializer().getName())) |
| 72 | + properties.put("key.serializer", producerConfig.keySerializer().getName()); |
| 73 | + if (isClassPropertySet(producerConfig.valueSerializer().getName())) |
| 74 | + properties.put("value.serializer", producerConfig.valueSerializer().getName()); |
| 75 | + for (String prop : producerConfig.properties()) { |
| 76 | + int split = prop.indexOf("="); |
| 77 | + if (split < 2) |
| 78 | + throw new ExtensionConfigurationException("The property '" + prop + "' for field " + producerField + " must be in the format 'key=value'"); |
| 79 | + properties.put(prop.substring(0, split), prop.substring(split + 1)); |
| 80 | + } |
| 81 | + |
| 82 | + // Auto-detect key/value serializers if needed |
| 83 | + if (producerField.getGenericType() instanceof ParameterizedType) { |
| 84 | + if (!properties.containsKey("key.serializer")) { |
| 85 | + Type keyType = ((ParameterizedType) producerField.getGenericType()).getActualTypeArguments()[0]; |
| 86 | + if (defaultSerailizers.containsKey(keyType)) |
| 87 | + properties.put("key.serializer", defaultSerailizers.get(keyType)); |
| 88 | + } |
| 89 | + if (!properties.containsKey("value.serializer")) { |
| 90 | + Type valueType = ((ParameterizedType) producerField.getGenericType()).getActualTypeArguments()[1]; |
| 91 | + if (defaultSerailizers.containsKey(valueType)) |
| 92 | + properties.put("value.serializer", defaultSerailizers.get(valueType)); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return properties; |
| 97 | + } |
| 98 | + |
| 99 | + Properties getConsumerProperties(Field consumerField) { |
| 100 | + KafkaConsumerConfig consumerConfig = consumerField.getAnnotation(KafkaConsumerConfig.class); |
| 101 | + Properties properties = new Properties(); |
| 102 | + String bootstrapServers = consumerConfig.bootstrapServers().isEmpty() ? globalBootstrapServers : consumerConfig.bootstrapServers(); |
| 103 | + if (bootstrapServers.isEmpty()) |
| 104 | + throw new ExtensionConfigurationException("To use @KafkaConsumerConfig on a KafkaConsumer a bootstrap server must be " + |
| 105 | + "defined in the @KafkaConsumerConfig annotation or using the " + |
| 106 | + "'org.microshed.kafka.bootstrap.servers' system property"); |
| 107 | + properties.put("bootstrap.servers", bootstrapServers); |
| 108 | + properties.put("group.id", consumerConfig.groupId()); |
| 109 | + if (isClassPropertySet(consumerConfig.keyDeserializer().getName())) |
| 110 | + properties.put("key.deserializer", consumerConfig.keyDeserializer().getName()); |
| 111 | + if (isClassPropertySet(consumerConfig.valueDeserializer().getName())) |
| 112 | + properties.put("value.deserializer", consumerConfig.valueDeserializer().getName()); |
| 113 | + for (String prop : consumerConfig.properties()) { |
| 114 | + int split = prop.indexOf("="); |
| 115 | + if (split < 2) |
| 116 | + throw new ExtensionConfigurationException("The property '" + prop + "' for field " + consumerField + " must be in the format 'key=value'"); |
| 117 | + properties.put(prop.substring(0, split), prop.substring(split + 1)); |
| 118 | + } |
| 119 | + |
| 120 | + // Auto-detect key/value deserializer if needed |
| 121 | + if (consumerField.getGenericType() instanceof ParameterizedType) { |
| 122 | + if (!properties.containsKey("key.deserializer")) { |
| 123 | + Type keyType = ((ParameterizedType) consumerField.getGenericType()).getActualTypeArguments()[0]; |
| 124 | + if (defaultDeserailizers.containsKey(keyType)) |
| 125 | + properties.put("key.deserializer", defaultDeserailizers.get(keyType)); |
| 126 | + } |
| 127 | + if (!properties.containsKey("value.deserializer")) { |
| 128 | + Type valueType = ((ParameterizedType) consumerField.getGenericType()).getActualTypeArguments()[1]; |
| 129 | + if (defaultDeserailizers.containsKey(valueType)) |
| 130 | + properties.put("value.deserializer", defaultDeserailizers.get(valueType)); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return properties; |
| 135 | + } |
| 136 | + |
| 137 | + private static boolean isClassPropertySet(String prop) { |
| 138 | + return !"java.lang.Object".equals(prop); |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments