jetbrick-commons/src/main/java/jetbrick/beans/ClasspathUtils.java
public static Collection<URL> getClasspathURLs(final ClassLoader classLoader) {
        Collection<URL> urls = new LinkedHashSet<URL>(32);
        ClassLoader loader = classLoader;
        while (loader != null) {
            String className = loader.getClass().getName();
            if (EXT_CLASS_LOADER_NAME.equals(className)) {
                break;
            }
            if (loader instanceof URLClassLoader) {
                for (URL url : ((URLClassLoader) loader).getURLs()) {
                    urls.add(url);
                }
            } else if (className.startsWith("weblogic.utils.classloaders.")) {
                // 该死的 WebLogic，只能特殊处理
                // GenericClassLoader, FilteringClassLoader, ChangeAwareClassLoader
                try {
                    Method method = loader.getClass().getMethod("getClassPath");
                    Object result = method.invoke(loader);
                    if (result != null) {
                        String[] paths = StringUtils.split(result.toString(), File.pathSeparatorChar);
                        for (String path : paths) {
                            urls.add(URLUtils.fromFile(path));
                        }
                    }
                } catch (NoSuchMethodException e) {
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } else if (className.startsWith("org.jboss.modules.ModuleClassLoader")) {
                // 该死的 Jboss/Wildfly 8，只能特殊处理
                try {
                    Set<URL> urlSet = JBossModuleUtils.getClasspathURLs(loader, false);
                    urls.addAll(urlSet);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            loader = loader.getParent();
        }

        // moved jsp classpath from ServletContext.attributes to System.properties
        String jsp_classpath = System.getProperty("org.apache.catalina.jsp_classpath");
        String classpath = System.getProperty("java.class.path");
        classpath = StringUtils.trimToEmpty(classpath) + File.pathSeparatorChar + StringUtils.trimToEmpty(jsp_classpath);
        if (classpath.length() > 1) {
            String[] paths = StringUtils.split(classpath, File.pathSeparatorChar);
            for (String path : paths) {
                path = path.trim();
                if (path.length() > 0) {
                    URL url = URLUtils.fromFile(path);
                    urls.add(url);
                }
            }
        }

        // 添加包含所有的 META-INF/MANIFEST.MF 的 jar 文件
        try {
            Enumeration<URL> paths = classLoader.getResources("META-INF/MANIFEST.MF");
            while (paths.hasMoreElements()) {
                URL url = paths.nextElement();
                File file = URLUtils.toFileObject(url);
                urls.add(file.toURI().toURL());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 删除 jdk 自带的 jar
        Iterator<URL> it = urls.iterator();
        while (it.hasNext()) {
            String path = it.next().getPath();
            if (path.contains("/jre/lib/")) {
                it.remove();
            }
        }

        return urls;
    }
public static Collection<URL> getClasspathURLs(final ClassLoader classLoader) {
        Collection<URL> urls = new LinkedHashSet<URL>(32);
        ClassLoader loader = classLoader;
        while (loader != null) {
            String className = loader.getClass().getName();
            if (EXT_CLASS_LOADER_NAME.equals(className)) {
                break;
            }
            if (loader instanceof URLClassLoader) {
                for (URL url : ((URLClassLoader) loader).getURLs()) {
                    urls.add(url);
                }
            } else if (className.startsWith("weblogic.utils.classloaders.")) {
                // 该死的 WebLogic，只能特殊处理
                // GenericClassLoader, FilteringClassLoader, ChangeAwareClassLoader
                try {
                    Method method = loader.getClass().getMethod("getClassPath");
                    Object result = method.invoke(loader);
                    if (result != null) {
                        String[] paths = StringUtils.split(result.toString(), File.pathSeparatorChar);
                        for (String path : paths) {
                            urls.add(URLUtils.fromFile(path));
                        }
                    }
                } catch (NoSuchMethodException e) {
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } else if (className.startsWith("org.jboss.modules.ModuleClassLoader")) {
                // 该死的 Jboss/Wildfly 8，只能特殊处理
                try {
                    Set<URL> urlSet = JBossModuleUtils.getClasspathURLs(loader, false);
                    urls.addAll(urlSet);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            loader = loader.getParent();
        }

        // moved jsp classpath from ServletContext.attributes to System.properties
        String jsp_classpath = System.getProperty("org.apache.catalina.jsp_classpath");
        String classpath = System.getProperty("java.class.path");
        classpath = StringUtils.trimToEmpty(classpath) + File.pathSeparatorChar + StringUtils.trimToEmpty(jsp_classpath);
        if (classpath.length() > 1) {
            String[] paths = StringUtils.split(classpath, File.pathSeparatorChar);
            for (String path : paths) {
                path = path.trim();
                if (path.length() > 0) {
                    URL url = URLUtils.fromFile(path);
                    urls.add(url);
                }
            }
        }

        // 添加包含所有的 META-INF/MANIFEST.MF 的 jar 文件
        try {
            Enumeration<URL> paths = classLoader.getResources("META-INF/MANIFEST.MF");
            while (paths.hasMoreElements()) {
                URL url = paths.nextElement();
                File file = URLUtils.toFileObject(url);
                urls.add(file.toURI().toURL());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 删除 jdk 自带的 jar
        Iterator<URL> it = urls.iterator();
        while (it.hasNext()) {
            String path = it.next().getPath();
            if (path.contains("/jre/lib/")) {
                it.remove();
            }
        }

        return urls;
    }
public static Collection < URL > getClasspathURLs ( final ClassLoader classLoader ) { Collection < URL > urls = new LinkedHashSet < URL > ( 32 ) ; ClassLoader loader = classLoader ; while ( loader != null ) { String className = loader . getClass ( ) . getName ( ) ; if ( EXT_CLASS_LOADER_NAME . equals ( className ) ) { break ; } if ( loader instanceof URLClassLoader ) { for ( URL url : ( ( URLClassLoader ) loader ) . getURLs ( ) ) { urls . add ( url ) ; } } else if ( className . startsWith ( "weblogic.utils.classloaders." ) ) { // 该死的 WebLogic，只能特殊处理 // GenericClassLoader, FilteringClassLoader, ChangeAwareClassLoader try { Method method = loader . getClass ( ) . getMethod ( "getClassPath" ) ; Object result = method . invoke ( loader ) ; if ( result != null ) { String [ ] paths = StringUtils . split ( result . toString ( ) , File . pathSeparatorChar ) ; for ( String path : paths ) { urls . add ( URLUtils . fromFile ( path ) ) ; } } } catch ( NoSuchMethodException e ) { } catch ( Exception e ) { throw new RuntimeException ( e ) ; } } else if ( className . startsWith ( "org.jboss.modules.ModuleClassLoader" ) ) { // 该死的 Jboss/Wildfly 8，只能特殊处理 try { Set < URL > urlSet = JBossModuleUtils . getClasspathURLs ( loader , false ) ; urls . addAll ( urlSet ) ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } } loader = loader . getParent ( ) ; } // moved jsp classpath from ServletContext.attributes to System.properties String jsp_classpath = System . getProperty ( "org.apache.catalina.jsp_classpath" ) ; String classpath = System . getProperty ( "java.class.path" ) ; classpath = StringUtils . trimToEmpty ( classpath ) + File . pathSeparatorChar + StringUtils . trimToEmpty ( jsp_classpath ) ; if ( classpath . length ( ) > 1 ) { String [ ] paths = StringUtils . split ( classpath , File . pathSeparatorChar ) ; for ( String path : paths ) { path = path . trim ( ) ; if ( path . length ( ) > 0 ) { URL url = URLUtils . fromFile ( path ) ; urls . add ( url ) ; } } } // 添加包含所有的 META-INF/MANIFEST.MF 的 jar 文件 try { Enumeration < URL > paths = classLoader . getResources ( "META-INF/MANIFEST.MF" ) ; while ( paths . hasMoreElements ( ) ) { URL url = paths . nextElement ( ) ; File file = URLUtils . toFileObject ( url ) ; urls . add ( file . toURI ( ) . toURL ( ) ) ; } } catch ( Exception e ) { throw new RuntimeException ( e ) ; } // 删除 jdk 自带的 jar Iterator < URL > it = urls . iterator ( ) ; while ( it . hasNext ( ) ) { String path = it . next ( ) . getPath ( ) ; if ( path . contains ( "/jre/lib/" ) ) { it . remove ( ) ; } } return urls ; }
根据 classLoader 获取所有的 Classpath URLs.
根据 classLoader 获取所有的 Classpath URLs .