|
| 1 | +/* |
| 2 | + * Copyright 2021 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.support.serializer; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +import org.apache.kafka.common.errors.SerializationException; |
| 24 | +import org.apache.kafka.common.header.Headers; |
| 25 | +import org.apache.kafka.common.serialization.Serializer; |
| 26 | + |
| 27 | +import org.springframework.util.Assert; |
| 28 | + |
| 29 | +/** |
| 30 | + * Delegates to a serializer based on type. |
| 31 | + * |
| 32 | + * @author Gary Russell |
| 33 | + * @since 2.7.9 |
| 34 | + * |
| 35 | + */ |
| 36 | +public class DelegatingByTypeSerializer implements Serializer<Object> { |
| 37 | + |
| 38 | + private static final String RAWTYPES = "rawtypes"; |
| 39 | + |
| 40 | + @SuppressWarnings(RAWTYPES) |
| 41 | + private final Map<Class<?>, Serializer> delegates = new HashMap<>(); |
| 42 | + |
| 43 | + /** |
| 44 | + * Construct an instance with the map of delegates. |
| 45 | + * @param delegates the delegates. |
| 46 | + */ |
| 47 | + @SuppressWarnings(RAWTYPES) |
| 48 | + public DelegatingByTypeSerializer(Map<Class<?>, Serializer> delegates) { |
| 49 | + Assert.notNull(delegates, "'delegates' cannot be null"); |
| 50 | + Assert.noNullElements(delegates.values(), "Serializers in delegates map cannot be null"); |
| 51 | + this.delegates.putAll(delegates); |
| 52 | + } |
| 53 | + |
| 54 | + @SuppressWarnings("unchecked") |
| 55 | + @Override |
| 56 | + public void configure(Map<String, ?> configs, boolean isKey) { |
| 57 | + this.delegates.values().forEach(del -> del.configure(configs, isKey)); |
| 58 | + } |
| 59 | + |
| 60 | + @SuppressWarnings({ RAWTYPES, "unchecked" }) |
| 61 | + @Override |
| 62 | + public byte[] serialize(String topic, Object data) { |
| 63 | + Serializer delegate = findDelegate(data); |
| 64 | + return delegate.serialize(topic, data); |
| 65 | + } |
| 66 | + |
| 67 | + @SuppressWarnings({ "unchecked", RAWTYPES }) |
| 68 | + @Override |
| 69 | + public byte[] serialize(String topic, Headers headers, Object data) { |
| 70 | + Serializer delegate = findDelegate(data); |
| 71 | + return delegate.serialize(topic, headers, data); |
| 72 | + } |
| 73 | + |
| 74 | + @SuppressWarnings(RAWTYPES) |
| 75 | + private Serializer findDelegate(Object data) { |
| 76 | + Serializer delegate = this.delegates.get(data.getClass()); |
| 77 | + if (delegate == null) { |
| 78 | + throw new SerializationException("No matching delegate for type: " + data.getClass().getName() |
| 79 | + + "; supported types: " + this.delegates.keySet().stream() |
| 80 | + .map(clazz -> clazz.getName()) |
| 81 | + .collect(Collectors.toList())); |
| 82 | + } |
| 83 | + return delegate; |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | +} |
0 commit comments