Test FTC100 fails since A1-SIM update
[nonrtric.git] / sample-services / ics-producer-consumer / producer / src / main / java / com / demo / producer / messages / PropertiesHelper.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  *
5  * Copyright (C) 2024: OpenInfra Foundation Europe
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  * ========================LICENSE_END===================================
19  */
20
21 package com.demo.producer.messages;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Properties;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.stereotype.Component;
30
31 import com.demo.producer.producer.SimpleProducer;
32
33 @Component
34 public class PropertiesHelper {
35     private static final Logger log = LoggerFactory.getLogger(PropertiesHelper.class);
36     private static String kafkaServers = null;
37
38     public static Properties getProperties() throws Exception {
39         Properties props = new Properties();
40         try (InputStream input = SimpleProducer.class.getClassLoader().getResourceAsStream("config.properties")) {
41             if (input == null) {
42                 log.error("Failed to load configuration file 'config.properties'");
43                 throw new IOException("Configuration file 'config.properties' not found");
44             }
45             props.load(input);
46             setBootstrapServers(props);
47         } catch (IOException e) {
48             log.error("Error reading configuration file: ", e);
49             throw e;
50         }
51         return props;
52     }
53
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);
58         } else {
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);
64             } else {
65                 log.info("Environment variable KAFKA_SERVERS not found, defaulting to config file");
66             }
67         }
68     }
69
70     public static void setKafkaServers(String servers) {
71         kafkaServers = servers;
72     }
73 }