?? configmanager.java
字號:
/**
*
*/
package com.justin.config;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.io.*;
import com.justin.log.*;
/**
* @author Justin
*
* 單例模式實現,此類用來讀取并存放程序的配置信息
*/
public class ConfigManager {
//用戶配置文件名
public static String USER_CONFIG_FILE = "config.properties";
//應用程序根目錄
public static String APP_ROOT = System.getProperty("user.dir");
//存放接收數據的目錄
public static String RECIEVED_DATA_DIR = "Recieved_Data_Dir";
//監測接收數據間隔
public static String DETECT_INTERVAL_RECEIVED = "Detect_Interval_Received";
//監測接收數據文件類型
public static String RECEIVED_FILE_TYPE = "Received_File_Type";
//發現后進行標記的文件類型
public static String MARKED_FILE_TYPE = "Found_File_Type";
//處理完成后的文件類型
public static String PROCESSED_FILE_TYPE = "Processed_File_Type";
//存放處理完后文件的目錄
public static String PROCESSED_DATA_DIR = "Processed_Data_Dir";
//監測處理完文件的間隔
public static String DETECT_INTERVAL_PROCESSED = "Detect_Interval_Processed";
//接收到的文件的備份目錄
public static String DATA_BACKUP_DIR = "Data_Backup_Dir";
//調試狀態
public static String DEGUB_STATE = "Debug_State";
//存放默認配置
public static Properties defaultProp;
//存放用戶配置
private static Properties userProp;
private static ConfigManager config = null;
public synchronized static ConfigManager getInstance() {
if (config == null) {
config = new ConfigManager();
}
return config;
}
private ConfigManager() {
try {
//設置默認屬性
defaultProp = new Properties();
defaultProp.setProperty(RECIEVED_DATA_DIR, "C:\\RecievedData");
defaultProp.setProperty(DETECT_INTERVAL_RECEIVED, "5000");
defaultProp.setProperty(RECEIVED_FILE_TYPE, "xls");
defaultProp.setProperty(MARKED_FILE_TYPE, "found");
defaultProp.setProperty(PROCESSED_FILE_TYPE, "");
defaultProp.setProperty(PROCESSED_DATA_DIR, "C:\\ProcessedData");
defaultProp.setProperty(DETECT_INTERVAL_PROCESSED, "5000");
defaultProp.setProperty(DATA_BACKUP_DIR, "C:\\BackupData");
defaultProp.setProperty(DEGUB_STATE, "false");
//加載用戶定制的配制文件
userProp = new Properties();
userProp.load(new FileInputStream(APP_ROOT + File.separator
+ USER_CONFIG_FILE));
//輸出默認屬性及配置文件的內容
userProp.list(System.out);
} catch (IOException ex) {
System.out.println("No Configuration File");
}
}
//獲取默認Long型屬性
public synchronized long getDefaultLongProp(String key) {
long value = 0;
if (defaultProp.containsKey(key)) {
value = Long.parseLong(defaultProp.getProperty(key));
}
return value;
}
//獲取默認String型屬性
public synchronized String getDefaultStringProp(String key) {
String value = "";
if (defaultProp.containsKey(key)) {
value = defaultProp.getProperty(key);
}
return value;
}
//獲取默認boolean型屬性
public synchronized boolean getDefaultBooleanProp(String key) {
boolean value = false;
if (defaultProp.containsKey(key)) {
value = Boolean.getBoolean(defaultProp.getProperty(key));
}
return value;
}
//獲取String型屬性
public synchronized String getStringProperty(String key) {
String value = "";
if (userProp.containsKey(key)) {
value = userProp.getProperty(key);
} else {
value = getDefaultStringProp(key);
}
return value;
}
//獲取Long型屬性
public synchronized long getLongProperty(String key) {
long value = 0;
if (userProp.containsKey(key)) {
try {
value = Long.parseLong(userProp.getProperty(key));
} catch (NumberFormatException e) {
value = getDefaultLongProp(key);
}
} else {
value = getDefaultLongProp(key);
}
return value;
}
//獲取boolean型屬性
public synchronized boolean getBooleanProperty(String key) {
boolean value = false;
if (userProp.containsKey(key)) {
try {
value = Boolean.getBoolean((userProp.getProperty(key)));
} catch (NumberFormatException e) {
value = getDefaultBooleanProp(key);
}
} else {
value = getDefaultBooleanProp(key);
}
return value;
}
//在用戶界面顯示所有的屬性及值
public synchronized void list(LogServer log) {
log.showMessage("-- listing properties --");
Hashtable h = new Hashtable();
for (Enumeration e = userProp.keys(); e.hasMoreElements();) {
String key = (String) e.nextElement();
h.put(key, (String) userProp.get(key));
}
for (Enumeration e = h.keys(); e.hasMoreElements();) {
String key = (String) e.nextElement();
String val = (String) h.get(key);
if (val.length() > 40) {
val = val.substring(0, 37) + "...";
}
log.showMessage(key + "=" + val);
}
}
//在輸出流中顯示所有屬性及值
public synchronized void list(PrintStream out) {
userProp.list(out);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -