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.producer.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.producer.producer.SimpleProducer;
34 public class PropertiesHelper {
35 private static final Logger log = LoggerFactory.getLogger(PropertiesHelper.class);
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();
42 log.error("Found no configuration file in resources");
43 throw new Exception("Sorry, unable to find config.properties");
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);
51 log.info("Env variable KAFKA_SERVERS not found, defaulting to config file");
53 } catch (IOException e) {
54 log.error("Error reading configuration file: ", e.getMessage());