Example usage for java.lang ClassLoader getParent

List of usage examples for java.lang ClassLoader getParent

Introduction

In this page you can find the example usage for java.lang ClassLoader getParent.

Prototype

@CallerSensitive
public final ClassLoader getParent() 

Source Link

Document

Returns the parent class loader for delegation.

Usage

From source file:sorcer.launcher.SorcerLauncher.java

static List<URL> getClassPath(ClassLoader classLoader) {
    List<URL> result = new ArrayList<URL>();
    ClassLoader cl = classLoader;
    do {/* w  ww  . j a  v  a  2s .  c  o m*/
        if (cl instanceof URLClassLoader) {
            URL[] urls = ((URLClassLoader) cl).getURLs();
            Collections.addAll(result, urls);
        }
    } while ((cl = cl.getParent()) != null);
    return result;
}

From source file:spoon.support.compiler.jdt.JDTBasedSpoonCompiler.java

private CompilerClassLoader getCompilerClassLoader(ClassLoader initialClassLoader) {
    while (initialClassLoader != null) {
        if (initialClassLoader instanceof CompilerClassLoader) {
            return (CompilerClassLoader) initialClassLoader;
        }/*from   w  w  w  . jav  a  2 s  .  c o  m*/
        initialClassLoader = initialClassLoader.getParent();
    }
    return null;
}

From source file:spoon.support.compiler.jdt.JDTBasedSpoonCompiler.java

private boolean hasClassLoader(ClassLoader initialClassLoader, ClassLoader classLoader) {
    while (initialClassLoader != null) {
        if (initialClassLoader == classLoader) {
            return true;
        }/*from ww w  .ja  v a 2s.co  m*/
        initialClassLoader = initialClassLoader.getParent();
    }
    return false;
}

From source file:tr.com.serkanozal.jcommon.util.ClasspathUtil.java

private static Set<URL> findClasspathUrls() {
    Set<URL> urls = new HashSet<URL>();

    try {//from  w  w  w .j av a  2 s  .c om
        String[] classpathProperties = System.getProperty("java.class.path").split(File.pathSeparator);
        for (String classpathProperty : classpathProperties) {
            urls.add(new File(classpathProperty).toURI().toURL());
        }
    } catch (MalformedURLException e) {
        logger.error("Error occured while getting classpath from system property \"java.class.path\"", e);
    }

    String surefireProperty = System.getProperty("surefire.test.class.path");
    if (StringUtils.isNotEmpty(surefireProperty)) {
        try {
            String[] surefireClasspathProperties = surefireProperty.split(File.pathSeparator);
            for (String surefireClasspathProperty : surefireClasspathProperties) {
                urls.add(new File(surefireClasspathProperty).toURI().toURL());
            }
        } catch (MalformedURLException e) {
            logger.error(
                    "Error occured while getting classpath from system property \"surefire.test.class.path\"",
                    e);
        }
    }

    // Start with Current Thread's loader
    ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
    ClassLoader loader = ctxLoader;
    while (loader != null) {
        urls.addAll(findClasspathsByLoader(loader));
        loader = loader.getParent();
    }

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

    ClassLoader sysLoader = ClassLoader.getSystemClassLoader();
    loader = sysLoader;
    while (loader != null) {
        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;
}