com.ch.utils.PropertyReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ch-java-sdk Show documentation
Show all versions of ch-java-sdk Show documentation
The Charles Hudson Java SDK wraps the REST API in a lightweight library.
The Java SDK enables you to develop a fast, flexible, and customized integration with CH cloud solution.
/**
*
*/
package com.ch.utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.rowset.serial.SerialException;
import org.apache.commons.lang3.StringUtils;
import com.ch.service.exception.ServiceException;
/**
* @author ravindra
*
*/
public class PropertyReader {
private final static Logger LOGGER = Logger.getLogger(PropertyReader.class.getName());
private static PropertyCache instance;
protected static Properties properties = null;
/**
*
* @param propFilePath
* @return Property instance
* @throws SerialException
*/
private Properties loadProperties(String propFilePath) throws ServiceException {
Properties properties = null;
if (StringUtils.isNotEmpty(propFilePath)) {
InputStream in = null;
try {
in = new FileInputStream(propFilePath);
Reader inStream = new InputStreamReader(in);
properties = new Properties();
properties.load(inStream);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Error loading the file " + propFilePath);
throw new ServiceException("Unable to read configuration file " + propFilePath, e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Error while closing the file " + propFilePath);
}
}
} else {
throw new ServiceException("Configuration file not set. Please provide a valid .properties file");
}
return properties;
}
public static synchronized PropertyCache getInstance(String chConfigFile) throws ServiceException {
if (instance == null) {
synchronized (PropertyReader.class) {
if (instance == null) {
Properties props = new PropertyReader().loadProperties(chConfigFile);
instance = new PropertyCache();
PropertyCache.properties = props;
}
}
}
return instance;
}
public static Properties getAllProperties() {
return PropertyCache.properties;
}
}