if (propertiesLoader.thePropertiesFileExists()) {
System.out.println("Properties file exists: " + propertiesFileName);
} else { ... }
String serverName = propertiesLoader.getProperty("serverName");
...
And the properties loader class:
package alan.lupsha.properties;
import org.apache.log4j.Logger;
import java.util.Properties;
import java.io.InputStream;
public class PropertiesLoader {
private static final Logger logger = Logger.getLogger(PropertiesLoader.class);
private Properties props = null;
private String propertiesFileName = null;
private boolean propertiesFileExists = false;
public PropertiesLoader(String propertiesFileName) {
this.propertiesFileName = propertiesFileName;
try {
props = new Properties();
ClassLoader cl = this.getClass().getClassLoader();
// does not work in 11G: java.net.URL url = cl.getResource(propertiesFileName);
InputStream in = cl.getResourceAsStream( propertiesFileName );
if( in != null )
{
props.load(in);
setPropertiesFileExists(true);
}
else
{
logger.warn("InputStream is null while trying to load properties file: " + propertiesFileName );
}
} catch (Exception e) {
logger.error("Error while loading properties from file "
+ propertiesFileName + ". Error is: " + e.toString());
System.out.println("Error while loading properties from file "
+ propertiesFileName + ". Error is: " + e.toString());
}
}
public String getProperty(String propertyName) {
String returnStr = "";
if (props == null) {
logger.error("Sorry, your props file couldn't be loaded: " + propertiesFileName);
} else {
returnStr = props.getProperty(propertyName);
if (returnStr == null) {
returnStr = "";
}
}
return returnStr;
}
public void setPropertiesFileExists(boolean propertiesFileExists) {
this.propertiesFileExists = propertiesFileExists;
}
public boolean thePropertiesFileExists() {
return propertiesFileExists;
}
}
No comments:
Post a Comment