wuxw
2019-04-01 fa74ecf22c36853d9097beda3dbb99482cfcb430
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
package com.java110.web.core;
 
import org.springframework.util.StringUtils;
 
import java.io.*;
import java.net.JarURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
 
/**
 * Created by wuxw on 2019/3/18.
 */
 
public abstract class PackageScanner {
 
    public void packageScanner(Class<?> klass,String suffix) {
        packageScanner(klass.getPackage().getName(), suffix);
    }
 
    public void packageScanner(String packageName,String suffix) {
        String packagePath = packageName.replace(".", File.separator);
 
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        try {
            Enumeration<URL> resources = classLoader.getResources(packagePath);
            while (resources.hasMoreElements()) {
                URL url = resources.nextElement();
                if (url.getProtocol().equals("jar")) {
                    scanPackage(url,suffix);
                } else {
                    File file = new File(url.toURI());
                    if (!file.exists()) {
                        continue;
                    }
                    scanPackage(packageName, file,suffix);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
 
    private void scanPackage(URL url,String suffix) throws IOException {
        JarURLConnection jarUrlConnection =  (JarURLConnection) url.openConnection();
        JarFile jarFile = jarUrlConnection.getJarFile();
        Enumeration<JarEntry> jarEntries = jarFile.entries();
 
        while (jarEntries.hasMoreElements()) {
            JarEntry jarEntry = jarEntries.nextElement();
            String jarName = jarEntry.getName();
            if (jarEntry.isDirectory() || !jarName.endsWith(suffix)) {
                continue;
            }
            String className = jarName.replace(suffix, "");
            handleResource(className);
        }
    }
 
    private void scanPackage(String packageName, final File currentfile,final String suffix) {
        File[] files = currentfile.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if (currentfile.isDirectory()) {
                    return true;
                }
                return pathname.getName().endsWith(suffix);
            }
        });
        for (File file : files) {
            if (file.isDirectory()) {
                scanPackage(packageName + "." + file.getName(), file,suffix);
            } else {
                packageName = packageName.replace(".",File.separator);
                String fileName = packageName + File.separator +  file.getName();
                if(StringUtils.isEmpty(fileName) || !fileName.endsWith(suffix)){
                    continue;
                }
                handleResource(fileName);
            }
        }
 
    }
 
    protected abstract void handleResource(String filePath);
 
    public static void main(String[] args) {
        new PackageScanner() {
            @Override
            protected void handleResource(String filePath) {
 
            }
        }.packageScanner("components","js");
    }
 
}