weblogic 下解决文件批读取

阅读更多

      自己写了些sql 脚本的配置文件,以前在tomcat下可以从根目录获得所有这些配置文件,并在服务器启动时进行解析放到缓存。但在weblogic下就不行了!我在网上找过各种weblogic获得文件绝对路径的方案,但都是针对于单个文件读取的方案,不能解决批读取。于是想到了如下解决方案:

      写一个xml配置文件来引用所有的之前的配置文件,并写一个类对用dom4j对这个配置文件进行解析,依次读取xml中引入的所有配置文件。

    无代码无真相:

     用这个xml文件来引入其它配置文件:

    



	
		
			infoTransfer.xml
			module.xml
			orgnization.xml
			permission.xml
			role.xml
			user.xml
		
		
			graph.xml
		
		
			sbccdwgl.xml
		
	

 然后这样进行解析:

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 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 filepaths = new ArrayList();
	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 getFilepaths() {
		return filepaths;
	}

	public void setFilepaths(List filepaths) {
		this.filepaths = filepaths;
	}

}

 

你可能感兴趣的:(Weblogic,XML,Tomcat,SQL,脚本)