📄 confighelper.java
字号:
package net.sf.component.config;
import static net.sf.util.StringUtil.toArray;
import static net.sf.util.StringUtil.toMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import net.sf.component.config.ConfigItems.ConfigItem;
import net.sf.util.CipherUtil;
import net.sf.util.StringUtil;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
/**
* 一组配置的静态辅助方法
*/
@SuppressWarnings("serial")
public class ConfigHelper {
//选项值
private static Properties propList=new Properties(){
@Override
public String getProperty(String key) {
return filterCipher(super.getProperty(key));
}
};
//配置项
private static ConfigItems configItems;
//初始化配置项
static {
configItems=new ConfigItems();
//基本配置项
configItems.addItem("work.ry", "使用人员",true,"用户");
configItems.addItem("work.bm", "所在部门",true,"部门");
//设置主目录为启动目录
String testHome = new File("test").getAbsolutePath();
testHome = testHome.substring(0, testHome.length() - 4);
configItems.addItem("work.datahome", "数据主目录",true,testHome,true,new EclipseFieldEditorFactory(),StringUtil.toMap("type=Directory"));
configItems.addItem("work.importhome", "导入时的目录",false,testHome);
//先同步一下,因为读扩展点时会start插件,而start插件中也可能会读配置,如mail插件,以防死循环
synConfig();
//从扩展点获取配置项
IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint("net.sf.mywork.component.configItems");
if(extension != null){
IConfigurationElement[] configElements = extension.getConfigurationElements();
for(IConfigurationElement element:configElements){
try {
IConfigItemEditor editor=(IConfigItemEditor) (StringUtil.isNull(element.getAttribute("editor"))?null:element.createExecutableExtension("editor"));
configItems.addItem(element.getAttribute("code"),element.getAttribute("name"),"true".equalsIgnoreCase(element.getAttribute("configureable")),element.getAttribute("defaultValue"),"true".equalsIgnoreCase(element.getAttribute("configureable")),editor,StringUtil.toMap(element.getAttribute("editorParam")));
} catch (Exception e) {
e.printStackTrace();
}
}
}
//最终的同步
synConfig();
}
//载入实际配置并进行对比
private static void synConfig() {
try {
propList.load(new FileInputStream("work.properties"));
//补充缺少的属性
Properties prop=configItems.getInitProperties();
for(Iterator it=prop.keySet().iterator();it.hasNext();){
String key=(String) it.next();
if(!propList.containsKey(key))
propList.put(key,prop.getProperty(key));
}
store();
}
catch (Exception ie) {
//加入缺省属性
propList.putAll(configItems.getInitProperties());
store();
}
}
//保存配置
public static void store(){
try {
propList.store((new FileOutputStream("work.properties")), "Default");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static List<ConfigItem> getConfigItems(){
List<ConfigItem> list=new ArrayList<ConfigItem>();
for(ConfigItem ci:configItems.getItems())
if(ci.configureable)
list.add(ci);
return list;
}
public static Properties getPropList(){
return propList;
}
//不允许实例化
private ConfigHelper() {
}
//透明处理加密信息,可处理多组,但不嵌套的编码
private static String filterCipher(String cipher){
if(cipher == null)
return cipher;
int start = cipher.indexOf(CipherUtil.PREFIX);
int end = cipher.indexOf(CipherUtil.SUFIX,start);
if(start == -1 || end == -1)
return cipher;
String seg=cipher.substring(start+CipherUtil.PREFIX.length(),end);
String plain=cipher.substring(0,start)+CipherUtil.Decrypt(seg)+cipher.substring(end+CipherUtil.SUFIX.length());
return filterCipher(plain);
}
/***一组固定配置***/
//取人员
public static String getRy() {
return propList.getProperty("work.ry");
}
//取部门
public static String getBm() {
return propList.getProperty("work.bm");
}
public static String getDataHome() {
return propList.getProperty("work.datahome")+"/";
}
/***end ***/
/** 一组通用属性获取方法,随着配置越来越多,逐步向通用方法转化,缺少编译检查能力,但提供灵活性**/
//获取字符属性
public static String getStringProperty(String propertyName){
return propList.getProperty(propertyName,"");
}
//获取字符串数组属性
public static String[] getStringArrayProperty(String property){
return toArray(propList.getProperty(property));
}
//获取整型属性
public static int getIntegerProperty(String propertyName){
return StringUtil.parseInt(propList.getProperty(propertyName));
}
//获取整型数组属性
public static int[] getIntegerArrayProperty(String propertyName){
return StringUtil.string2int(getStringArrayProperty(propertyName),null);
}
//获取map属性
public static Map<String,String> getMapProperty(String property){
return toMap(propList.getProperty(property));
}
//获取布尔属性
public static boolean getBooleanProperty(String propertyName){
return "true".equalsIgnoreCase(propList.getProperty(propertyName));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -