package com.vci.common.utility;
|
|
import java.io.File;
|
import java.lang.reflect.Method;
|
import java.net.URL;
|
import java.net.URLClassLoader;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import com.vci.common.log.ServerWithLog4j;
|
import com.vci.common.resource.CommonProperties;
|
|
/**
|
* 加载用户自定义jar包
|
* @author liudi
|
*
|
* 2013-2-25
|
*/
|
public final class ClassLoaderUtil {
|
/** URLClassLoader的addURL方法 */
|
public static Method addURL = initAddMethod();
|
|
/** 初始化方法 */
|
public static final Method initAddMethod() {
|
try {
|
Method add = URLClassLoader.class
|
.getDeclaredMethod("addURL", new Class[] { URL.class });
|
add.setAccessible(true);
|
return add;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
public static URLClassLoader system = (URLClassLoader) ClassLoader.getSystemClassLoader();
|
|
/**
|
* 循环遍历目录,找出所有的JAR包
|
*/
|
private static final void loopFiles(File file, List<File> files) {
|
if (file.isDirectory()) {
|
File[] tmps = file.listFiles();
|
for (File tmp : tmps) {
|
loopFiles(tmp, files);
|
}
|
} else {
|
if (file.getAbsolutePath().endsWith(".jar") || file.getAbsolutePath().endsWith(".zip")) {
|
files.add(file);
|
}
|
}
|
}
|
|
/**
|
* <pre>
|
* 加载JAR文件
|
* </pre>
|
*
|
* @param file
|
*/
|
public static final void loadJarFile(File file) {
|
try {
|
addURL.invoke(system, new Object[] { file.toURI().toURL() });
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
* <pre>
|
* 从一个目录加载所有JAR文件
|
* </pre>
|
*
|
* @param path
|
*/
|
public void loadJarPath(String path) {
|
List<File> files = new ArrayList<File>();
|
File lib = new File(path);
|
loopFiles(lib, files);
|
for (File file : files) {
|
loadJarFile(file);
|
}
|
}
|
|
public List<File> loadJar(String path) {
|
List<File> files = new ArrayList<File>();
|
File lib = new File(path);
|
loopFiles(lib, files);
|
return files;
|
}
|
|
public List<File> getNeedLoadJars(){
|
List<File> files = new ArrayList<File>();
|
List<String> paths = getNeedLoadJarPaths();
|
for (String path : paths) {
|
if("".equals(path)){
|
continue;
|
}
|
if(!new File(path).exists()){
|
continue;
|
}
|
File lib = new File(path);
|
loopFiles(lib, files);
|
}
|
return files;
|
}
|
|
/**
|
* 返回需要自动加载.jar所在的路径
|
* @return
|
*/
|
public List<String> getNeedLoadJarPaths(){
|
String basePath = System.getProperty("user.dir") + File.separator;
|
List<String> paths = new ArrayList<String>();
|
String[] jarPaths = CommonProperties.getStringProperty("jarPath").split(",");
|
for (String jarPath : jarPaths) {
|
if("".equals(jarPath.trim())) {
|
continue;
|
}
|
paths.add(jarPath);
|
}
|
paths.add(basePath + "lib");
|
paths.add(basePath + "modules");
|
paths.add(basePath + "libs");
|
paths.add(basePath + "projects");
|
return paths;
|
}
|
|
/**
|
* 执行加载需要自动加载的.jar
|
*/
|
public void loadNeedLoadJars(){
|
System.getProperties().list(System.out);
|
List<String> paths = getNeedLoadJarPaths();
|
for (String path : paths) {
|
if("".equals(path)){
|
continue;
|
}
|
if(!new File(path).exists()){
|
continue;
|
}
|
loadJarPath(path);
|
ServerWithLog4j.logger.info("loadJarPath:" + path);
|
}
|
}
|
}
|