📄 clientconfigutil.java
字号:
package com.fetion.cmpp.client.conf;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import net.sf.cglib.beans.BeanMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jconfig.Configuration;
import org.jconfig.ConfigurationManager;
import org.jconfig.handler.XMLFileHandler;
import com.fetion.cmpp.client.ClientConstants;
import com.fetion.cmpp.common.util.ConfigException;
import com.fetion.cmpp.common.util.NameTypeBean;
/**
* 客户端的配置工具类
* @author Administrator
*
*/
public class ClientConfigUtil {
private static Log log = LogFactory.getLog(ClientConfigUtil.class);
private static final ConfigurationManager cm = ConfigurationManager
.getInstance();
public static ClientConfig loadConfig() throws ConfigException {
Class clientConfigClass = ClientConfig.class;
File file = new File(ClientConstants.CMPP_CONFIG_NAME + File.separator
+ ClientConstants.CLIENT_XML_CONFIG);
if (!file.exists())
throw new ConfigException("no file found,use the default value");
XMLFileHandler handler = new XMLFileHandler();
handler.setFile(file);
try {
log.info("trying to load file");
cm.load(handler, "myConfig1");
log.info("file successfully processed");
Configuration config = ConfigurationManager
.getConfiguration("myConfig1");
ClientConfig bean = (ClientConfig)clientConfigClass.newInstance();
BeanMap map = BeanMap.create(bean);
List<NameTypeBean> fieldNames = getFieldNames(ClientConfig.class);
String fieldName = null;
String typeName = null;
for(NameTypeBean nameType : fieldNames){
fieldName = nameType.getName();
typeName = nameType.getType().getName();
if(typeName.equals("int")){
map.put(nameType.getName(), config.getIntProperty(fieldName, ClientConstants.DEFAULT_PORT));
}else if(typeName.equals("boolean")){
map.put(nameType.getName(), config.getBooleanProperty(fieldName, false));
}else if(typeName.equals("double")){
map.put(nameType.getName(), config.getDoubleProperty(fieldName, 0.00));
}else if(typeName.equals("long")){
map.put(nameType.getName(), config.getLongProperty(fieldName, 0L));
}else {
map.put(nameType.getName(), config.getProperty(fieldName));
}
//map.put(nameType.getName(), config.getProperty(nameType.getName()));
}
return bean;
} catch (Exception e) {
throw new ConfigException("save config error : "+e);
}
}
public static List<NameTypeBean> getFieldNames(Class beanClass) {
if (beanClass != null) {
List<NameTypeBean> nameValues = new ArrayList<NameTypeBean>();
Field[] fields = beanClass.getDeclaredFields();
if (fields != null && fields.length > 0){
String fieldName = null;
Class type = null;
for (Field field : fields) {
fieldName = field.getName();
type = field.getType();
nameValues.add(new NameTypeBean(fieldName,type));
}
}
return nameValues;
} else
return null;
}
/**
* @param args
*/
public static void main(String[] args) {
try {
ClientConfig conf = ClientConfigUtil.loadConfig();
System.out.println(conf);
} catch (ConfigException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -