Search in sources :

Example 6 with URLClassLoader

use of java.net.URLClassLoader in project vert.x by eclipse.

the class ServiceHelperTest method loadFactoriesFromTCCL.

@Test
public void loadFactoriesFromTCCL() throws Exception {
    ClassLoader custom = new URLClassLoader(new URL[] { new File("target/externals").toURI().toURL() });
    // Try without the TCCL classloader.
    Collection<SomeFactory> factories = ServiceHelper.loadFactories(SomeFactory.class);
    assertThat(factories).isNotNull().hasSize(0);
    // Try with the TCCL classloader
    final ClassLoader originalTCCL = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(custom);
        factories = ServiceHelper.loadFactories(SomeFactory.class);
        assertThat(factories).isNotNull().hasSize(1);
        assertThat(factories.iterator().next().classloader()).isEqualTo(custom);
    } finally {
        Thread.currentThread().setContextClassLoader(originalTCCL);
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File) SomeFactory(io.vertx.test.spi.SomeFactory) Test(org.junit.Test)

Example 7 with URLClassLoader

use of java.net.URLClassLoader in project vert.x by eclipse.

the class ServiceCommandLoaderTest method testNoCommandsWhenLoadedFromEmptyClassloader.

@Test
public void testNoCommandsWhenLoadedFromEmptyClassloader() {
    ClassLoader classLoader = new URLClassLoader(new URL[0], null);
    // We see the implementation from the classpath
    loader = new ServiceCommandFactoryLoader(classLoader);
    assertThat(loader.lookup()).isNotEmpty();
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test)

Example 8 with URLClassLoader

use of java.net.URLClassLoader in project vert.x by eclipse.

the class MetricsOptionsTest method createMetricsFromMetaInfLoader.

static ClassLoader createMetricsFromMetaInfLoader(String factoryFqn) {
    return new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader()) {

        @Override
        public Enumeration<URL> findResources(String name) throws IOException {
            if (name.equals("META-INF/services/io.vertx.core.spi.VertxMetricsFactory")) {
                File f = File.createTempFile("vertx", ".txt");
                f.deleteOnExit();
                Files.write(f.toPath(), factoryFqn.getBytes());
                return Collections.enumeration(Collections.singleton(f.toURI().toURL()));
            }
            return super.findResources(name);
        }
    };
}
Also used : URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL)

Example 9 with URLClassLoader

use of java.net.URLClassLoader in project vert.x by eclipse.

the class NestedRootJarResolverTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // This folder is inside the nested-inf/classes directory, inside nestedroot.jar
    webRoot = "webroot2";
    prevCL = Thread.currentThread().getContextClassLoader();
    URL jarUrl = prevCL.getResource("nestedroot.jar");
    URL rootUrl = new URL("jar:" + jarUrl + "!/nested-inf/classes!/");
    URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { rootUrl }, prevCL);
    Thread.currentThread().setContextClassLoader(urlClassLoader);
}
Also used : URLClassLoader(java.net.URLClassLoader) URL(java.net.URL)

Example 10 with URLClassLoader

use of java.net.URLClassLoader in project elasticsearch by elastic.

the class JarHell method checkJarHell.

/**
     * Checks the current classpath for duplicate classes
     * @throws IllegalStateException if jar hell was found
     */
public static void checkJarHell() throws IOException, URISyntaxException {
    ClassLoader loader = JarHell.class.getClassLoader();
    Logger logger = Loggers.getLogger(JarHell.class);
    if (logger.isDebugEnabled()) {
        logger.debug("java.class.path: {}", System.getProperty("java.class.path"));
        logger.debug("sun.boot.class.path: {}", System.getProperty("sun.boot.class.path"));
        if (loader instanceof URLClassLoader) {
            logger.debug("classloader urls: {}", Arrays.toString(((URLClassLoader) loader).getURLs()));
        }
    }
    checkJarHell(parseClassPath());
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Logger(org.apache.logging.log4j.Logger)

Aggregations

URLClassLoader (java.net.URLClassLoader)1351 URL (java.net.URL)872 File (java.io.File)514 Test (org.junit.Test)317 IOException (java.io.IOException)256 ArrayList (java.util.ArrayList)202 MalformedURLException (java.net.MalformedURLException)186 Method (java.lang.reflect.Method)177 InvocationTargetException (java.lang.reflect.InvocationTargetException)68 JarFile (java.util.jar.JarFile)54 InputStream (java.io.InputStream)50 HashSet (java.util.HashSet)49 HashMap (java.util.HashMap)44 URISyntaxException (java.net.URISyntaxException)41 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)35 Path (java.nio.file.Path)33 QuickTest (com.hazelcast.test.annotation.QuickTest)32 Test (org.junit.jupiter.api.Test)28 URI (java.net.URI)27 List (java.util.List)27