io.streamnative.pulsar.handlers.kop.proxy.KafkaProxyConfiguration Maven / Gradle / Ivy
/**
* Copyright (c) 2019 - 2024 StreamNative, Inc.. All Rights Reserved.
*/
/**
* 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.
*/
package io.streamnative.pulsar.handlers.kop.proxy;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import lombok.Getter;
import lombok.Setter;
@Getter
public class KafkaProxyConfiguration {
public static final String KAFKA_LISTENERS_CONFIG = "kafkaListeners";
public static final String KAFKA_ADVERTISED_LISTENERS_CONFIG = "kafkaAdvertisedListeners";
public static final String KAFKA_BOOTSTRAP_SERVERS_CONFIG = "kafkaBootstrapServers";
public static final String KAFKA_TOKEN_SECRET_KEY = "kafkaTokenSecretKey";
private final String kafkaListeners;
private final String kafkaAdvertisedListeners;
private final List kafkaBootstrapServers;
// Configs that reuses the same fields from ProxyConfiguration
@Setter
private int brokerProxyConnectTimeoutMs = 10000;
public KafkaProxyConfiguration(final Properties properties) {
kafkaListeners = properties.getProperty(KAFKA_LISTENERS_CONFIG);
if (kafkaListeners == null || kafkaListeners.isEmpty()) {
throw new IllegalArgumentException("Empty " + KAFKA_LISTENERS_CONFIG);
}
kafkaAdvertisedListeners = Optional.ofNullable(properties.getProperty(KAFKA_ADVERTISED_LISTENERS_CONFIG))
.orElse(kafkaListeners);
String value = properties.getProperty(KAFKA_BOOTSTRAP_SERVERS_CONFIG);
if (value == null) {
throw new IllegalArgumentException("Empty " + KAFKA_BOOTSTRAP_SERVERS_CONFIG);
}
kafkaBootstrapServers = new ArrayList<>();
for (String address : value.split(",")) {
int pos = address.indexOf(':');
if (pos < 0) {
throw new IllegalArgumentException("Illegal address " + address);
}
kafkaBootstrapServers.add(InetSocketAddress.createUnresolved(address.substring(0, pos),
Integer.parseInt(address.substring(pos + 1))));
}
if (kafkaBootstrapServers.isEmpty()) {
throw new IllegalArgumentException("Empty " + KAFKA_LISTENERS_CONFIG);
}
}
@Override
public String toString() {
return String.join("\n", KAFKA_LISTENERS_CONFIG + ": \"" + kafkaListeners + "\"",
KAFKA_ADVERTISED_LISTENERS_CONFIG + ": \"" + kafkaAdvertisedListeners + "\"",
KAFKA_BOOTSTRAP_SERVERS_CONFIG + ": \"" + kafkaBootstrapServers.stream()
.map(InetSocketAddress::toString).reduce("", (x, y) -> x + "," + y));
}
}