Java URL Connection findClasspathUrls(ClassLoader classLoader)

Here you can find the source of findClasspathUrls(ClassLoader classLoader)

Description

find Classpath Urls

License

Open Source License

Declaration

public static Set<URL> findClasspathUrls(ClassLoader classLoader) 

Method Source Code


//package com.java2s;
/*//ww  w .ja  va2 s. co  m
 * Copyright (c) 1986-2016, Serkan OZAL, All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

public class Main {
    public static Set<URL> findClasspathUrls(ClassLoader classLoader) {
        Set<URL> urls = new HashSet<URL>();

        try {
            String[] classpathProperties = System.getProperty("java.class.path").split(File.pathSeparator);
            for (String classpathProperty : classpathProperties) {
                urls.add(new File(classpathProperty).toURI().toURL());
            }
            urls.addAll(getExtURLs(getExtDirs()));
        } catch (IOException e) {
            e.printStackTrace();
        }

        String surefireProperty = System.getProperty("surefire.test.class.path");
        if (surefireProperty != null && surefireProperty.trim().length() > 0) {
            try {
                String[] surefireClasspathProperties = surefireProperty.split(File.pathSeparator);
                for (String surefireClasspathProperty : surefireClasspathProperties) {
                    urls.add(new File(surefireClasspathProperty).toURI().toURL());
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }

        // Also start with this classes's loader, in some environment this can
        // be different than the current thread's one
        for (ClassLoader loader = classLoader; loader != null; loader = loader.getParent()) {
            urls.addAll(findClasspathsByLoader(loader));
            loader = loader.getParent();
        }

        Map<URL, URL> replaceURLs = new HashMap<URL, URL>();
        Set<URL> derivedUrls = new HashSet<URL>();
        for (URL url : urls) {
            if (url.getProtocol().startsWith("vfs")) {
                try {
                    URLConnection conn = url.openConnection();
                    Object virtualFile = conn.getContent();
                    if (virtualFile.getClass().getName().equals("org.jboss.vfs.VirtualFile")) {
                        File file = (File) virtualFile.getClass().getMethod("getPhysicalFile").invoke(virtualFile);
                        String fileName = file.getCanonicalPath();
                        String name = (String) virtualFile.getClass().getMethod("getName").invoke(virtualFile);
                        name = name.trim().toLowerCase();
                        if ((name.endsWith("jar") || name.endsWith("zip") && fileName.endsWith("/contents"))) {
                            fileName = fileName.replace("contents", name);
                        }
                        URL repURL = new URL("file:/" + fileName);
                        replaceURLs.put(url, repURL);
                    }
                } catch (Exception e) {
                    // We don't expect to trapped here
                    e.printStackTrace();
                }
            }
            try {
                if (url.toExternalForm().endsWith("WEB-INF/classes")) {
                    derivedUrls.add(new URL(url.toExternalForm().replace("WEB-INF/classes", "WEB-INF/lib")));
                } else if (url.toExternalForm().endsWith("WEB-INF/classes/")) {
                    derivedUrls.add(new URL(url.toExternalForm().replace("WEB-INF/classes/", "WEB-INF/lib/")));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        urls.removeAll(replaceURLs.keySet());
        urls.addAll(replaceURLs.values());
        urls.addAll(derivedUrls);
        replaceURLs.clear();
        // Check contained urls
        for (URL url : urls) {
            for (URL rootUrl : urls) {
                if (url.equals(rootUrl)) {
                    continue;
                }
                if (url.toExternalForm().startsWith(rootUrl.toExternalForm())) {
                    if (replaceURLs.get(url) != null) {
                        URL settledUrl = replaceURLs.get(url);
                        if (settledUrl.toExternalForm().startsWith(rootUrl.toExternalForm())) {
                            replaceURLs.put(url, rootUrl);
                        }
                    } else {
                        replaceURLs.put(url, rootUrl);
                    }
                }
            }
        }
        urls.removeAll(replaceURLs.keySet());
        return urls;
    }

    protected static Set<URL> getExtURLs(File[] dirs) throws IOException {
        Set<URL> urls = new HashSet<URL>();
        for (int i = 0; i < dirs.length; i++) {
            String[] files = dirs[i].list();
            if (files != null) {
                for (int j = 0; j < files.length; j++) {
                    if (!files[j].equals("meta-index")) {
                        File f = new File(dirs[i], files[j]);
                        urls.add(f.toURI().toURL());
                    }
                }
            }
        }
        return urls;
    }

    protected static File[] getExtDirs() {
        String extDirsProperty = System.getProperty("java.ext.dirs");
        File[] extDirs;
        if (extDirsProperty != null) {
            StringTokenizer st = new StringTokenizer(extDirsProperty, File.pathSeparator);
            int count = st.countTokens();
            extDirs = new File[count];
            for (int i = 0; i < count; i++) {
                extDirs[i] = new File(st.nextToken());
            }
        } else {
            extDirs = new File[0];
        }
        return extDirs;
    }

    private static Set<URL> findClasspathsByLoader(ClassLoader loader) {
        Set<URL> urls = new HashSet<URL>();
        if (loader instanceof URLClassLoader) {
            URLClassLoader urlLoader = (URLClassLoader) loader;
            urls.addAll(Arrays.asList(urlLoader.getURLs()));
        } else {
            Enumeration<URL> urlEnum;
            try {
                urlEnum = loader.getResources("");
                while (urlEnum.hasMoreElements()) {
                    URL url = urlEnum.nextElement();
                    if (url.getProtocol().startsWith("bundleresource")) {
                        continue;
                    }
                    urls.add(url);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return urls;
    }
}

Related

  1. doGet(String targetURL)
  2. doGet(String url, String charset)
  3. doPost(final URL url, final Map parameters, final boolean encode)
  4. doPost(String url, String params, String charset)
  5. findClasses(URL resource, String packageName, boolean annotated)
  6. findResourceInJarPackage(URL url, String packageName, String packageDirName, boolean recursive, List resources)
  7. getAsStream(URL url)
  8. getBaseAuthInputStreamFromURL(String query, String basicAuthString)
  9. getBaseURL(URLConnection conn)