From 33f539c97f75232926009312dee888fc09a45bd0 Mon Sep 17 00:00:00 2001 From: "Tschaen, Brendan" Date: Thu, 1 Nov 2018 15:28:02 -0400 Subject: Fix property loading in jar Change-Id: Idbadcf1d1ae6203b97f50b6659a929595cb53b20 Issue-ID: MUSIC-148 Signed-off-by: Tschaen, Brendan --- src/main/java/org/onap/music/main/MusicCore.java | 10 ++++++++ src/main/java/org/onap/music/main/MusicUtil.java | 29 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/main/java/org/onap/music/main/MusicCore.java b/src/main/java/org/onap/music/main/MusicCore.java index 6b32fc53..d7c5bcec 100644 --- a/src/main/java/org/onap/music/main/MusicCore.java +++ b/src/main/java/org/onap/music/main/MusicCore.java @@ -105,6 +105,11 @@ public class MusicCore { logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring data store handle"); long start = System.currentTimeMillis(); if (mDstoreHandle == null) { + try { + MusicUtil.loadProperties(); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "No properties file defined. Falling back to default."); + } mDstoreHandle = new CassaDataStore(remoteIp); } long end = System.currentTimeMillis(); @@ -122,6 +127,11 @@ public class MusicCore { logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring data store handle"); long start = System.currentTimeMillis(); if (mDstoreHandle == null) { + try { + MusicUtil.loadProperties(); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "No properties file defined. Falling back to default."); + } // Quick Fix - Best to put this into every call to getDSHandle? if (! MusicUtil.getMyCassaHost().equals("localhost") ) { mDstoreHandle = new CassaDataStore(MusicUtil.getMyCassaHost()); diff --git a/src/main/java/org/onap/music/main/MusicUtil.java b/src/main/java/org/onap/music/main/MusicUtil.java index c11b4c7e..1cfd5fbf 100755 --- a/src/main/java/org/onap/music/main/MusicUtil.java +++ b/src/main/java/org/onap/music/main/MusicUtil.java @@ -23,12 +23,15 @@ package org.onap.music.main; import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import java.math.BigInteger; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.Scanner; import java.util.StringTokenizer; import java.util.UUID; @@ -334,6 +337,7 @@ public class MusicUtil { * @param myCassaHost */ public static void setMyCassaHost(String myCassaHost) { + System.out.println("Setting my cassa host: " + myCassaHost); MusicUtil.myCassaHost = myCassaHost; } @@ -567,4 +571,29 @@ public class MusicUtil { } + + public static void loadProperties() throws Exception { + Properties prop = new Properties(); + InputStream input = null; + try { + // load the properties file + input = MusicUtil.class.getClassLoader().getResourceAsStream("music.properties"); + prop.load(input); + } catch (Exception ex) { + logger.error(EELFLoggerDelegate.errorLogger, "Unable to find properties file."); + throw new Exception(); + } finally { + if (input != null) { + try { + input.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + MusicUtil.setMyCassaHost(prop.getProperty("cassandra.host")); + MusicUtil.setCassName(prop.getProperty("cassandra.user")); + MusicUtil.setCassPwd(prop.getProperty("cassandra.password")); + } + } -- cgit 1.2.3-korg