📄 configutil.java.svn-base
字号:
package com.fetion.cmpp.server.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
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.common.util.ConfigException;
import com.fetion.cmpp.common.util.NameTypeBean;
import com.fetion.cmpp.common.util.NameValueBean;
import com.fetion.cmpp.server.conf.ServerConfig;
import com.fetion.cmpp.server.conf.ServerConfigMBean;
/**
* 服务器端的配置工具类
* @author Administrator
*
*/
public class ConfigUtil {
private static Log log = LogFactory.getLog(ConfigUtil.class);
private static final ConfigurationManager cm = ConfigurationManager
.getInstance();
public static void save(ServerConfigMBean serverConfig) throws ConfigException {
if (serverConfig == null)
return;
File file = new File(Constants.CMPP_CONFIG_NAME + File.separator
+ Constants.SERVER_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");
List<NameValueBean> nameValues = getNameValues(serverConfig);
if(nameValues != null && nameValues.size() > 0){
for(NameValueBean nameValue : nameValues)
config.setProperty(nameValue.getName(), nameValue.getValue().toString());
}
log.info("trying to save file");
cm.save(handler, config);
log.info("file successfully saved");
} catch (Exception e) {
throw new ConfigException("save config error : "+e);
}
}
public static ServerConfig loadConfig() throws ConfigException {
Class ServerConfigClass = Constants.SERVER_CONFIG_CLASS;
File file = new File(Constants.CMPP_CONFIG_NAME + File.separator
+ "server.xml");
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");
ServerConfig bean = (ServerConfig)ServerConfigClass.newInstance();
BeanMap map = BeanMap.create(bean);
List<NameTypeBean> fieldNames = getFieldNames(ServerConfig.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, Constants.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 Properties loadConfig(String propertiesName)
throws ConfigException {
Properties config = null;
File configFile = new File(propertiesName);
if (!configFile.exists())
throw new ConfigException("no file found,use the default value");
else {
try {
InputStream is = new FileInputStream(configFile);
if (is == null)
throw new ConfigException(
"no file found,use the default value");
config = new Properties();
config.load(is);
} catch (IOException e) {
// TODO Auto-generated catch block
throw new ConfigException("no file found", e);
}
}
return config;
}
public static List<NameValueBean> getNameValues(Object bean) {
if (bean != null) {
List<NameValueBean> nameValues = new ArrayList<NameValueBean>();
BeanMap map = BeanMap.create(bean);
Field[] fields = bean.getClass().getDeclaredFields();
if (fields != null && fields.length > 0){
String fieldName = null;
Object value;
for (Field field : fields) {
fieldName = field.getName();
value = map.get(fieldName);
nameValues.add(new NameValueBean(fieldName,value));
}
}
return nameValues;
} else
return null;
}
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) {
// Properties config;
// try {
// ConfigUtil.loadConfig(Constants.CMPP_CONFIG_NAME + File.separator
// + "serverConfig.properties");
// // System.out.println(config);
// } catch (ConfigException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
ServerConfig conf = new ServerConfig();
conf.setCmppServerPort(123);
conf.setJmxServerPort(234);
conf.setShedule_Expression("aa");
// ConfigUtil.getNameValues(conf);
// try {
// ConfigUtil.save(conf);
// } catch (ConfigException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
try {
ServerConfig conf1 =ConfigUtil.loadConfig();
System.out.println(conf1);
} catch (ConfigException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -