package com.xxx.config;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 读配置集成类<br>
* 如果不设置配置源,默认使用xxxxxx的配置
* @author
*/
public class ConfigManager {
private static Configs _default = null;
//private static HashMap configs = new HashMap();
private static boolean hasdefault = true;
/**
* 得到指定配置文件的配置信息
* @param conf 配置文件名
* @return Configs对象
*/
public static Configs getConfigs(String module) {
Configs cf = null;//(Configs) configs.get(module);
if (cf == null) {
Configs df = getDefaultConfigs();
if (df == null)
return null;
String file = df.getConfig(module);
if (file != null && file.length() > 0 && new java.io.File(file).isFile()) {
cf = new Configs();
cf.init(file, df.getConfigProperty());
} else {
cf = df;
}
//configs.put(module, cf);
}
return cf;
}
/**
* 得到指定配置文件的配置信息
* @param conf 配置文件名
* @return Configs对象
*/
public static void setDefault(String module) {
Configs df = getDefaultConfigs();
if (df == null)
return;
String file = df.getConfig(module);
if (file != null && file.length() > 0 && new java.io.File(file).isFile()) {
Configs cf = new Configs();
cf.init(file, df.getConfigProperty());
_default = cf;
}
}
/**
* 得到指定配置文件的配置信息
* @param conf 配置文件名
* @return Configs对象
*/
public static void resetDefault() {
_default = null;
}
/**
* 根据配置节点名得到配置值
* @param key 配置节点名
* @return 配置值
*/
public static String getKey(String key) {
return getKey(key, null, false);
}
/**
* 根据配置节点名得到配置值
* @param key 配置节点名
* @param defaul 默认值
* @return 如果有配置值,则返回配置值,否则返回默认值
*/
public static String getKey(String key, String defaul) {
return getKey(key, defaul, false);
}
/**
* 根据配置节点名得到配置值
* @param key 配置节点名
* @param islog 是否写日志。如果为true且返回的配置值为null,则写日志
* @return 配置值
*/
public static String getKey(String key, boolean islog) {
return getKey(key, null, islog);
}
/**
* 根据配置节点名得到配置值
* @param key 配置节点名
* @param defaul 默认值
* @param islog 是否写日志。如果为true且返回的配置值为null,则写日志
* @return 如果有配置值,则返回配置值,否则返回默认值
*/
public static String getKey(String key, String defaul, boolean islog) {
Configs cf = getDefaultConfigs();
if (cf != null)
return cf.getConfig(key, defaul, islog);
return defaul;
}
/**
* 得到默认配置
* @return Configs对象
*/
public static Configs getDefaultConfigs() {
if (_default == null && hasdefault) {
loadDefaultConfig();
}
return _default;
}
static void loadDefaultConfig() {
String f = "";
if(File.separatorChar == '\\')
f = "C:\\xxx.conf";
else
f = "/xxx.conf";
System.out.println("配置文件地址"+f+"---------------------");
File file = new File(f);
if (!file.isFile()) {
hasdefault = false;
return;
}
_default = new Configs();
_default.init(f);
}
}