反射机制调用protected保护方法加载jar

package com.web.app.listener.util;

import org.apache.catalina.loader.WebappClassLoader;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

import javax.management.loading.MLet;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;

import com.web.utils.FileUtil;
import com.web.utils.XmlReaderUtil;

/**
 * 启动装在额外jar包工具类,实例化后调用loadJar方法装在 需要配置好configfilepath配置文件, xml格式<config>
 * <jarFile></jarFile> //类路径 classpath:开头 跟“/a.jar” //lib路径 libpath:开头跟"/a.jar"
 * </config>
 * 
 * @author Administrator
 *
 */
public class LoadJarUtil {
	private Logger log = Logger.getLogger(this.getClass());
	private final String ROOT_ELEMENT_NAME = "config";
	private final String NODE_ELEMENT_NAME = "jarFile";
	private String configfilepath;

	public LoadJarUtil(String configpath) {
		this.configfilepath = configpath;
		log.info("Add Jar configfile path:" + configpath);
	}

	/**
	 * loadJar from config file {@configfilepath};
	 */
	public void loadJar() {
		File configfile = FileUtil.getFile(configfilepath);
		if (configfile == null || !configfile.exists() || !configfile.isFile()) {
			log.warn("Error to instantiate configfile object,configfile is wrong.");
			return;
		}
		ArrayList<File> loadfile = new ArrayList<File>();
		try {
			Document dom = XmlReaderUtil.readXmlFromFile(configfile);
			Element root = dom.getRootElement();
			if (ROOT_ELEMENT_NAME.equals(root.getName())) {
				List<Element> list = (List<Element>) root
						.elements(NODE_ELEMENT_NAME);
				if (list != null && list.size() > 0) {
					for (Element e : list) {
						String loadjar_str = e.getText();
						// System.out.println(loadjar_str);
						if (loadjar_str.indexOf(".jar") > 0) {
							File loadjar_fl = FileUtil.getFile(loadjar_str);
							if (loadjar_fl != null && loadjar_fl.exists()
									&& loadjar_fl.isFile()) {
								loadfile.add(loadjar_fl);
							}
						}
					}
				} else {
					log.warn("No jarFile to load.");
				}
			}
		} catch (DocumentException e1) {
			log.error("", e1);
		} catch (IOException e1) {
			log.error("", e1);
		}

		if (loadfile.size() == 0) {
			log.warn("LoadJar list is empty.Stop to load.");
			return;
		}

		ClassLoader cls = 
		 Thread.currentThread().getContextClassLoader();
		System.out.println(cls.getClass().getName());
		URLClassLoader cload = null;
		org.apache.catalina.loader.WebappClassLoader wcload = null;
		if("java.net.URLClassLoader".equals(cls.getClass().getName())){
			cload = (URLClassLoader)cls;
		}else if("org.apache.catalina.loader.WebappClassLoader".equals(cls.getClass().getName())){
			wcload = (WebappClassLoader)cls;
		}
		try {
			
			  if(cload!=null){//tomcat6 classloader
				  Class[] margs = new Class[]{URL.class}; 
				  Method method = cload.getClass().getDeclaredMethod("addURL", margs);
				  method.setAccessible(true); 
				  if(method!=null){ 
				  	for(File loadf:loadfile){ 
				  		URL url = loadf.toURI().toURL();
				 		method.invoke(cload,new Object[]{url}); 
				 		log.info("LoadJar:"+url.toString()); 
				  	} 
				  }
			  }else if(wcload!=null){//tomcat7 classloader
				  for(File loadf:loadfile){ 
			  		String fileurl = loadf.toURI().toURL().toString();
			  		wcload.addRepository(fileurl);
			 		log.info("LoadJar:"+fileurl);
				  } 
			  }
		} catch (SecurityException e) {
			log.error("", e);
		} catch (Exception e) {
			log.error("", e);
		}
		Runtime.getRuntime().gc();
	}

	public String getConfigfilepath() {
		return configfilepath;
	}

	public void setConfigfilepath(String configfilepath) {
		this.configfilepath = configfilepath;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值