Test FTC100 fails since A1-SIM update
[nonrtric.git] / sample-services / ics-producer-consumer / producer / src / main / java / com / demo / producer / messages / PropertiesHelper.java
index 7dc2b1e..d7d9f98 100644 (file)
@@ -33,26 +33,41 @@ import com.demo.producer.producer.SimpleProducer;
 @Component
 public class PropertiesHelper {
     private static final Logger log = LoggerFactory.getLogger(PropertiesHelper.class);
+    private static String kafkaServers = null;
 
     public static Properties getProperties() throws Exception {
-        Properties props = null;
+        Properties props = new Properties();
         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");
+                log.error("Failed to load configuration file 'config.properties'");
+                throw new IOException("Configuration file 'config.properties' not found");
             }
             props.load(input);
-            String kafkaServers = System.getenv("KAFKA_SERVERS");
-            if (kafkaServers != null) {
+            setBootstrapServers(props);
+        } catch (IOException e) {
+            log.error("Error reading configuration file: ", e);
+            throw e;
+        }
+        return props;
+    }
+
+    private static void setBootstrapServers(Properties props) {
+        if (kafkaServers != null && !kafkaServers.isEmpty()) {
+            props.setProperty("bootstrap.servers", kafkaServers);
+            log.info("Using actively bootstrap servers: {}", kafkaServers);
+        } else {
+            String kafkaServersEnv = System.getenv("KAFKA_SERVERS");
+            if (kafkaServersEnv != null && !kafkaServersEnv.isEmpty()) {
+                kafkaServers = kafkaServersEnv;
                 props.setProperty("bootstrap.servers", kafkaServers);
-                log.info("Env variable KAFKA_SERVERS found, adding: " + kafkaServers);
+                log.info("Using environment variable KAFKA_SERVERS: {}", kafkaServers);
             } else {
-                log.info("Env variable KAFKA_SERVERS not found, defaulting to config file");
+                log.info("Environment variable KAFKA_SERVERS not found, defaulting to config file");
             }
-        } catch (IOException e) {
-            log.error("Error reading configuration file: ", e.getMessage());
         }
-        return props;
+    }
+
+    public static void setKafkaServers(String servers) {
+        kafkaServers = servers;
     }
 }