X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=sample-services%2Fics-producer-consumer%2Fconsumer%2Fsrc%2Fmain%2Fjava%2Fcom%2Fdemo%2Fconsumer%2Fmessages%2FPropertiesHelper.java;fp=sample-services%2Fics-producer-consumer%2Fconsumer%2Fsrc%2Fmain%2Fjava%2Fcom%2Fdemo%2Fconsumer%2Fmessages%2FPropertiesHelper.java;h=18be2f8e14f19ad470abf4de8b0872d1c823f3a3;hb=6360bbb90944220eef2f0b8f03623ae40c9646cd;hp=0000000000000000000000000000000000000000;hpb=0f1c16fd071c70215eed25fa45ecce4803c83d72;p=nonrtric.git diff --git a/sample-services/ics-producer-consumer/consumer/src/main/java/com/demo/consumer/messages/PropertiesHelper.java b/sample-services/ics-producer-consumer/consumer/src/main/java/com/demo/consumer/messages/PropertiesHelper.java new file mode 100644 index 00000000..18be2f8e --- /dev/null +++ b/sample-services/ics-producer-consumer/consumer/src/main/java/com/demo/consumer/messages/PropertiesHelper.java @@ -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.consumer.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.consumer.consumer.SimpleConsumer; + +@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 = SimpleConsumer.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; + } +}