Created
December 9, 2024 13:13
-
-
Save borodicht/29ed03d9b86dd95c0c1904580b18c180 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.Properties; | |
public final class PropertyReader { | |
private static String propertiesPath = "/config.properties"; | |
private static volatile Properties properties; | |
private static InputStream inputStream; | |
private PropertyReader() { | |
} | |
private static String getCorrectPath() { | |
if (propertiesPath.charAt(0) != '/') | |
propertiesPath = "/" + propertiesPath; | |
return propertiesPath; | |
} | |
public static Properties readProperties() { | |
properties = new Properties(); | |
try { | |
inputStream = PropertyReader.class.getResourceAsStream(getCorrectPath()); | |
if (inputStream != null) | |
properties.load(inputStream); | |
} catch (Exception ex) { | |
if (inputStream != null) { | |
try { | |
inputStream.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
if (properties.getProperty("config_file") != null) { | |
Properties additionalProperties = getProperties(properties.getProperty("config_file")); | |
properties.putAll(additionalProperties); | |
} | |
return properties; | |
} | |
private static Properties loadProperties() { | |
return properties != null ? properties : readProperties(); | |
} | |
public static Properties getProperties(String path) { | |
propertiesPath = path; | |
return readProperties(); | |
} | |
public static String getProperty(String propertyName) { | |
return loadProperties().getProperty(propertyName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment