Search in sources :

Example 1 with VisitableURLClassLoader

use of org.gradle.internal.classloader.VisitableURLClassLoader in project gradle by gradle.

the class DefaultToolingImplementationLoader method createImplementationClassLoader.

private ClassLoader createImplementationClassLoader(Distribution distribution, ProgressLoggerFactory progressLoggerFactory, InternalBuildProgressListener progressListener, File userHomeDir, BuildCancellationToken cancellationToken) {
    ClassPath implementationClasspath = distribution.getToolingImplementationClasspath(progressLoggerFactory, progressListener, userHomeDir, cancellationToken);
    LOGGER.debug("Using tooling provider classpath: {}", implementationClasspath);
    FilteringClassLoader.Spec filterSpec = new FilteringClassLoader.Spec();
    filterSpec.allowPackage("org.gradle.tooling.internal.protocol");
    FilteringClassLoader filteringClassLoader = new FilteringClassLoader(classLoader, filterSpec);
    return new VisitableURLClassLoader(filteringClassLoader, implementationClasspath);
}
Also used : ClassPath(org.gradle.internal.classpath.ClassPath) VisitableURLClassLoader(org.gradle.internal.classloader.VisitableURLClassLoader) FilteringClassLoader(org.gradle.internal.classloader.FilteringClassLoader)

Example 2 with VisitableURLClassLoader

use of org.gradle.internal.classloader.VisitableURLClassLoader in project gradle by gradle.

the class InProcessWorkerFactory method createWorkerClassLoader.

private ClassLoader createWorkerClassLoader(ClassLoader actionClasspathLoader, Iterable<String> sharedPackages, Class<? extends WorkerAction> actionClass) {
    FilteringClassLoader.Spec actionFilterSpec = new FilteringClassLoader.Spec();
    for (String packageName : sharedPackages) {
        actionFilterSpec.allowPackage(packageName);
    }
    ClassLoader actionFilteredClasspathLoader = classLoaderFactory.createFilteringClassLoader(actionClasspathLoader, actionFilterSpec);
    FilteringClassLoader.Spec gradleApiFilterSpec = new FilteringClassLoader.Spec();
    // Logging
    gradleApiFilterSpec.allowPackage("org.slf4j");
    gradleApiFilterSpec.allowClass(Logger.class);
    gradleApiFilterSpec.allowClass(LogLevel.class);
    // Native
    gradleApiFilterSpec.allowPackage("org.gradle.internal.nativeintegration");
    gradleApiFilterSpec.allowPackage("org.gradle.internal.nativeplatform");
    gradleApiFilterSpec.allowPackage("net.rubygrapefruit.platform");
    // TODO:pm Add Gradle API and a way to opt out of it (for compiler workers)
    ClassLoader gradleApiLoader = classLoaderFactory.createFilteringClassLoader(actionClass.getClassLoader(), gradleApiFilterSpec);
    ClassLoader actionAndGradleApiLoader = new CachingClassLoader(new MultiParentClassLoader(gradleApiLoader, actionFilteredClasspathLoader));
    return new VisitableURLClassLoader(actionAndGradleApiLoader, ClasspathUtil.getClasspath(actionClass.getClassLoader()));
}
Also used : VisitableURLClassLoader(org.gradle.internal.classloader.VisitableURLClassLoader) FilteringClassLoader(org.gradle.internal.classloader.FilteringClassLoader) MultiParentClassLoader(org.gradle.internal.classloader.MultiParentClassLoader) FilteringClassLoader(org.gradle.internal.classloader.FilteringClassLoader) VisitableURLClassLoader(org.gradle.internal.classloader.VisitableURLClassLoader) MultiParentClassLoader(org.gradle.internal.classloader.MultiParentClassLoader) CachingClassLoader(org.gradle.internal.classloader.CachingClassLoader) CachingClassLoader(org.gradle.internal.classloader.CachingClassLoader)

Example 3 with VisitableURLClassLoader

use of org.gradle.internal.classloader.VisitableURLClassLoader in project gradle by gradle.

the class DefaultPayloadClassLoaderRegistry method getClassLoader.

private ClassLoader getClassLoader(ClassLoaderDetails details) {
    ClassLoader classLoader = cache.getClassLoader(details, detailsToClassLoader);
    if (details.spec instanceof ClientOwnedClassLoaderSpec) {
        ClientOwnedClassLoaderSpec spec = (ClientOwnedClassLoaderSpec) details.spec;
        VisitableURLClassLoader urlClassLoader = (VisitableURLClassLoader) classLoader;
        Set<URL> currentClassPath = ImmutableSet.copyOf(urlClassLoader.getURLs());
        for (URL url : spec.getClasspath()) {
            if (!currentClassPath.contains(url)) {
                JavaReflectionUtil.method(URLClassLoader.class, Void.class, "addURL", URL.class).invoke(urlClassLoader, url);
            }
        }
    }
    return classLoader;
}
Also used : VisitableURLClassLoader(org.gradle.internal.classloader.VisitableURLClassLoader) URLClassLoader(java.net.URLClassLoader) VisitableURLClassLoader(org.gradle.internal.classloader.VisitableURLClassLoader) URLClassLoader(java.net.URLClassLoader) VisitableURLClassLoader(org.gradle.internal.classloader.VisitableURLClassLoader) URL(java.net.URL)

Example 4 with VisitableURLClassLoader

use of org.gradle.internal.classloader.VisitableURLClassLoader in project gradle by gradle.

the class ProcessBootstrap method runNoExit.

private void runNoExit(String mainClassName, String[] args) throws Exception {
    ClassPathRegistry classPathRegistry = new DefaultClassPathRegistry(new DefaultClassPathProvider(new DefaultModuleRegistry(CurrentGradleInstallation.get())));
    ClassLoaderFactory classLoaderFactory = new DefaultClassLoaderFactory();
    ClassPath antClasspath = classPathRegistry.getClassPath("ANT");
    ClassPath runtimeClasspath = classPathRegistry.getClassPath("GRADLE_RUNTIME");
    ClassLoader antClassLoader = classLoaderFactory.createIsolatedClassLoader(antClasspath);
    ClassLoader runtimeClassLoader = new VisitableURLClassLoader(antClassLoader, runtimeClasspath);
    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(runtimeClassLoader);
    try {
        Class<?> mainClass = runtimeClassLoader.loadClass(mainClassName);
        Object entryPoint = mainClass.newInstance();
        Method mainMethod = mainClass.getMethod("run", String[].class);
        mainMethod.invoke(entryPoint, new Object[] { args });
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
        ClassLoaderUtils.tryClose(runtimeClassLoader);
        ClassLoaderUtils.tryClose(antClassLoader);
    }
}
Also used : DefaultClassLoaderFactory(org.gradle.internal.classloader.DefaultClassLoaderFactory) ClassPath(org.gradle.internal.classpath.ClassPath) ClassLoaderFactory(org.gradle.internal.classloader.ClassLoaderFactory) DefaultClassLoaderFactory(org.gradle.internal.classloader.DefaultClassLoaderFactory) VisitableURLClassLoader(org.gradle.internal.classloader.VisitableURLClassLoader) DefaultClassPathProvider(org.gradle.api.internal.DefaultClassPathProvider) Method(java.lang.reflect.Method) ClassPathRegistry(org.gradle.api.internal.ClassPathRegistry) DefaultClassPathRegistry(org.gradle.api.internal.DefaultClassPathRegistry) DefaultModuleRegistry(org.gradle.api.internal.classpath.DefaultModuleRegistry) DefaultClassPathRegistry(org.gradle.api.internal.DefaultClassPathRegistry) VisitableURLClassLoader(org.gradle.internal.classloader.VisitableURLClassLoader)

Aggregations

VisitableURLClassLoader (org.gradle.internal.classloader.VisitableURLClassLoader)4 FilteringClassLoader (org.gradle.internal.classloader.FilteringClassLoader)2 ClassPath (org.gradle.internal.classpath.ClassPath)2 Method (java.lang.reflect.Method)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 ClassPathRegistry (org.gradle.api.internal.ClassPathRegistry)1 DefaultClassPathProvider (org.gradle.api.internal.DefaultClassPathProvider)1 DefaultClassPathRegistry (org.gradle.api.internal.DefaultClassPathRegistry)1 DefaultModuleRegistry (org.gradle.api.internal.classpath.DefaultModuleRegistry)1 CachingClassLoader (org.gradle.internal.classloader.CachingClassLoader)1 ClassLoaderFactory (org.gradle.internal.classloader.ClassLoaderFactory)1 DefaultClassLoaderFactory (org.gradle.internal.classloader.DefaultClassLoaderFactory)1 MultiParentClassLoader (org.gradle.internal.classloader.MultiParentClassLoader)1