All jar package under specified JAVA scan path, and save all fixed type implements interface

 Private  static the Map <String, Object> loadAllJarFromAbsolute (String directoryPath) throws 
            a NoSuchMethodException, a InvocationTargetException, IllegalAccessException, IOException { 

        File Directory = new new File (directoryPath);
         // determine whether the folder, if the file directly with a single analytical method jar to resolve 
        IF (! directory.isDirectory ()) {
             // add the jar scan path 
            addURL (Directory);
             return loadJarFromAbsolute (directoryPath); 
        } 
        // if the folder is the current folder needs to cyclic loading all of the following jar 
        the Map < String, Object> = clazzMap new new the HashMap <> (16 );
        File[] jars = directory.listFiles();
        if (jars != null && jars.length > 0) {
            List<String> jarPath = new LinkedList<>();
            for (File file : jars) {
                String fPath = file.getPath();
                // 只加载jar
                if (fPath.endsWith(".jar")) {
                    addUrl(file);
                    jarPath.add(fPath);
                }
            }
            if (jarPath.size() > 0) {
                for (String path : jarPath) {
                    clazzMap.putAll(loadJarFromAbsolute(path));
                }
            }
        }
        return clazzMap;
    }

    private static Map<String, Object> loadJarFromAbsolute(String path) throws IOException {
        JarFile jar = new JarFile(path);
        Enumeration<JarEntry> entryEnumeration = jar.entries();
        Map<String, Object> clazzMap = new HashMap<>(32);
        while (entryEnumeration.hasMoreElements()) {
            JarEntry entry = EntryEnumeration.nextElement ();
             // first get the name of the class, do the treatment after qualifying, avoid dealing with ineligible class 
            String clazzName = entry.getName ();
             IF (clazzName.endsWith ( "class." )) {
                 // remove the file name suffix 
                clazzName clazzName.substring = (0, clazzName.length () -. 6 );
                 // replace delimiter 
                clazzName = clazzName.replace ( "/", "." );
                 // load the class, If it fails to skip 
                the try { 
                    Class clazz = <?> the Class.forName (clazzName); 
                    Class superClass =clazz.getSuperclass ();
                     IF (superClass.equals (ComponentExtend. class )) { 
                        Object O = clazz.newInstance (); 
                        Method, fieldExtendComponentId = clazz.getMethod ( "extendComponentId" ); 
                        String ID = (String) fieldExtendComponentId.invoke (O ); 
                        clazzMap.put (ID, O); 
                    } 
                } the catch (the Throwable E) {
                     // this can occur is dependent on some type of failure, skip, without processing, the processing can be done while 
                } 
            } 
        } 
        return clazzMap;
    }

    private static void addUrl(File jarPath) throws NoSuchMethodException, InvocationTargetException,
            IllegalAccessException, MalformedURLException {

        URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        URL url = jarPath.toURI().toURL();
        // the current path is added to the jar class loader path to be scanned 
        Method.invoke (classLoader, URL); 
    }

 

Guess you like

Origin www.cnblogs.com/zzq-include/p/12061113.html