2 * ========================LICENSE_START=================================
5 * Copyright (C) 2024: OpenInfra Foundation Europe
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ========================LICENSE_END===================================
21 package com.demo.consumer.messages;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Properties;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.stereotype.Component;
31 import com.demo.consumer.consumer.SimpleConsumer;
34 public class PropertiesHelper {
35 private static final Logger log = LoggerFactory.getLogger(PropertiesHelper.class);
36 private static String kafkaServers = null;
38 public static Properties getProperties() throws Exception {
39 Properties props = new Properties();
40 try (InputStream input = SimpleConsumer.class.getClassLoader().getResourceAsStream("config.properties")) {
42 log.error("Failed to load configuration file 'config.properties'");
43 throw new IOException("Configuration file 'config.properties' not found");
46 setBootstrapServers(props);
47 } catch (IOException e) {
48 log.error("Error reading configuration file: ", e);
54 private static void setBootstrapServers(Properties props) {
55 if (kafkaServers != null && !kafkaServers.isEmpty()) {
56 props.setProperty("bootstrap.servers", kafkaServers);
57 log.info("Using actively bootstrap servers: {}", kafkaServers);
59 String kafkaServersEnv = System.getenv("KAFKA_SERVERS");
60 if (kafkaServersEnv != null && !kafkaServersEnv.isEmpty()) {
61 kafkaServers = kafkaServersEnv;
62 props.setProperty("bootstrap.servers", kafkaServers);
63 log.info("Using environment variable KAFKA_SERVERS: {}", kafkaServers);
65 log.info("Environment variable KAFKA_SERVERS not found, defaulting to config file");
70 public static void setKafkaServers(String servers) {
71 kafkaServers = servers;