`
uule
  • 浏览: 6306602 次
  • 性别: Icon_minigender_1
  • 来自: 一片神奇的土地
社区版块
存档分类
最新评论

XML缓存工具类WebserviceXml

 
阅读更多

实现每次代理时,将服务对应的XML Node数据webservice节点写入webservices.xml中。

具体方法是将整个XML提取为一个XML操作工具类WebserviceXml,根节点下的每个子节点抽象为NODE对象WebserviceNode,缓存WebserviceInfoCache操作XML类,XML类中操作NODE元素对象。

 

webservices.xml:

<?xml version="1.0" encoding="UTF-8"?>

<webservices xmlns="http://ws.test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="webservices.xsd">  
  <webservice wsname="ws1" wsversion=""> 
    <wsdlurl>http://localhost:8888/ws1?wsdl</wsdlurl> 
  </webservice>  
  <webservice wsname="ws2"> 
    <wsdlurl>http://localhost:8888/ws2?wsdl</wsdlurl> 
  </webservice>  
  <webservice wsname="ws3" wsversion="v1"> 
    <wsdlurl>http://localhost:8888/ws3?wsdl</wsdlurl> 
  </webservice>  
  <webservice wsname="srv4"> 
    <wsdlurl>http://localhost:8889/ws4?wsdl</wsdlurl> 
  </webservice>  
  <webservice wsname="ESB_YS_YS_InquiryMachineInfoSrv">
    <wsdlurl>http://10.204.104.87:8888/ESB_YS_YS_InquiryMachineInfoSrv/ESBYSYSInquiryMachineInfoSrv?wsdl</wsdlurl>
  </webservice>
</webservices>

 webservices.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://ws.test.com" 
	targetNamespace="http://ws.test.com" 
	xmlns:xs="http://www.w3.org/2001/XMLSchema" 
	elementFormDefault="qualified" 
	attributeFormDefault="unqualified">
	<!-- 简易元素的定义 -->
	<xs:element name="wsdlurl" type="xs:string"/>
	
	<!-- 复合元素的定义 -->
	<xs:element name="webservice">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="wsdlurl"/>
			</xs:sequence>
			<xs:attribute name="wsname" type="xs:string" use="required"/>
			<xs:attribute name="wsversion" type="xs:string"/>
		</xs:complexType>
	</xs:element>
	<xs:element name="webservices">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="webservice" maxOccurs="unbounded"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

 这两个文件放在JBOSS的D:\jboss-5.1.0.GA\server\default\conf\wsconfig路径下,wsconfig为自增文件夹。

 

WebserviceNode节点MODEL类:

/**
 * 对应webservices.xml中的webservice节点
 * 
 *
 */
public class WebserviceNode {

	public WebserviceNode() {
		super();
	}

	public WebserviceNode(String wsName, String wsVersion, String wsdlUrl) {
		super();
		this.wsName = wsName;
		this.wsVersion = wsVersion;
		this.wsdlUrl = wsdlUrl;
	}

	private String wsName;
	
	private String wsVersion;
	
	private String wsdlUrl;
	
	
	public boolean hasVersionInfo(){
		return StringUtil.isNotEmpty(getWsVersion());
	}
	
	public String getWsName() {
		return wsName;
	}

	public void setWsName(String wsName) {
		if(wsName != null){
			this.wsName = wsName.trim();
		}
	}

	public String getWsVersion() {
		return wsVersion;
	}

	public void setWsVersion(String wsVersion) {
		if(wsVersion != null){
			this.wsVersion = wsVersion.trim();
		}
	}

	public String getWsdlUrl() {
		return wsdlUrl;
	}

	public void setWsdlUrl(String wsdlUrl) {
		if(wsdlUrl != null){
			this.wsdlUrl = wsdlUrl;
		}
	}

	@Override
	public String toString() {
		return "WebserviceInfo [wsName=" + wsName + (hasVersionInfo() ? ", wsVersion=" + wsVersion : "")
				+ ", wsdlUrl=" + wsdlUrl + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((wsName == null) ? 0 : wsName.hashCode());
		result = prime * result
				+ ((wsVersion == null) ? 0 : wsVersion.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		WebserviceNode other = (WebserviceNode) obj;
		if (wsName == null) {
			if (other.wsName != null)
				return false;
		} else if (!wsName.equals(other.wsName))
			return false;
		if (wsVersion == null) {
			if (other.wsVersion != null)
				return false;
		} else if (!wsVersion.equals(other.wsVersion))
			return false;
		return true;
	}
	
	
}

 

 

WebserviceXml类:

(包含Main测试方法)

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * 作为webservices.xml文件的抽象类
 * schema 参考webservices.xsd
 */
public class WebserviceXml {

	private final static String PATH;
	
	static{
		PATH = System.getProperty("jboss.server.home.dir") + "/conf/wsconfig/webservices.xml";
		
	
	}
	
	
	private Document doc = null;
	
	private List<WebserviceNode> wis = new ArrayList<WebserviceNode>();
	
	public WebserviceXml() {
		super();
		load();
	}
	
	/**
	 * 从硬盘加载
	 */
	@SuppressWarnings("unchecked")
	private void load(){
		// 从xml中加载数据		
		SAXReader sr = new SAXReader();
		File xml = new File(PATH);
		try {
			if(xml.exists() && xml.isFile()){
				doc = sr.read(xml);
			}else{
				System.err.println("'" + PATH + "' is Not Exists...");
			}
		} catch (DocumentException e) {
			System.err.println("Load '" + PATH + "' is Failed...");
			e.printStackTrace();
		}
		if(doc != null){
			Element root = doc.getRootElement();
			List<Element> elementList = root.elements();
	        for (Element e : elementList) { 
	        	WebserviceNode wi = new WebserviceNode();
	        	wi.setWsName(e.attributeValue("wsname"));
	        	wi.setWsVersion(e.attributeValue("wsversion"));
	        	wi.setWsdlUrl(e.elementText("wsdlurl"));
	        	
	        	wis.add(wi);
	        }
		}
	}
	
	/**
	 * 添加或更新多个webservice子节点
	 * @param wi 封装的服务信息们
	 */
	public synchronized void addOrUpdate(List<WebserviceNode> addWis){
		if(doc != null){
			Element root = doc.getRootElement();
			
			for(WebserviceNode wi : addWis){
				addOrUpdateWebservice(wi, root);
			}
			
			save();
		}
	}

	/**
	 * 添加或更新单个webservice子节点
	 * @param wi 封装的服务信息
	 */
	public synchronized void addOrUpdate(WebserviceNode wi) {
		if(doc != null){
			Element root = doc.getRootElement();
			
			addOrUpdateWebservice(wi, root);
			
			save();
		}
	}

	/**
	 * 在指定的节点上添加webservice子节点
	 * 
	 * @param wi 封装的服务信息
	 * @param root root节点
	 */
	private void addOrUpdateWebservice(WebserviceNode wi, Element root) {
		removeWebservices(wi, root);
		
		addOrUpdateWebserviceElement(wi, root);
		
		wis.add(wi);
	}

	/**
	 * 查找匹配的webservice元素
	 * 
	 * @param wi
	 * @param root
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<Element> findWebserviceElements(WebserviceNode wi, Element root) {
		Map<String, String> ns = new HashMap<String, String>();
		ns.put("vis", "http://ws.vispractice.com");
		
		String xpath = "/vis:webservices/vis:webservice[@wsname='"+ wi.getWsName() + "'" + (wi.hasVersionInfo() ? " and @wsversion='" + wi.getWsVersion() + "'" : " and (not(@wsversion) or normalize-space(@wsversion)='')") + "]";
		XPath x = root.createXPath(xpath);
		x.setNamespaceURIs(ns);
		
		//System.out.println(xpath);
		
		List<Element> es = x.selectNodes(root);
		return es;
	}

	/**
	 * 在指定的节点上添加webservice子节点(xml document)
	 * 
	 * @param wi
	 * @param root
	 */
	private void addOrUpdateWebserviceElement(WebserviceNode wi, Element root) {
		Element ws = root.addElement("webservice");
		ws.addAttribute("wsname", wi.getWsName());
		if(wi.hasVersionInfo()){
			ws.addAttribute("wsversion", wi.getWsVersion());
		}
		ws.addElement("wsdlurl").setText(wi.getWsdlUrl());
	}
	
	/**
	 * 保存至硬盘
	 */
	private void save() {
		// 将document保存至硬盘
		OutputFormat format = OutputFormat.createPrettyPrint();
		try {
			XMLWriter writer = new XMLWriter(new FileWriter(PATH), format);
			writer.write(doc);
			writer.close();
		} catch (IOException e) {
			System.err.println("Persist '" + PATH + "' is Failed...");
			e.printStackTrace();
		}
	}
	
	/**
	 * 返回webservice子节点清单
	 * @return
	 */
	public List<WebserviceNode> get(){
		
		return wis;
	}

	/**
	 * 删除指定的webservice节点
	 * 
	 * @param wis
	 */
	public void remove(List<WebserviceNode> rmWis){
		// 删除指定的webservice节点
		Element root = doc.getRootElement();
		for(WebserviceNode wi : rmWis){
			removeWebservices(wi, root);
		}
		
		save();
	}
	
	private void removeWebservices(WebserviceNode wi, Element root) {
		List<Element> es = findWebserviceElements(wi, root);
		
		if(es.size() > 0){
			// 删除doc中的元素
			for(Element e : es){
				root.remove(e);
			}
			// 删除集合中的元素
			Iterator<WebserviceNode> wiIterator = wis.iterator();
			while(wiIterator.hasNext()){  
				WebserviceNode i = wiIterator.next();  
				if(i.equals(wi)){
					wiIterator.remove();
				}
			}  
		}
	}
	
	/**
	 * 测试使用,注意把path修改成本地路径
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		WebserviceXml wx = new WebserviceXml();
		for(WebserviceNode wi : wx.get()){
			System.out.println(wi);
		}
		
		/* == add test == 
		WebserviceNode wi = new WebserviceNode();
		wi.setWsName(" srv5 ");
		wi.setWsVersion("v3.2");
		wi.setWsdlUrl("http://localhost:8888/ws4?wsdl");
		WebserviceNode wi2 = new WebserviceNode("srv4", null, "http://localhost:8889/ws4?wsdl");
		
		List<WebserviceNode> twis = new ArrayList<WebserviceNode>();
		twis.add(wi);
		twis.add(wi2);
		
		wx.addOrUpdate(twis);
		*/
		
		/* == remove test == */
		WebserviceNode wi3 = new WebserviceNode();
		wi3.setWsName(" srv5 ");
		wi3.setWsVersion("v3.2");
		List<WebserviceNode> twis2 = new ArrayList<WebserviceNode>();
		twis2.add(wi3);
		wx.remove(twis2);
		
	}
	
}

  

缓存类WebserviceInfoCache:

 

import java.util.HashMap;
import java.util.List;

/**
 * 服务信息缓存
 */
public class WebserviceInfoCache {

	/**
	 * 存储WebserviceInfo信息
	 * key:目前为wsname,启用wsversion后则使用wsname+"_"+wsversion
	 * value:WebserviceInfo对象
	 */
	private static HashMap<String, WebserviceNode> cache = new HashMap<String, WebserviceNode>();
	
	private static WebserviceXml wx;
	
	/**
	 * 调用WebserviceXml填充cache属性
	 */
	static{
		load();
	}
	
	private static void load(){
		// 调用WebserviceXml填充cache属性
		wx = new WebserviceXml();
		List<WebserviceNode> wis = wx.get();
		for(WebserviceNode wi : wis){
			cache.put(buildKey(wi), wi);
		}
	}
	
	/**
	 * 根据封装的服务信息创建key
	 * @param wi
	 * @return
	 */
	private static String buildKey(WebserviceNode wi) {
		StringBuilder key = new StringBuilder();
		if(wi != null && wi.getWsName() != null){
			key.append(wi.getWsName());
			if(wi.hasVersionInfo()){
				key.append("_").append(wi.getWsVersion().trim());
			}
		}
		return key.toString();
	}

	/**
	 * 通过wsname或wsname+"_"+wsversion获取wsdlurl信息
	 * @param wi 封装的服务信息
	 * @return wsdlurl信息,如果没有查找到缓存的信息,则返回null
	 */
	public static String getWebserviceWsdlUrl(WebserviceNode wi){
		WebserviceNode cc = cache.get(buildKey(wi));
		if(cc != null){
			return cc.getWsdlUrl();
		}
		return null;
	}
	
	/**
	 * 刷新缓存
	 */
	public static void refresh(){
		wx = null;
		cache = new HashMap<String, WebserviceNode>();
		load();
	}

	/**
	 * 添加或更新单个wsdlurl
	 * @param wi 封装的服务信息
	 */
	public static void addOrUpdateWebserviceInfo(WebserviceNode wi){
		// 添加单个wsdlurl
		cache.put(buildKey(wi), wi);
		wx.addOrUpdate(wi);
	}
	
	/**
	 * 添加或更新多个wsdlurl
	 * @param addWis 封装的服务信息们
	 */
	public static void addOrUpdateWebserviceInfo(List<WebserviceNode> addWis){
		// 添加多个wsdlurl
		for(WebserviceNode wi : addWis){
			cache.put(buildKey(wi), wi);
		}
		wx.addOrUpdate(addWis);
	}
	
	public static void removeWebserviceInfo(List<WebserviceNode> rmWis){
		wx.remove(rmWis);
	}
}

 

 。。。 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics