kr.pe.kwonnam.hibernate4memcached.util.PropertiesUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate4-memcached-core Show documentation
Show all versions of hibernate4-memcached-core Show documentation
hibernate4 memcached L2 cache implementation.
package kr.pe.kwonnam.hibernate4memcached.util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @author KwonNam Son ([email protected])
*/
public class PropertiesUtils {
private static Logger LOG = LoggerFactory.getLogger(PropertiesUtils.class);
public static Properties loadFromClasspath(String resourcePath) {
if (StringUtils.isEmpty(resourcePath)) {
throw new IllegalArgumentException("resourcePath must not be empty.");
}
LOG.debug("resourcePath to load : {}", resourcePath);
Properties properties = new Properties();
try {
InputStream resourceStream = PropertiesUtils.class.getClassLoader().getResourceAsStream(resourcePath);
if (resourceStream == null) {
throw new IllegalStateException("Failed to load properties from '" + resourcePath + "'. You might set illegal resource path. Leading slash(/) is not allowed.");
}
if (resourcePath.toLowerCase().endsWith(".xml")) {
properties.loadFromXML(resourceStream);
} else {
properties.load(resourceStream);
}
} catch (IOException e) {
throw new IllegalStateException("Failed to load properties from '" + resourcePath + "'.");
}
return properties;
}
}