您的位置:首页 > 其它

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

2017-12-06 08:36 363 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: