com.moviejukebox.tools.PropertiesUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yamj Show documentation
Show all versions of yamj Show documentation
Static analysis of MovieJukebox project
/*
* Copyright (c) 2004-2012 YAMJ Members
* http://code.google.com/p/moviejukebox/people/list
*
* Web: http://code.google.com/p/moviejukebox/
*
* This software is licensed under a Creative Commons License
* See this page: http://code.google.com/p/moviejukebox/wiki/License
*
* For any reuse or distribution, you must make clear to others the
* license terms of this work.
*/
package com.moviejukebox.tools;
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import org.apache.commons.lang3.StringUtils;
import static org.apache.commons.lang3.StringUtils.isBlank;
import org.apache.log4j.Logger;
/**
*
* @author altman.matthew
*/
public class PropertiesUtil {
private static final Logger logger = Logger.getLogger(PropertiesUtil.class);
private static final String logMessage = "PropertiesUtil: ";
private static final String PROPERTIES_CHARSET = "UTF-8";
private static Properties props = new Properties();
private static final String PREFERENCES_FILENAME = "preferences.xsl";
public static final String TRUE = "true";
public static final String FALSE = "false";
public static boolean setPropertiesStreamName(String streamName) {
return setPropertiesStreamName(streamName, true);
}
public static boolean setPropertiesStreamName(String streamName, boolean warnFatal) {
logger.info("Using properties file " + streamName);
InputStream propertiesStream = ClassLoader.getSystemResourceAsStream(streamName);
Reader reader = null;
try {
if (propertiesStream == null) {
propertiesStream = new FileInputStream(streamName);
}
reader = new InputStreamReader(propertiesStream, PROPERTIES_CHARSET);
props.load(reader);
} catch (IOException error) {
// Output a warning if required.
if (warnFatal) {
logger.error("Failed loading file " + streamName + ": Please check your configuration. The properties file should be in the classpath.");
} else {
logger.warn("Warning (non-fatal): User properties file '" + streamName + "', not found.");
}
return false;
} finally {
try {
if (propertiesStream != null) {
propertiesStream.close();
}
} catch (IOException e) {
// Ignore
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
// Ignore
}
}
return true;
}
public static String getProperty(String key) {
return props.getProperty(key);
}
public static String getProperty(String key, String defaultValue) {
return props.getProperty(key, defaultValue);
}
/**
* Return the key property as an integer
*
* @param key
* @param defaultValue
* @return
*/
public static int getIntProperty(String key, String defaultValue) {
String property = getProperty(key, defaultValue).trim();
try {
return Integer.parseInt(property);
} catch (NumberFormatException nfe) {
return Integer.parseInt(defaultValue);
}
}
/**
* Return the key property as an long
*
* @param key
* @param defaultValue
* @return
*/
public static long getLongProperty(String key, String defaultValue) {
String property = getProperty(key, defaultValue).trim();
try {
return Long.parseLong(property);
} catch (NumberFormatException nfe) {
return Long.parseLong(defaultValue);
}
}
/**
* Return the key property as a boolean
*
* @param key
* @param defaultValue
* @return
*/
public static boolean getBooleanProperty(String key, String defaultValue) {
String property = getProperty(key, defaultValue).trim();
try {
return Boolean.parseBoolean(property);
} catch (NumberFormatException nfe) {
return Boolean.parseBoolean(defaultValue);
}
}
/**
* Return the key property as a float
*
* @param key
* @param defaultValue
* @return
*/
public static float getFloatProperty(String key, String defaultValue) {
String property = getProperty(key, defaultValue).trim();
try {
return Float.parseFloat(property);
} catch (NumberFormatException nfe) {
return Float.parseFloat(defaultValue);
}
}
// Issue 309
public static Set> getEntrySet() {
// Issue 728
// Shamelessly adapted from: http://stackoverflow.com/questions/54295/how-to-write-java-util-properties-to-xml-with-sorted-keys
return new TreeMap