ICS sample producer and consumer
[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
37     public static Properties getProperties() throws Exception {
38         Properties props = null;
39         try (InputStream input = SimpleProducer.class.getClassLoader().getResourceAsStream("config.properties")) {
40             props = new Properties();
41             if (input == null) {
42                 log.error("Found no configuration file in resources");
43                 throw new Exception("Sorry, unable to find config.properties");
44             }
45             props.load(input);
46             String kafkaServers = System.getenv("KAFKA_SERVERS");
47             if (kafkaServers != null) {
48                 props.setProperty("bootstrap.servers", kafkaServers);
49                 log.info("Env variable KAFKA_SERVERS found, adding: " + kafkaServers);
50             } else {
51                 log.info("Env variable KAFKA_SERVERS not found, defaulting to config file");
52             }
53         } catch (IOException e) {
54             log.error("Error reading configuration file: ", e.getMessage());
55         }
56         return props;
57     }
58 }