weblogic 下解决文件批读取

本文介绍了一种在WebLogic环境下批量读取多个配置文件的方法,通过创建一个XML配置文件来引用其他配置文件,并使用DOM4J进行解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      自己写了些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;
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值