📄 xmlconfigfactory.java
字号:
package com.easyjf.dbo.config;
import org.apache.log4j.Logger;
import org.dom4j.*;
import org.dom4j.io.*;
import com.easyjf.dbo.DBField;
import com.easyjf.dbo.DBMapping;
import com.easyjf.dbo.DBTable;
import com.easyjf.dbo.DataSourceManager;
import com.easyjf.dbo.EasyDBOException;
import java.io.File;
import java.io.InputStream;
import java.util.*;
import javax.sql.DataSource;
public class XMLConfigFactory implements IConfigFactory {
private Document doc;
private static final Logger logger = (Logger) Logger.getLogger(IConfigFactory.class.getName());
public XMLConfigFactory()
{
this("");
}
public XMLConfigFactory(String fileName)
{
try{
doc=parse(fileName);
}
catch(Exception e)
{
logger.error(fileName+"配置文件错误!"+e);
}
}
public XMLConfigFactory(InputStream in)
{
try{
SAXReader reader = new SAXReader();
doc=reader.read(in);
}
catch(Exception e)
{
logger.warn("配置文件错误!"+e);
}
}
public static XMLConfigFactory getInstance()throws EasyDBOException
{
synchronized (XMLConfigFactory.class){
InputStream in=null;
try{
logger.debug("从easyjf-dbo.xml文件初始化数据源!");
in=XMLConfigFactory.class.getResourceAsStream("/easyjf-dbo.xml");
}
catch(Exception e)
{
logger.warn("查找数据库配置文件文件错误!"+e);
}
if(in==null)throw new EasyDBOException("无法加载EasyDBO数据源配置文件!");
return new XMLConfigFactory(in);
}
}
public void init(DBOConfig dboconfig) throws EasyDBOException{
initDataSource(dboconfig);
initTables(dboconfig.getTables());
}
private void initDataSource(DBOConfig dboconfig) throws EasyDBOException{
if(doc==null)return;
DBMapping dbMapping =DBMapping.getInstance();
if(!dbMapping.getMap().isEmpty())dbMapping.getMap().clear();
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);
String dialect=(String)property.get(IConfigFactory.DIALECT);
if("true".equals(show_sql))dboconfig.setShow_sql(true);
if("true".equals(optimize))dboconfig.setOptimize(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.getInstance().addDialect(name,dialect);
if(i==0)//把第一个数据源设置成默认链接
{
DBOConfig.getInstance().setDataSource(dsource);
DBOConfig.getInstance().setDialect(dialect);
}
}
}
}
}
}
private void initTables(Map tables) {
if(doc==null)return;
if(!tables.isEmpty())tables.clear();
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"));
//表主键配置信息
List idList=e.selectNodes("id");
if(idList!=null && (idList.size()>0))
{
Element id=(Element)idList.get(0);
//System.out.println(id.asXML());
if(id!=null)
{
List idColumn=id.selectNodes("column");
if(idColumn!=null&& (idColumn.size()>0))
{
Element column=(Element)idColumn.get(0);
//System.out.println(column.asXML());
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)
{
}
field.setType(id.attributeValue("type"));
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);
//System.out.println(generator.asXML());
if(generator!=null){
table.setIdGenerator(generator.attributeValue("class"));
//System.out.println(table.getIdGenerator());
}
}
}
}
//表字段配置信息
List lPage=e.selectNodes("property");
for(int j=0;j<lPage.size();j++)
{
List columnList=((Element)lPage.get(j)).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)
{
}
field.setType(((Element)lPage.get(j)).attributeValue("type"));
table.addField(((Element)lPage.get(j)).attributeValue("name"),field);//把属性名称作为key
//field.setLength(new Integer(column.attributeValue("length")).intValue());.
}
}
}
//System.out.println(e.attributeValue("name")+" "+table);
tables.put(e.attributeValue("name"),table);
}
}
public Map initOther()
{
if(doc==null)return null;
Node node=doc.selectSingleNode("/easyjf-web/frame-setting/template-base");
Map result=new HashMap();
if(node!=null)result.put("TemplateBasePath",node.getText());
List appList=doc.selectNodes("/easyjf-web/frame-setting/init-app/app-class");
List list=new ArrayList();
if(appList!=null)
{
for(int i=0;i<appList.size();i++)
{
list.add(((Node)appList.get(i)).getText());
}
}
result.put("initApp",list);
return result;
}
public 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;
}
public static void main(String[] args) {
//XMLConfigFactory.getInstance().init(DBOConfig.getInstance());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -