Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
simplify props loading
Browse files Browse the repository at this point in the history
  • Loading branch information
kosteman committed Dec 29, 2016
1 parent 7249740 commit 3038cc5
Showing 1 changed file with 8 additions and 25 deletions.
33 changes: 8 additions & 25 deletions src/main/java/ru/sbtqa/tag/qautils/properties/Props.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.sbtqa.tag.qautils.properties;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
Expand All @@ -22,18 +21,11 @@ public class Props {
* @throws java.io.IOException if there are an error to read properties file
*/
public Props() throws IOException {
System.setProperty("logback.configurationFile", "config/logback.xml");
String sConfigFile = System.getProperty("BDDConfigFile", "config/application.properties");
properties = new Properties();
log.info("Loading properties from: " + sConfigFile);
try (InputStream streamFromResources = Props.class.getClassLoader().getResourceAsStream(sConfigFile);
InputStream streamFromFilesystem = new FileInputStream(sConfigFile)) {
//first try to load properties from resources. If failed try to load from filesystem
if (streamFromResources != null) {
properties.load(streamFromResources);
} else {
properties.load(streamFromFilesystem);
}
try (InputStream streamFromResources = Props.class.getClassLoader().getResourceAsStream(sConfigFile);) {
properties.load(streamFromResources);
}
}

Expand Down Expand Up @@ -63,7 +55,7 @@ public static synchronized Props getInstance() {
private String getProp(String name) {
String val = getProps().getProperty(name, "");
if (val.isEmpty()) {
log.error("Property {} was not found in Props", name);
log.warn("Property {} was not found in properties file", name);
}
return val.trim();
}
Expand All @@ -75,11 +67,7 @@ private String getProp(String name) {
* @return property value.
*/
public static String get(String prop) {
if (instance != null) {
return instance.getProp(prop);
} else {
return null;
}
return getInstance().getProp(prop);
}

/**
Expand All @@ -90,18 +78,13 @@ public static String get(String prop) {
* @return property value.
*/
public static String get(String prop, String defaultValue) {
if (instance != null) {
String value = instance.getProp(prop);

if (value.isEmpty()) {
return defaultValue;
}
String value = getInstance().getProp(prop);

return value;
} else {
return null;
if (value.isEmpty()) {
return defaultValue;
}

return value;
}

/**
Expand Down

0 comments on commit 3038cc5

Please sign in to comment.