Merge "ICS sample producer and consumer"
[nonrtric.git] / sample-services / ics-producer-consumer / producer / src / main / java / com / demo / producer / messages / PropertiesHelper.java
diff --git a/sample-services/ics-producer-consumer/producer/src/main/java/com/demo/producer/messages/PropertiesHelper.java b/sample-services/ics-producer-consumer/producer/src/main/java/com/demo/producer/messages/PropertiesHelper.java
new file mode 100644 (file)
index 0000000..7dc2b1e
--- /dev/null
@@ -0,0 +1,58 @@
+/*-
+ * ========================LICENSE_START=================================
+ * O-RAN-SC
+ *
+ * Copyright (C) 2024: OpenInfra Foundation Europe
+ *
+ * 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.
+ * ========================LICENSE_END===================================
+ */
+
+package com.demo.producer.messages;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import com.demo.producer.producer.SimpleProducer;
+
+@Component
+public class PropertiesHelper {
+    private static final Logger log = LoggerFactory.getLogger(PropertiesHelper.class);
+
+    public static Properties getProperties() throws Exception {
+        Properties props = null;
+        try (InputStream input = SimpleProducer.class.getClassLoader().getResourceAsStream("config.properties")) {
+            props = new Properties();
+            if (input == null) {
+                log.error("Found no configuration file in resources");
+                throw new Exception("Sorry, unable to find config.properties");
+            }
+            props.load(input);
+            String kafkaServers = System.getenv("KAFKA_SERVERS");
+            if (kafkaServers != null) {
+                props.setProperty("bootstrap.servers", kafkaServers);
+                log.info("Env variable KAFKA_SERVERS found, adding: " + kafkaServers);
+            } else {
+                log.info("Env variable KAFKA_SERVERS not found, defaulting to config file");
+            }
+        } catch (IOException e) {
+            log.error("Error reading configuration file: ", e.getMessage());
+        }
+        return props;
+    }
+}