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:com.clican.pluto.orm.dynamic.impl.ClassLoaderUtilImpl.java

private void populateJars(Set<String> jars, ClassLoader loader) {
    if (!(loader instanceof URLClassLoader)) {
        if (log.isDebugEnabled()) {
            log.debug("The ClassLoader[" + loader.getClass().getName() + "] is ignored");
        }//from   www . ja va  2 s. c o  m
    } else {
        URLClassLoader urlClassLoader = (URLClassLoader) loader;

        for (URL url : urlClassLoader.getURLs()) {
            jars.add(url.getPath());
        }

    }
    if (loader == ClassLoader.getSystemClassLoader()) {
        return;
    } else {
        populateJars(jars, loader.getParent());
    }
}

From source file:psiprobe.beans.LogResolverBean.java

/**
 * Gets the all log destinations./* ww w.  ja  v  a  2s  .  c  o  m*/
 *
 * @return the all log destinations
 */
private List<LogDestination> getAllLogDestinations() {
    if (Instruments.isInitialized()) {
        List<LogDestination> allAppenders = new ArrayList<>();

        //
        // interrogate classloader hierarchy
        //
        ClassLoader cl2 = Thread.currentThread().getContextClassLoader().getParent();
        while (cl2 != null) {
            interrogateClassLoader(cl2, null, allAppenders);
            cl2 = cl2.getParent();
        }

        //
        // check for known stdout files, such as "catalina.out"
        //
        interrogateStdOutFiles(allAppenders);

        //
        // interrogate webapp classloaders and available loggers
        //
        List<Context> contexts = getContainerWrapper().getTomcatContainer().findContexts();
        for (Context ctx : contexts) {
            interrogateContext(ctx, allAppenders);
        }

        return allAppenders;
    }
    return null;
}

From source file:hudson.ClassicPluginStrategy.java

private DependencyClassLoader findAncestorDependencyClassLoader(ClassLoader classLoader) {
    for (; classLoader != null; classLoader = classLoader.getParent()) {
        if (classLoader instanceof DependencyClassLoader) {
            return (DependencyClassLoader) classLoader;
        }/*w w w. j  a v a  2s  .  c  o m*/

        if (classLoader instanceof AntClassLoader) {
            // AntClassLoaders hold parents not only as AntClassLoader#getParent()
            // but also as AntClassLoader#getConfiguredParent()
            DependencyClassLoader ret = findAncestorDependencyClassLoader(
                    ((AntClassLoader) classLoader).getConfiguredParent());
            if (ret != null) {
                return ret;
            }
        }
    }
    return null;
}

From source file:cn.teamlab.wg.framework.struts2.json.PackageBasedActionConfigBuilder.java

private UrlSet buildUrlSet(List<URL> resourceUrls) throws IOException {
    ClassLoaderInterface classLoaderInterface = getClassLoaderInterface();
    UrlSet urlSet = new UrlSet(resourceUrls);
    urlSet = urlSet.include(new UrlSet(classLoaderInterface, this.fileProtocols));

    // excluding the urls found by the parent class loader is desired, but
    // fails in JBoss (all urls are removed)
    if (excludeParentClassLoader) {
        // exclude parent of classloaders
        ClassLoaderInterface parent = classLoaderInterface.getParent();
        // if reload is enabled, we need to step up one level, otherwise the
        // UrlSet will be empty
        // this happens because the parent of the realoding class loader is
        // the web app classloader
        if (parent != null && isReloadEnabled())
            parent = parent.getParent();

        if (parent != null)
            urlSet = urlSet.exclude(parent);

        try {/*from ww  w.jav a2  s. c o  m*/
            // This may fail in some sandboxes, ie GAE
            ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
            urlSet = urlSet.exclude(new ClassLoaderInterfaceDelegate(systemClassLoader.getParent()));

        } catch (SecurityException e) {
            if (LOG.isWarnEnabled())
                LOG.warn(
                        "Could not get the system classloader due to security constraints, there may be improper urls left to scan");
        }
    }

    // try to find classes dirs inside war files
    urlSet = urlSet.includeClassesUrl(classLoaderInterface, new UrlSet.FileProtocolNormalizer() {
        public URL normalizeToFileProtocol(URL url) {
            return fileManager.normalizeToFileProtocol(url);
        }
    });

    urlSet = urlSet.excludeJavaExtDirs();
    urlSet = urlSet.excludeJavaEndorsedDirs();
    try {
        urlSet = urlSet.excludeJavaHome();
    } catch (NullPointerException e) {
        // This happens in GAE since the sandbox contains no java.home
        // directory
        if (LOG.isWarnEnabled())
            LOG.warn("Could not exclude JAVA_HOME, is this a sandbox jvm?");
    }
    urlSet = urlSet.excludePaths(System.getProperty("sun.boot.class.path", ""));
    urlSet = urlSet.exclude(".*/JavaVM.framework/.*");

    String[] localIncludeJars = cn.teamlab.wg.framework.struts2.json.util.StringUtils.concat(includeJars,
            conventionIncludeJars);

    if (localIncludeJars == null) {
        urlSet = urlSet.exclude(".*?\\.jar(!/|/)?");
    } else {
        // jar urls regexes were specified
        List<URL> rawIncludedUrls = urlSet.getUrls();
        Set<URL> includeUrls = new HashSet<URL>();
        boolean[] patternUsed = new boolean[localIncludeJars.length];

        for (URL url : rawIncludedUrls) {
            if (fileProtocols.contains(url.getProtocol())) {
                // it is a jar file, make sure it macthes at least a url
                // regex
                for (int i = 0; i < localIncludeJars.length; i++) {
                    String includeJar = localIncludeJars[i];
                    if (Pattern.matches(includeJar, url.toExternalForm())) {
                        includeUrls.add(url);
                        patternUsed[i] = true;
                        break;
                    }
                }
            } else {
                // it is not a jar
                includeUrls.add(url);
            }
        }

        if (LOG.isWarnEnabled()) {
            for (int i = 0; i < patternUsed.length; i++) {
                if (!patternUsed[i]) {
                    LOG.warn("The includeJars pattern [#0] did not match any jars in the classpath",
                            localIncludeJars[i]);
                }
            }
        }
        return new UrlSet(includeUrls);
    }

    return urlSet;
}

From source file:es.cenobit.struts2.json.PackageBasedActionConfigBuilder.java

private UrlSet buildUrlSet(List<URL> resourceUrls) throws IOException {
    ClassLoaderInterface classLoaderInterface = getClassLoaderInterface();
    UrlSet urlSet = new UrlSet(resourceUrls);
    urlSet = urlSet.include(new UrlSet(classLoaderInterface, this.fileProtocols));

    // excluding the urls found by the parent class loader is desired, but
    // fails in JBoss (all urls are removed)
    if (excludeParentClassLoader) {
        // exclude parent of classloaders
        ClassLoaderInterface parent = classLoaderInterface.getParent();
        // if reload is enabled, we need to step up one level, otherwise the
        // UrlSet will be empty
        // this happens because the parent of the realoding class loader is
        // the web app classloader
        if (parent != null && isReloadEnabled())
            parent = parent.getParent();

        if (parent != null)
            urlSet = urlSet.exclude(parent);

        try {/*from   w  ww  .ja v  a2  s . c om*/
            // This may fail in some sandboxes, ie GAE
            ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
            urlSet = urlSet.exclude(new ClassLoaderInterfaceDelegate(systemClassLoader.getParent()));

        } catch (SecurityException e) {
            if (LOG.isWarnEnabled())
                LOG.warn(
                        "Could not get the system classloader due to security constraints, there may be improper urls left to scan");
        }
    }

    // try to find classes dirs inside war files
    urlSet = urlSet.includeClassesUrl(classLoaderInterface, new UrlSet.FileProtocolNormalizer() {
        public URL normalizeToFileProtocol(URL url) {
            return fileManager.normalizeToFileProtocol(url);
        }
    });

    urlSet = urlSet.excludeJavaExtDirs();
    urlSet = urlSet.excludeJavaEndorsedDirs();
    try {
        urlSet = urlSet.excludeJavaHome();
    } catch (NullPointerException e) {
        // This happens in GAE since the sandbox contains no java.home
        // directory
        if (LOG.isWarnEnabled())
            LOG.warn("Could not exclude JAVA_HOME, is this a sandbox jvm?");
    }
    urlSet = urlSet.excludePaths(System.getProperty("sun.boot.class.path", ""));
    urlSet = urlSet.exclude(".*/JavaVM.framework/.*");

    String[] localIncludeJars = es.cenobit.struts2.json.util.StringUtils.concat(includeJars,
            conventionIncludeJars);

    if (localIncludeJars == null) {
        urlSet = urlSet.exclude(".*?\\.jar(!/|/)?");
    } else {
        // jar urls regexes were specified
        List<URL> rawIncludedUrls = urlSet.getUrls();
        Set<URL> includeUrls = new HashSet<URL>();
        boolean[] patternUsed = new boolean[localIncludeJars.length];

        for (URL url : rawIncludedUrls) {
            if (fileProtocols.contains(url.getProtocol())) {
                // it is a jar file, make sure it macthes at least a url
                // regex
                for (int i = 0; i < localIncludeJars.length; i++) {
                    String includeJar = localIncludeJars[i];
                    if (Pattern.matches(includeJar, url.toExternalForm())) {
                        includeUrls.add(url);
                        patternUsed[i] = true;
                        break;
                    }
                }
            } else {
                // it is not a jar
                includeUrls.add(url);
            }
        }

        if (LOG.isWarnEnabled()) {
            for (int i = 0; i < patternUsed.length; i++) {
                if (!patternUsed[i]) {
                    LOG.warn("The includeJars pattern [#0] did not match any jars in the classpath",
                            localIncludeJars[i]);
                }
            }
        }
        return new UrlSet(includeUrls);
    }

    return urlSet;
}

From source file:de.icongmbh.oss.maven.plugin.javassist.JavassistTransformerExecutor.java

private void debugClassLoader(final ClassPool classPool) {
    if (!LOGGER.isDebugEnabled()) {
        return;/*w  ww .  j  a  va  2s . c o  m*/
    }
    LOGGER.debug(" - classPool: {}", classPool.toString());
    ClassLoader classLoader = classPool.getClassLoader();
    while (classLoader != null) {
        LOGGER.debug(" -- {}: {}", classLoader.getClass().getName(), classLoader.toString());
        if (classLoader instanceof URLClassLoader) {
            LOGGER.debug(" --- urls: {}", Arrays.deepToString(((URLClassLoader) classLoader).getURLs()));
        }
        classLoader = classLoader.getParent();
    }
}

From source file:com.ibm.team.build.internal.hjplugin.RTCFacadeFactory.java

private static RTCFacadeWrapper newFacade(String fullClassName, File toolkitFile, PrintStream debugLog)
        throws Exception {

    if (!toolkitFile.exists()) {
        throw new IllegalArgumentException(
                Messages.RTCFacadeFactory_toolkit_not_found(toolkitFile.getAbsolutePath()));
    }//from  www.  j  a va  2 s  .c om
    if (!toolkitFile.isDirectory()) {
        throw new IllegalArgumentException(
                Messages.RTCFacadeFactory_toolkit_path_not_directory(toolkitFile.getAbsolutePath()));
    }

    RTCFacadeWrapper result = new RTCFacadeWrapper();

    URL[] toolkitURLs = getToolkitJarURLs(toolkitFile, debugLog);

    Class<?> originalClass = RTCFacadeFactory.class;
    ClassLoader originalClassLoader = originalClass.getClassLoader();
    debug(debugLog, "Original class loader: " + originalClassLoader); //$NON-NLS-1$

    // Get the jar for the hjplugin-rtc jar.
    URL[] combinedURLs;
    URL hjplugin_rtcJar = getFacadeJarURL(debugLog);
    if (hjplugin_rtcJar != null) {
        combinedURLs = new URL[toolkitURLs.length + 1];
        combinedURLs[0] = hjplugin_rtcJar;
        System.arraycopy(toolkitURLs, 0, combinedURLs, 1, toolkitURLs.length);
    } else {
        combinedURLs = toolkitURLs;
    }
    debug(debugLog, "System class loader " + ClassLoader.getSystemClassLoader()); //$NON-NLS-1$

    // We want the parent class loader to exclude the class loader which would normally
    // load classes from the hjplugin-rtc jar (because that class loader doesn't include
    // the toolkit jars).
    ClassLoader parentClassLoader = ClassLoader.getSystemClassLoader();

    // Normally the system class loader and the original class loader are different.
    // However in the case of running the tests within our build, the system class loader
    // is the original class loader with our single jar (and not its dependencies from the
    // toolkit) which results in ClassNotFound for classes we depend on (i.e. IProgressMonitor).
    // So use the parent in this case.
    if (parentClassLoader == originalClassLoader) {
        debug(debugLog, "System class loader and original are the same. Using parent " //$NON-NLS-1$
                + originalClassLoader.getParent());
        parentClassLoader = originalClassLoader.getParent();
    }
    if (Boolean.parseBoolean(System.getProperty(DISABLE_RTC_FACADE_CLASS_LOADER_PROPERTY, "false"))) { //$NON-NLS-1$
        debug(debugLog, "RTCFacadeClassLoader disabled, using URLClassLoader"); //$NON-NLS-1$
        result.newClassLoader = new URLClassLoader(combinedURLs, parentClassLoader);
    } else {
        result.newClassLoader = new RTCFacadeClassLoader(combinedURLs, parentClassLoader);
    }

    debug(debugLog, "new classloader: " + result.newClassLoader); //$NON-NLS-1$

    Class<?> facadeClass = result.newClassLoader.loadClass(fullClassName);
    debug(debugLog, "facadeClass: " + facadeClass); //$NON-NLS-1$
    debug(debugLog, "facadeClass classloader: " + facadeClass.getClassLoader()); //$NON-NLS-1$

    // using the new class loader get the facade instance
    // then revert immediately back to the original class loader
    ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(result.newClassLoader);
    try {
        result.facade = facadeClass.newInstance();
    } finally {
        Thread.currentThread().setContextClassLoader(originalContextClassLoader);
    }

    debug(debugLog, "facade: " + result.facade); //$NON-NLS-1$
    return result;
}

From source file:com.apdplat.platform.struts.APDPlatPackageBasedActionConfigBuilder.java

private UrlSet buildUrlSet() throws IOException {
    ClassLoaderInterface classLoaderInterface = getClassLoaderInterface();
    UrlSet urlSet = new UrlSet(classLoaderInterface, this.fileProtocols);

    //excluding the urls found by the parent class loader is desired, but fails in JBoss (all urls are removed)
    if (excludeParentClassLoader) {
        //exclude parent of classloaders
        ClassLoaderInterface parent = classLoaderInterface.getParent();
        //if reload is enabled, we need to step up one level, otherwise the UrlSet will be empty
        //this happens because the parent of the realoding class loader is the web app classloader
        if (parent != null && isReloadEnabled())
            parent = parent.getParent();

        if (parent != null)
            urlSet = urlSet.exclude(parent);

        try {/*ww  w .  j  a  v  a 2s .  com*/
            // This may fail in some sandboxes, ie GAE
            ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
            urlSet = urlSet.exclude(new ClassLoaderInterfaceDelegate(systemClassLoader.getParent()));

        } catch (SecurityException e) {
            if (LOG.isWarnEnabled())
                LOG.warn(
                        "Could not get the system classloader due to security constraints, there may be improper urls left to scan");
        }
    }

    //try to find classes dirs inside war files
    urlSet = urlSet.includeClassesUrl(classLoaderInterface);

    urlSet = urlSet.excludeJavaExtDirs();
    urlSet = urlSet.excludeJavaEndorsedDirs();
    try {
        urlSet = urlSet.excludeJavaHome();
    } catch (NullPointerException e) {
        // This happens in GAE since the sandbox contains no java.home directory
        if (LOG.isWarnEnabled())
            LOG.warn("Could not exclude JAVA_HOME, is this a sandbox jvm?");
    }
    urlSet = urlSet.excludePaths(System.getProperty("sun.boot.class.path", ""));
    urlSet = urlSet.exclude(".*/JavaVM.framework/.*");

    if (includeJars == null) {
        urlSet = urlSet.exclude(".*?\\.jar(!/|/)?");
    } else {
        Set<URL> includeUrls = new HashSet<URL>();
        boolean[] patternUsed = new boolean[includeJars.length];
        for (int i = 0; i < includeJars.length; i++) {
            try {
                String includeJar = includeJars[i];
                FileSystemResource resource = new FileSystemResource(FileUtils.getAbsolutePath(includeJar));
                includeUrls.add(resource.getURL());
                patternUsed[i] = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (LOG.isWarnEnabled()) {
            for (int i = 0; i < patternUsed.length; i++) {
                if (!patternUsed[i]) {
                    LOG.warn("The includeJars pattern [#0] did not match any jars in the classpath",
                            includeJars[i]);
                }
            }
        }
        return new UrlSet(includeUrls);
    }

    return urlSet;
}

From source file:com.meltmedia.cadmium.servlets.ClassLoaderLeakPreventor.java

/**
 * Test if provided ClassLoader is the classloader of the web application, or a child thereof
 *//*  ww  w  . j a va 2 s  . co  m*/
protected boolean isWebAppClassLoaderOrChild(ClassLoader cl) {
    final ClassLoader webAppCL = getWebApplicationClassLoader();
    // final ClassLoader webAppCL = Thread.currentThread().getContextClassLoader();

    while (cl != null) {
        if (cl == webAppCL)
            return true;

        cl = cl.getParent();
    }

    return false;
}

From source file:com.icesoft.jasper.compiler.TldLocationsCache.java

private void scanJars() throws Exception {

    ClassLoader webappLoader = Thread.currentThread().getContextClassLoader();
    ClassLoader loader = webappLoader;

    while (loader != null) {
        if (loader instanceof URLClassLoader) {
            URL[] urls = ((URLClassLoader) loader).getURLs();
            if (null != urls) {
                for (int i = 0; i < urls.length; i++) {
                    URLConnection conn = urls[i].openConnection();
                    if (conn instanceof JarURLConnection) {
                        if (needScanJar(loader, webappLoader,
                                ((JarURLConnection) conn).getJarFile().getName())) {
                            scanJar((JarURLConnection) conn, true);
                        }/*from  w w  w  . j a  v  a  2 s.  co m*/
                    } else {
                        String urlStr = urls[i].toString();
                        if (urlStr.startsWith(FILE_PROTOCOL) && urlStr.endsWith(JAR_FILE_SUFFIX)
                                && needScanJar(loader, webappLoader, urlStr)) {
                            URL jarURL = new URL("jar:" + urlStr + "!/");
                            scanJar((JarURLConnection) jarURL.openConnection(), true);
                        }
                    }
                }
            }
        }

        loader = loader.getParent();
    }
}