读取配置文件

来源:互联网 发布:coc野蛮人之王升级数据 编辑:程序博客网 时间:2024/06/05 14:23
import java.util.Properties;
import org.apache.log4j.LogManager;

import org.apache.log4j.Logger;


public class ConfigurationManager {

private static Logger log = LogManager.getLogger(ConfigurationManager.class);

private static Properties configProps = new Properties();
private static String CONFIG_FILE = "lmk.properties";


static {
initialize();
}

private static void initialize() {


log.trace("Begin to intitialize ConfiguraitonManager...");


try {
configProps.load(ConfigurationManager.class.getResourceAsStream(CONFIG_FILE));

String loginConfigFile = configProps.getProperty(ConfigurationManager.LOGIN_CONFIG);
loginConfigFile = ConfigurationManager.class.getResource(loginConfigFile).getPath();
System.setProperty(ConfigurationManager.LOGIN_CONFIG,loginConfigFile);

String waspLocation = configProps.getProperty(ConfigurationManager.WSAP_LOCATION);
waspLocation = ConfigurationManager.class.getResource(waspLocation).getPath();
System.setProperty(ConfigurationManager.WSAP_LOCATION,waspLocation);

} catch (Exception e) {
log.error("Intitializing ConfiguraitonManager error: ", e);
System.exit(0);
}
log.trace("Finish intitializing ConfiguraitonManager.");

}


/**
* Get the configured value as integer. If not correctly configured, return 0.

* @param configKey
*            the key name
* @return the int value
*/
public static String getConfigValue(String configKey) {
String configValue = (String) configProps.getProperty(configKey);
log.debug("ConfigurationManager.getConfiguration("
+ configKey + ")");
log.debug("configValue=" + configValue);
return configValue;
}


public static int getConfigValueAsInt(String configKey) {
int intValue = 0;
try {
intValue = Integer.parseInt(getConfigValue(configKey));
} catch (NumberFormatException e) {
intValue = 0;
log.error("Incorrect int value configured for " + configKey);
}
return intValue;
}

public static boolean getConfigValueAsBoolean(String configKey){
boolean booleanValue = false;
booleanValue = Boolean.parseBoolean(getConfigValue(configKey));
return booleanValue;
}

}
原创粉丝点击