package com.westdream.config;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletContext;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
public class Configuration implements Serializable, ConfigConstant {
private static final long serialVersionUID = -8572272649468107517L;
private static Configuration config = null;
private HashMap<String, ActionConfig> actionMappings = new HashMap<String, ActionConfig>();
private HashMap<String, FormConfig> formMappings = new HashMap<String, FormConfig>();
private JDBCConfig jdbcConfig = null;
public HashMap<String, FormConfig> getFormMappings() {
return formMappings;
}
private Configuration() {
}
public static Configuration newInstance() {
if (null == config) {
config = new Configuration();
}
return config;
}
public Configuration configure(final ServletContext context) {
return configure(DEFAULT_CONFIG_FILE, context);
}
@SuppressWarnings("unchecked")
public Configuration configure(final String cfg , final ServletContext context)
throws ConfigurationException {
if (null == cfg | "".equals(cfg.trim()))
throw new ConfigurationException("Configuration file is required.");
/** 使用JDOM的SAX解析配置文件 **/
SAXBuilder saxBuilder = new SAXBuilder();
try {
Document document = saxBuilder.build(Thread.currentThread()
.getContextClassLoader().getResourceAsStream(cfg));
Element root = document.getRootElement();
List<Element> actionElmts = (List<Element>)XPath.selectNodes(root, VIEW_ACTION);
for(Element actionElmt : actionElmts) {
ActionConfig actionConfig = new ActionConfig();
/** 将配置文件的Action属性装配到ActionConfig **/
actionConfig.setPath(context.getContextPath() + actionElmt.getAttributeValue(ACTION_ATTRIB_PATH));
actionConfig.setClassName(actionElmt.getAttributeValue(ACTION_ATTRIB_CLASS));
actionConfig.setName(actionElmt.getAttributeValue(ACTION_ATTRIB_NAME));
/** 将配置文件的Action的result子节点装配到ActionConfig **/
List<Element> resultElmts = actionElmt.getChildren(ACTION_CHILDREN_RESULT);
for(Element resultElmt : resultElmts) {
ResultConfig resultConfig = new ResultConfig();
Attribute nameAttrib = resultElmt.getAttribute(ACTION_CHILDREN_RESULT_NAME);
Attribute pathAttrib = resultElmt.getAttribute(ACTION_CHILDREN_RESULT_PATH);
Attribute typeAttrib = resultElmt.getAttribute(ACTION_CHILDREN_RESULT_TYPE);
resultConfig.setName(nameAttrib.getValue());
resultConfig.setForward(typeAttrib.getBooleanValue());
resultConfig.setPath(pathAttrib.getValue());
actionConfig.getResults().put(resultConfig.getName(), resultConfig);
}
/** 将ActionConfig装配到Configuration **/
actionMappings.put(actionConfig.getPath(), actionConfig);
}
/** 将配置文件的form属性装配到FormConfig **/
List<Element> formElmts = (List<Element>)XPath.selectNodes(root, VIEW_FORMS_FORM);
for(Element formElmt : formElmts) {
FormConfig formConfig = new FormConfig();
formConfig.setName(formElmt.getAttributeValue(FORM_ATTRIB_NAME));
formConfig.setClassName(formElmt.getAttributeValue(FORM_ATTRIB_CLASS));
List<Element> propertyElmts = formElmt.getChildren(FORM_CHILDREN_PROP);
for(Element propertyElmt : propertyElmts) {
PropertyConfig propertyConfig = new PropertyConfig();
propertyConfig.setName(propertyElmt.getAttributeValue(FORM_CHILDREN_PROP_NAME));
propertyConfig.setType(propertyElmt.getAttributeValue(FORM_CHILDREN_PROP_TYPE));
propertyConfig.setValue(propertyElmt.getAttributeValue(FORM_CHILDREN_PROP_VALUE));
formConfig.getProperties().add(propertyConfig);
}
formMappings.put(formConfig.getName(), formConfig);
}
/** 配置JDBC **/
Element driverClassNameElmt = (Element)XPath.selectSingleNode(root, JDBC_DRIVER_CLASS_NAME);
Element urlElmt = (Element)XPath.selectSingleNode(root, JDBC_URL);
Element userElmt = (Element)XPath.selectSingleNode(root, JDBC_USER);
Element passwordElmt = (Element)XPath.selectSingleNode(root, JDBC_PASSWORD);
Element maxActiveElmt = (Element)XPath.selectSingleNode(root, JDBC_MAX_ACTIVE);
Element maxWaitElmt = (Element)XPath.selectSingleNode(root, JDBC_MAX_WAIT);
jdbcConfig = new JDBCConfig();
jdbcConfig.setDriverClassName(driverClassNameElmt.getText());
jdbcConfig.setUrl(urlElmt.getText());
jdbcConfig.setUser(userElmt.getText());
jdbcConfig.setPassword(passwordElmt.getText());
jdbcConfig.setMaxActive(Integer.valueOf(maxActiveElmt.getText()));
jdbcConfig.setMaxWait(Integer.valueOf(maxWaitElmt.getText()));
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return config;
}
public HashMap<String, ActionConfig> getActionMappings() {
return actionMappings;
}
public JDBCConfig getJdbcConfig() {
return jdbcConfig;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
view.rar (61个子文件)
view
.project 1KB
.settings
com.genuitec.eclipse.j2eedt.core.prefs 1KB
.mymetadata 286B
WebRoot
success.jsp 975B
WEB-INF
web.xml 1KB
lib
ant.jar 715KB
xml-apis.jar 122KB
xerces.jar 937KB
jaxen-core.jar 157KB
mysql-connector-java-5.1.7-bin.jar 693KB
saxpath.jar 23KB
xalan.jar 1.23MB
jdom.jar 150KB
jaxen-jdom.jar 6KB
classes
configure.xml 1KB
com
westdream
jdbc
dbcp
utils
DBSession.class 1KB
action
ListMessageAction.class 1KB
Action.class 727B
MessageAction.class 1KB
UserAction.class 1KB
ActionServlet.class 6KB
config
ActionConfig.class 2KB
JDBCConfig.class 2KB
ConfigurationException.class 839B
Configuration.class 7KB
ConfigConstant.class 1KB
FormConfig.class 1KB
PropertyConfig.class 1KB
ResultConfig.class 946B
filter
charset
CharsetFilter.class 1KB
service
impl
MessageServiceImpl.class 4KB
MessageService.class 567B
pojo
Userinfo.class 836B
Message.class 2KB
converter
index.jsp 962B
META-INF
MANIFEST.MF 39B
failure.jsp 784B
list.jsp 1KB
leaveMsg.jsp 1KB
.myeclipse
src
configure.xml 1KB
com
westdream
jdbc
dbcp
utils
DBSession.java 1KB
action
Action.java 424B
UserAction.java 612B
MessageAction.java 821B
ListMessageAction.java 654B
config
ResultConfig.java 537B
ConfigConstant.java 1KB
FormConfig.java 803B
Configuration.java 5KB
PropertyConfig.java 858B
ActionConfig.java 856B
ConfigurationException.java 475B
JDBCConfig.java 1KB
filter
charset
CharsetFilter.java 934B
service
impl
MessageServiceImpl.java 2KB
MessageService.java 363B
pojo
Userinfo.java 544B
Message.java 1KB
ActionServlet.java 4KB
converter
.classpath 1012B
view_sql.sql 361B
共 61 条
- 1
资源评论
- shazi1681682013-01-27能否写个文档,这样方便阅读代码。
- mu045512c2012-07-13没有指导怎么用 用起来不方便
kings988
- 粉丝: 22
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功