自己写了些sql 脚本的配置文件,以前在tomcat下可以从根目录获得所有这些配置文件,并在服务器启动时进行解析放到缓存。但在weblogic下就不行了!我在网上找过各种weblogic获得文件绝对路径的方案,但都是针对于单个文件读取的方案,不能解决批读取。于是想到了如下解决方案:
写一个xml配置文件来引用所有的之前的配置文件,并写一个类对用dom4j对这个配置文件进行解析,依次读取xml中引入的所有配置文件。
无代码无真相:
用这个xml文件来引入其它配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<dbscript-files>
<dbscript-location value="/com/sinosoft/emergency/dbscripts/">
<list path="authorization">
<value>infoTransfer.xml</value>
<value>module.xml</value>
<value>orgnization.xml</value>
<value>permission.xml</value>
<value>role.xml</value>
<value>user.xml</value>
</list>
<list path="graph">
<value>graph.xml</value>
</list>
<list path="sbgl/sbgl001_sbccdwgl">
<value>sbccdwgl.xml</value>
</list>
</dbscript-location>
</dbscript-files>
然后这样进行解析:
package com.sinosoft.emergency.parser.core;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.sinosoft.emergency.exception.core.ParserException;
public class DBScriptsConfigParser {
public static List<String> parser(InputStream is) throws ParserException{
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(is);
Element root = doc.getRootElement();
DBScriptsConfigVisitor visitor = new DBScriptsConfigVisitor();
root.accept(visitor);
return visitor.getFilepaths();
} catch (DocumentException e) {
throw new ParserException("parser dbscript config exception.", e);
}
}
}
package com.sinosoft.emergency.parser.core;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.VisitorSupport;
public class DBScriptsConfigVisitor extends VisitorSupport {
private List<String> filepaths = new ArrayList<String>();
private String location = "";
private String listPath = "";
@Override
public void visit(Element element) {
String name = element.getName();
if(name.equals("dbscript-location")) {
location = "";
Attribute valueAttr = element.attribute("value");
if(valueAttr == null) {
assert false : "node [dbscript-location] must has [value] attribute.";
}
location = valueAttr.getValue().trim();
}
if(name.equals("list")) {
Attribute pathAttr = element.attribute("path");
listPath = "";
if(pathAttr != null) {
String pathValue = pathAttr.getValue().trim();
if(!location.endsWith("/") && !pathValue.startsWith("/")) {
listPath = "/" + pathValue;
} else {
listPath = pathValue;
}
}
}
if(name.equals("value")){
String valueText = element.getText();
if(!(location+listPath).trim().endsWith("/") && !valueText.startsWith("/")) {
valueText = "/" + valueText.trim();
}
filepaths.add(location + listPath + valueText);
}
if(name.equals("dbscript-file")){
String fileText = element.getText();
filepaths.add(fileText);
}
}
public List<String> getFilepaths() {
return filepaths;
}
public void setFilepaths(List<String> filepaths) {
this.filepaths = filepaths;
}
}