讀取war文件裏的jar裏的文件

問題是須要讀取war包裏的jar包裏的一個xml文件。故記錄下。java

package com.dbconfig.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

public class JndiProcess {
		public List findJndi(String warPath) throws IOException{
			List<String> jndi=new ArrayList<String>();
			JarFile jar = new JarFile(warPath);  			
	        Enumeration<JarEntry> entries = jar.entries();  
	        //遍歷條目。   
	        while (entries.hasMoreElements()) {     
	        	JarEntry jarentry= entries.nextElement();
	        	//System.out.println(jarentry.getName());
	        	if(jarentry.getName().startsWith("WEB-INF/lib/jbp-")&&jarentry.getName().endsWith(".jar")){
	        		JarInputStream jis=new JarInputStream(jar.getInputStream(jarentry));
	        		try {
	        			boolean flag=true;
	        			while(flag){
	        				JarEntry je=jis.getNextJarEntry();
	        				if(je==null){	        					
	        					break;
	        				}
	        				//正則匹配
	        				String regEx="^spring-(.*)-context.xml$";  
	        				Pattern pattern = Pattern.compile(regEx);
	        				Matcher matcher=pattern.matcher(je.getName());
	        				if(matcher.matches()){
	        					jndi.addAll(copyInputStream(jis,je.getName()));	        					
	        				}
	        				
	            		}
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						//若是沒有找到文件會報錯,忽略此錯誤
					}
	        		
	        	}
	        }
	        return jndi;
		}
		
		 public  List copyInputStream(InputStream in, String entryName) throws IOException, DocumentException {  
			 		List<String> jndiName=new ArrayList<String>();
		            ByteArrayOutputStream _copy = new ByteArrayOutputStream();  
		            int read = 0;  
		            int chunk = 0;  
		            byte[] data = new byte[256];  
		            while(-1 != (chunk = in.read(data)))  
		            {  
		                read += data.length;  
		                _copy.write(data, 0, chunk);  
		            } 

		           InputStream is=(InputStream)new ByteArrayInputStream(_copy.toByteArray());  
		           SAXReader reader = new SAXReader();
					Document   document = reader.read(is);
					XPath xPath = document.createXPath("//jee:jndi-lookup");    
					Map<String, String> names = new HashMap<String, String>();    
					names.put("jee", "http://www.springframework.org/schema/jee");     
					xPath.setNamespaceURIs(names);        
					List<Element> nodes=xPath.selectNodes(document);
					if(nodes!=null){
						for(Element node:nodes){
							jndiName.add(node.attributeValue("jndi-name"));
						}
					}	
					return jndiName;
		    }  
}
相關文章
相關標籤/搜索