From 43d31d47f10c9eaaf23fb6617d3fb026bdf4b01c Mon Sep 17 00:00:00 2001 From: Jacob Peterson <14127217+Petersoj@users.noreply.github.com> Date: Wed, 27 Oct 2021 23:49:57 -0600 Subject: [PATCH] Change PropertyUtil to use system class loader as fallback This is an attempt to fix NPE/ParseExceptions thrown on init due to iqfeed4j.default.properties not existing on the system class loader's classpath. --- .../iqfeed4j/util/properties/PropertyUtil.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/jacobpeterson/iqfeed4j/util/properties/PropertyUtil.java b/src/main/java/net/jacobpeterson/iqfeed4j/util/properties/PropertyUtil.java index b4e5fed..deb864e 100644 --- a/src/main/java/net/jacobpeterson/iqfeed4j/util/properties/PropertyUtil.java +++ b/src/main/java/net/jacobpeterson/iqfeed4j/util/properties/PropertyUtil.java @@ -16,7 +16,8 @@ public class PropertyUtil { private static final Logger LOGGER = LoggerFactory.getLogger(PropertyUtil.class); - private static final Map<String, Properties> CACHED_PROPERTIES = Collections.synchronizedMap(new HashMap<>()); + + public static final Map<String, Properties> CACHED_PROPERTIES = Collections.synchronizedMap(new HashMap<>()); /** * Gets a string property from a property file. Will try to IO load the properties in the property file if not @@ -80,11 +81,14 @@ public static String getProperty(String propertyFile, String defaultPropertyFile * @return the properties */ public synchronized static Properties loadPropertyFile(String propertyFile, String defaultPropertyFile) { - Properties properties = null; + ClassLoader classLoader = PropertyUtil.class.getClassLoader(); + if (classLoader == null) { + classLoader = ClassLoader.getSystemClassLoader(); + } // Load the default property file if exists Properties defaultProperties = null; - InputStream defaultPropertyStream = ClassLoader.getSystemClassLoader().getResourceAsStream(defaultPropertyFile); + InputStream defaultPropertyStream = classLoader.getResourceAsStream(defaultPropertyFile); if (defaultPropertyStream != null) { defaultProperties = new Properties(); @@ -109,7 +113,8 @@ public synchronized static Properties loadPropertyFile(String propertyFile, Stri } // Load the property file - InputStream propertyStream = ClassLoader.getSystemClassLoader().getResourceAsStream(propertyFile); + Properties properties = null; + InputStream propertyStream = classLoader.getResourceAsStream(propertyFile); if (propertyStream != null) { // Add default properties if they were found