Created
July 4, 2013 15:56
-
-
Save danielpcox/5928794 to your computer and use it in GitHub Desktop.
Simple, static, global configuration. Depends on Log4j and Guava.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rmt.analytics.tagger.configuration; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.util.Properties; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import com.google.common.base.Charsets; | |
import com.google.common.io.Files; | |
import com.google.common.io.Resources; | |
/** | |
* Simple, static, global configuration. | |
*/ | |
public class Config { | |
private static Logger logger = LogManager.getLogger("Config"); | |
public static final String CONFIG_DEFAULTS_FILE = "/configDefaults.properties"; | |
private static Properties props; | |
static { | |
props = new Properties(); | |
try { | |
props.load(Config.class.getResourceAsStream(CONFIG_DEFAULTS_FILE)); | |
} catch (IOException e) { | |
logger.error("Could not load default config file '" + CONFIG_DEFAULTS_FILE + "': " + e.getMessage()); | |
} | |
} | |
public static void loadConfigFile(String path) { | |
try { | |
props.load(new FileInputStream(path)); | |
} catch (Exception e) { | |
logger.error("Could not load config file '" + path + "': " + e.getMessage()); | |
} | |
} | |
public static Object get(String field) { | |
Object value = props.get(field); | |
if (value==null) | |
throw new RuntimeException("Missing property in configuration: " + field); | |
else | |
return value; | |
} | |
public static Integer getInteger(String field) { | |
return Integer.parseInt(get(field).toString()); | |
} | |
public static Float getFloat(String field) { | |
return Float.parseFloat(get(field).toString()); | |
} | |
public static Boolean getBoolean(String field) { | |
return Boolean.parseBoolean(get(field).toString()); | |
} | |
public static String getString(String field) { | |
return get(field).toString(); | |
} | |
public static String getResourceAsString(String field) { | |
try { | |
return Resources.toString(Resources.getResource(Config.class, getString(field)), Charsets.UTF_8); | |
} catch (Exception e) { | |
throw new RuntimeException("Could not load resource '" + getString(field) + "' from config file field '" + field + "': " + | |
e.getMessage()); | |
} | |
} | |
public static String getFileAsString(String field) { | |
try { | |
return Files.toString(new File(getString(field)), Charsets.UTF_8); | |
} catch (IOException e) { | |
throw new RuntimeException("Could not load File '" + getString(field) + "' from config file field '" + field + "': " + | |
e.getMessage()); | |
} | |
} | |
public static boolean containsKey(Object field) { | |
return props.containsKey(field); | |
} | |
public static void put(Object field, Object value) { | |
props.put(field, value); | |
} | |
private Config() { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment