This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nms-nmsserver/src/com/nms/server/common/Config.java
2018-09-27 16:17:06 +08:00

160 lines
4.4 KiB
Java

package com.nms.server.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import javax.swing.JOptionPane;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
/**
* 获取和保存myconfig信息的类
*
* @author ZGGG3
*
*/
public class Config {
private static Logger logger = Logger.getLogger(Config.class);
private static Properties properties;
private static String url = null;
public static Config config = Config.getInstance();
// private static ResourceBundle resource;
public static String getSystemDir() {
return System.getProperty("user.dir");
}
/**
* 单例 初始化获取实例
*
* @return
*/
public static Config getInstance() {
if (config == null)
return new Config();
return config;
}
/**
* 单例 重新读取文件已获取参数
*
* @return
*/
public static Config reinitialize() {
config = null;
return new Config();
}
/**
* 构造函数私有,用于单例实例
*/
private Config() {
//执行参数更新及检查
ConfigUpdate.getInstance();
//加载配置文件
URL urlObj = Config.class.getClassLoader().getResource("myconfig.properties");
// resource = ResourceBundle.getBundle(DefaultConfig.class.getName());
if(urlObj==null){
// JOptionPane.showMessageDialog(null, "未找到参数配文件!\n请运行"+Constants.COMMON_CONFIG_EXE_NAME+"初始化参数配置", "错误", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null, "Parameter configuration file not found! \nPlease run "+Constants.COMMON_CONFIG_EXE_NAME+"initialization parameter configuration", "Error", JOptionPane.ERROR_MESSAGE);
// JOptionPane.showMessageDialog(null, "i18n_server.Config.initConfig_n81i"+Constants.COMMON_CONFIG_EXE_NAME+"i18n_server.Config.initConfig.init_n81i", "i18n_server.Config.error_n81i", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}else{
url = urlObj.getPath().replaceAll("%20", " ");
}
properties = new Properties();
try {
properties.load(new FileInputStream(url));
} catch (IOException e) {
logger.error("Reading properties file error"+"",e);
}
}
/**
* 根据name获取value
*
* @param name
* @return
*/
public static Boolean getBoolan(String name,Boolean defaultValue) {
try {
String val = getString(name,defaultValue==null?null:defaultValue.toString());
return StringUtils.isEmpty(val)? null : Boolean.valueOf(val);
} catch (Exception e) {
logger.error("Digital formatting error", e);
}
return null;
}
/**
* 根据name获取value
*
* @param name
* @return
*/
public static Integer getInteger(String name,Integer defaultValue) {
try {
String val = getString(name,defaultValue==null?null:defaultValue.toString());
return StringUtils.isEmpty(val)? null : Integer.parseInt(val);
} catch (Exception e) {
logger.error("Digital formatting error", e);
}
return null;
}
/**
* 根据name获取value
*
* @param name
* @return
*/
public static String getString(String name,String defaultValue) {
String str = null;
/* 获取参数值:先从资源文件取值,如果为空,则从默认参数配置中取值 */
str = properties.getProperty(name,defaultValue);
if(StringUtils.isNotEmpty(str)){
str = str.trim();
}else{
logger.warn("Resource configuration file abnormality >> "+name+" Value is empty");
}
return str;
}
/**
* 向资源配置文件中添加或更新一个键值对
*
* @time Jul 7, 2011-3:52:45 PM
* @param key
* @param value
*/
public static void setValueByName(String key, String value) {
// 添加或更新键值对
properties.setProperty(key, value);
try {
// 保存到文件
if (url != null && !"".equals(url)) {
String fileName = url.substring(
url.lastIndexOf(File.separator), url.length());
properties.store(new FileOutputStream(url), fileName);
}
} catch (FileNotFoundException e) {
logger.error("The properties file Set Value file does not find ERROR "+"",e);
} catch (IOException e) {
logger.error("Properties file Set Value set value ERROR"+"",e);
}
}
/*
// 测试主函数
public static void main(String[] args) {
Config cfg=new Config();
String oid="mission.file.download.dir";
System.out.println("---------"+cfg.getString(oid));
}
*/
}