反射机制调用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; } }
相关文章推荐
- 居于反射机制Java动态调用jar方法
- 使用反射机制实现对类中私有方法的调用
- Java的反射机制---动态调用对象的简单方法
- C# aspx页面动态加载ascx用户控件 及 利用反射调用其内方法
- 反射机制进行动态调用的两种创建方法实例与hama中的反射机制
- 如何使用Java的反射机制以及调用构造方法?
- java类加载机制及方法调用
- 反射实现方法调用(1):执行机制
- 论java之反射机制,访问类中私有变量,调用私有方法
- 反射机制调用普通成员方法
- 使用反射机制调用service层方法Autowired标签自动注入失败
- java中由类名和方法名字符串实现其调用【反射机制】
- C#实现反射调用动态加载的DLL文件中的方法
- C# aspx页面动态加载ascx用户控件 及 利用反射调用其内方法
- Java方法的动态调用(采用反射机制)
- Java加载jar文件并调用jar文件当中有参数和返回值的方法
- 通过反射机制调用某个类的方法
- java 反射机制的几个调用方法
- C#的反射机制调用方法 推荐
- 消息函数一般是私有的,因为不需要程序员显示的调用,但子类如果需要改写这个方法,则改成保护方法Protected