All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkiverse.kafkastreamsprocessor.impl.DefaultSourceToTopicsMappingBuilder Maven / Gradle / Ivy

/*-
 * #%L
 * Quarkus Kafka Streams Processor
 * %%
 * Copyright (C) 2024 Amadeus s.a.s.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package io.quarkiverse.kafkastreamsprocessor.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import io.quarkiverse.kafkastreamsprocessor.api.SourceToTopicsMappingBuilder;
import io.quarkiverse.kafkastreamsprocessor.api.properties.KStreamsProcessorConfig;
import io.quarkiverse.kafkastreamsprocessor.api.properties.SourceConfig;
import lombok.extern.slf4j.Slf4j;

/**
 * Multi-input topic configuration Inspired by smallrye-reactive-messaging
 * ConfiguredChannelFactory
 */
@ApplicationScoped
@Slf4j
public class DefaultSourceToTopicsMappingBuilder implements SourceToTopicsMappingBuilder {
    /**
     * Configuration of the extension
     */
    private final KStreamsProcessorConfig extensionConfiguration;

    /**
     * Injection constructor
     *
     * @param extensionConfiguration
     *        Configuration of the extension
     */
    @Inject
    public DefaultSourceToTopicsMappingBuilder(KStreamsProcessorConfig extensionConfiguration) {
        this.extensionConfiguration = extensionConfiguration;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Map sourceToTopicsMapping() {
        // Extract topic name for each channel
        Map sourceToTopicMapping = buildMapping();

        // Backward compatibility
        if (sourceToTopicMapping.isEmpty()) {
            Optional> inputTopicList = extensionConfiguration.input().topics();
            if (inputTopicList.isPresent()) {
                return Map.of(DEFAULT_SOURCE_NAME, inputTopicList.get().toArray(new String[0]));
            }
            Optional singleInputTopic = extensionConfiguration.input().topic();
            if (singleInputTopic.isPresent()) {
                return Map.of(DEFAULT_SOURCE_NAME, new String[] { singleInputTopic.get() });
            }
        }

        return sourceToTopicMapping;
    }

    private Map buildMapping() {
        Map sourceToTopicMapping = new HashMap<>();
        for (Map.Entry sourceEntry : extensionConfiguration.input().sources().entrySet()) {
            List topicNames = sourceEntry.getValue().topics();
            if (topicNames != null && !topicNames.isEmpty()) {
                sourceToTopicMapping.put(sourceEntry.getKey(), topicNames.toArray(new String[0]));
            }
        }
        return sourceToTopicMapping;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy