📄 xmlconfigfactory.java
字号:
package com.easyjf.dbo.config;
import com.easyjf.dbo.*;
import org.apache.log4j.Logger;
import org.dom4j.*;
import org.dom4j.io.*;
//~--- JDK imports ------------------------------------------------------------
import java.io.*;
import java.net.URL;
import java.util.*;
import javax.sql.*;
/**
*
* <p>
* Title: 基于XML文件格式的配置处理类
* </p>
*
* <p>
* Description:
* IConfigFactory实现了IConfigFactory,在EasyDBO启动的时候,将由DBOConfig的init方法启动XMLConfigFactory类,处理基于xml格式的配置信息。
* </p>
*
* <p>
* Copyright: Copyright (c) 2006
* </p>
*
* <p>
* Company: EasyJF开源团队-EasyDBO项目组
* </p>
*
* @author 大峡
* @version 1.0
*/
public class XMLConfigFactory implements IConfigFactory {
private static final Logger logger = (Logger) Logger
.getLogger(IConfigFactory.class.getName());
/**
* 存放配置文件Dom
*/
private Document doc;
/**
* 默认的配置文件名。 (注:该方式初始配置文件不能支持中文文件名及特殊格式的文件名!)
*/
private static final String defaultFileName = XMLConfigFactory.class
.getResource("/easyjf-dbo.xml").getPath();
// ~--- constructors -------------------------------------------------------
/**
* 使用默认配置文件进行初始化
*/
public XMLConfigFactory() {
this(defaultFileName);
}
/**
* 通过指定的文件名进行初始化,文件要求全路径
*
* @param fileName
* 文件名要求全路径
*/
public XMLConfigFactory(String fileName) {
try {
doc = parse(fileName);
} catch (Exception e) {
logger.error(fileName + "配置文件错误!" + e);
}
}
/**
* 通过输入流进行初始化,输入流中是xml格式配置信息
*
* @param in
*/
public XMLConfigFactory(InputStream in) {
try {
SAXReader reader = new SAXReader();
doc = reader.read(in);
logger.debug("数据库配置文件内容:" + doc.asXML());
} catch (Exception e) {
e.printStackTrace();
logger.warn("配置文件错误!" + e);
}
}
/**
* 配置文件信息初始化
*/
public void init(DBOConfig dboconfig) throws EasyDBOException {
try{
/**
* 初始化数据源
*/
initDataSource(dboconfig);
initTables(dboconfig.getTables());
}
catch(Exception e)
{
e.printStackTrace();
throw new IllegalArgumentException("配置文件加载错误,请确认类别是否正确!");
}
}
/**
* 初始化数据源
* @param dboconfig
* @throws EasyDBOException
*/
private void initDataSource(DBOConfig dboconfig) throws EasyDBOException {
if (doc == null) {
return;
}
List nodes = doc.selectNodes("/easyjf-dbo/datasource");
if (nodes != null) {
for (int i = 0; i < nodes.size(); i++) {
Element e = (Element) nodes.get(i);
Map property = new HashMap();
List props = e.selectNodes("property");
if (props != null) {
String type = (String) e.attributeValue("type");
String name = (String) e.attributeValue("name");
for (int j = 0; j < props.size(); j++) {
Element node = (Element) props.get(j);
property.put(node.attributeValue("name"), node
.getText());
}
// JNDI配置参数
String jndi = (String) property
.get(IConfigFactory.CONNECTION_JNDI);
String jndiFactory = (String) property
.get(IConfigFactory.CONNECTION_JNDIFACTORY);
// 连接沲使用
String poolFactory = (String) property
.get(IConfigFactory.CONNECTION_POOLFACTORY);
String driver = (String) property
.get(IConfigFactory.CONNECTION_DRIVER);
String url = (String) property
.get(IConfigFactory.CONNECTION_URL);
String userName = (String) property
.get(IConfigFactory.CONNECTION_USER);
String password = (String) property
.get(IConfigFactory.CONNECTION_PASSWORD);
// 系统配置参数
Object show_sql = property.get(IConfigFactory.SHOW_SQL);
Object optimize = property.get(IConfigFactory.OPTIMIZE);
Object cacheName = property.get(IConfigFactory.CACHE_NAME);
Object enable_cache = property
.get(IConfigFactory.ENABLE_CACHE);
String dialect = (String) property
.get(IConfigFactory.DIALECT);
dboconfig.setCacheName((String) cacheName);
if ("true".equals(show_sql)) {
dboconfig.setShow_sql(true);
}
if ("true".equals(optimize)) {
dboconfig.setOptimize(true);
}
if ("true".equals(enable_cache)) {
dboconfig.setEnableCache(true);
}
DataSourceManager dbm;
DataSource dsource;
if ("jndi".equals(type)) { // 通过JNDI连接数据源
dbm = new DataSourceManager(jndiFactory, url, jndi);
dsource = dbm.createJNDISource();
} else { // 通过驱动连接数据源
dbm = new DataSourceManager(driver, url, userName,
password, poolFactory);
dsource = dbm.createDriverSource();
}
if (dsource != null) {
DataSourceManager.addDataSource(name, dsource);
dboconfig.addDialect(name, dialect);
if (i == 0) { // 把第一个数据源设置成默认链接
dboconfig.setDataSource(dsource);
dboconfig.setDialect(dialect);
}
}
}
}
}
}
public Map initOther() {
if (doc == null) {
return null;
}
Map result = new HashMap();
return result;
}
private void initTables(Map tables) throws Exception {
if (doc == null) {
return;
}
if (!tables.isEmpty()) {
tables.clear();
}
/**
* 从easyjf-dbo.xml获取配置信息
*/
List nodes = doc.selectNodes("/easyjf-dbo/tables/class");
for (int i = 0; i < nodes.size(); i++) {
Element e = (Element) nodes.get(i);
DBTable table = new DBTable();
table.setName(e.attributeValue("table"));
table.setSchema(e.attributeValue("schema"));
table.setCatalog(e.attributeValue("catalog"));
if(e.attributeValue("lazy")!=null)table.setLazy(e.attributeValue("lazy"));
// 表主键配置信息
List idList = e.selectNodes("id");
if ((idList != null) && (idList.size() > 0)) {
Element id = (Element) idList.get(0);
if (id != null) {
List idColumn = id.selectNodes("column");
if ((idColumn != null) && (idColumn.size() > 0)) {
Element column = (Element) idColumn.get(0);
if (column != null) {
DBField field = new DBField();
field.setName(column.attributeValue("name"));
table.setId(column.attributeValue("name"));
try {
field.setLength(Integer.parseInt(column
.attributeValue("length")));
} catch (Exception ex) {
}
Class fieldType=com.easyjf.dbo.JavaTypeRegister.getInstance().findType(id.attributeValue("type"));
if(fieldType==null)fieldType=Class.forName(id.attributeValue("type"));
field.setType(fieldType);
table.addField(id.attributeValue("name"), field);
// field.setLength(new
// Integer(column.attributeValue("length")).intValue());.
}
}
List idGenerator = id.selectNodes("generator");
if ((idGenerator != null) && (idGenerator.size() > 0)) {
Element generator = (Element) idGenerator.get(0);
if (generator != null) {
table.setIdGenerator(generator
.attributeValue("class"));
}
}
}
}
// 表字段配置信息
List lPage = e.selectNodes("property");
for (int j = 0; j < lPage.size(); j++) {
Element el=(Element) lPage.get(j);
List columnList = (el.selectNodes("column"));
if ((columnList != null) && (columnList.size() > 0)) {
Element column = (Element) columnList.get(0);
if (column != null) {
DBField field = new DBField();
field.setName(column.attributeValue("name"));
try {
field.setLength(Integer.parseInt(column
.attributeValue("length")));
} catch (Exception ex) {
}
Class fieldType=com.easyjf.dbo.JavaTypeRegister.getInstance().findType(((Element) lPage.get(j))
.attributeValue("type"));
if(fieldType==null)fieldType=Class.forName(((Element) lPage.get(j))
.attributeValue("type"));
String lazy=el.attributeValue("lazy");
if(lazy!=null)field.setLazy(Boolean.parseBoolean(lazy));
field.setType(fieldType);
table.addField(((Element) lPage.get(j))
.attributeValue("name"), field); // 把属性名称作为key
// field.setLength(new
// Integer(column.attributeValue("length")).intValue());.
}
}
}
//下面解析one-to-one属性
List list= e.selectNodes("one-to-one");
if(list!=null && list.size()>0)
{
for(int j=0;j<list.size();j++)
{
Element el=(Element)list.get(j);
OneToOneField field=new OneToOneField();
field.setName(el.attributeValue("column"));
Class fieldType=com.easyjf.dbo.JavaTypeRegister.getInstance().findType(el.attributeValue("type"));
if(fieldType==null)fieldType=Class.forName(el.attributeValue("type"));
field.setType(fieldType);
field.setColumn(el.attributeValue("column"));
String lazy=el.attributeValue("lazy");
if(lazy!=null)field.setLazy(Boolean.parseBoolean(lazy));
field.setKey(el.attributeValue("key"));
field.setTableName(el.attributeValue("tableName"));
table.addClassField(el.attributeValue("name"), field);
}
}
//下面解析many-to-one属性
list= e.selectNodes("many-to-one");
if(list!=null && list.size()>0)
{
for(int j=0;j<list.size();j++)
{
Element el=(Element)list.get(j);
ManyToOneField field=new ManyToOneField();
field.setName(el.attributeValue("name"));
Class fieldType=com.easyjf.dbo.JavaTypeRegister.getInstance().findType(el.attributeValue("fieldType"));
if(fieldType==null)fieldType=Class.forName(el.attributeValue("fieldType"));
field.setFieldType(fieldType);
Class type=com.easyjf.dbo.JavaTypeRegister.getInstance().findType(el.attributeValue("type"));
if(type==null)type=Class.forName(el.attributeValue("type"));
field.setType(type);
field.setColumn(el.attributeValue("column"));
String lazy=el.attributeValue("lazy");
if(lazy!=null)field.setLazy(Boolean.parseBoolean(lazy));
field.setKey(el.attributeValue("key"));
field.setTableName(el.attributeValue("tableName"));
table.addClassField(el.attributeValue("name"), field);
}
}
//最后处理many-to-many
list= e.selectNodes("many-to-many");
if(list!=null && list.size()>0)
{
for(int j=0;j<list.size();j++)
{
Element el=(Element)list.get(j);
ManyToManyField field=new ManyToManyField();
field.setName(el.attributeValue("name"));
Class fieldType=com.easyjf.dbo.JavaTypeRegister.getInstance().findType(el.attributeValue("fieldType"));
if(fieldType==null)fieldType=Class.forName(el.attributeValue("fieldType"));
field.setFieldType(fieldType);
Class type=com.easyjf.dbo.JavaTypeRegister.getInstance().findType(el.attributeValue("type"));
if(type==null)type=Class.forName(el.attributeValue("type"));
field.setType(type);
field.setColumn(el.attributeValue("column"));
String lazy=el.attributeValue("lazy");
if(lazy!=null)field.setLazy(Boolean.parseBoolean(lazy));
field.setKey(el.attributeValue("key"));
field.setTableName(el.attributeValue("tableName"));
field.setTagColumn(el.attributeValue("tagColumn"));
field.setTagKey(el.attributeValue("tagKey"));
table.addClassField(el.attributeValue("name"), field);
}
}
// System.out.println(e.attributeValue("name")+" "+table);
tables.put(e.attributeValue("name"), table);
}
}
/**
* 使用Dom4J解析指定的xml配置文件
*
* @param fileName
* @return
* @throws DocumentException
*/
private Document parse(String fileName) throws DocumentException {
SAXReader reader = new SAXReader();
File file = new File(fileName);
Document document = file.exists() ? reader.read(fileName) : null;
return document;
}
// ~--- get methods --------------------------------------------------------
/**
* 工厂方法,使用
*/
public static XMLConfigFactory getInstance() throws EasyDBOException {
synchronized (XMLConfigFactory.class) {
InputStream in = null;
try {
logger.debug("从easyjf-dbo.xml文件初始化数据源!");
in=Thread.currentThread().getContextClassLoader().getResourceAsStream("/easyjf-dbo.xml");
if(in==null)in=XMLConfigFactory.class.getResourceAsStream("/easyjf-dbo.xml");
} catch (Exception e) {
logger.error("查找数据库配置文件文件错误!" + e);
}
if (in == null) {
throw new EasyDBOException("无法加载EasyDBO数据源配置文件!");
}
XMLConfigFactory config=new XMLConfigFactory(in);
return config;
}}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -