1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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);
        }
    }