Search in sources :

Example 76 with URLClassLoader

use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.

the class LeakTest method testShadow.

private static WeakReference<ClassLoader> testShadow(Class<?> originalTestClass) throws Exception {
    URLClassLoader originalLoader = (URLClassLoader) originalTestClass.getClassLoader();
    URL[] urls = originalLoader.getURLs();
    URLClassLoader shadowLoader = new ShadowClassLoader(urls, originalLoader.getParent());
    System.out.println("Shadow loader is " + shadowLoader);
    String className = originalTestClass.getName();
    Class<?> testClass = Class.forName(className, false, shadowLoader);
    if (testClass.getClassLoader() != shadowLoader) {
        throw new IllegalArgumentException("Loader didn't work: " + testClass.getClassLoader() + " != " + shadowLoader);
    }
    Method main = testClass.getMethod("main", String[].class);
    main.invoke(null, (Object) new String[0]);
    return new WeakReference<ClassLoader>(shadowLoader);
}
Also used : URLClassLoader(java.net.URLClassLoader) WeakReference(java.lang.ref.WeakReference) Method(java.lang.reflect.Method) URL(java.net.URL)

Example 77 with URLClassLoader

use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.

the class Test7064279 method test.

private static Object test(String jarName, String className) throws Exception {
    StringBuilder sb = new StringBuilder(256);
    sb.append("file:");
    sb.append(System.getProperty("test.src", "."));
    sb.append(File.separatorChar);
    sb.append(jarName);
    ClassLoader newLoader = new URLClassLoader(new URL[] { new URL(sb.toString()) });
    ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(newLoader);
    test(newLoader.loadClass(className));
    Thread.currentThread().setContextClassLoader(oldLoader);
    return newLoader;
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL)

Example 78 with URLClassLoader

use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.

the class TwiceIndirectlyLoadABundle method loadAndTest.

public boolean loadAndTest() throws Throwable {
    // Find out where we are running from so we can setup the URLClassLoader URLs
    // test.src and test.classes will be set if running in jtreg, but probably
    // not otherwise
    String testDir = System.getProperty("test.src", System.getProperty("user.dir"));
    String testClassesDir = System.getProperty("test.classes", System.getProperty("user.dir"));
    URL[] urls = new URL[2];
    // Allow for both jtreg and standalone cases here
    // Unlike the 1-level test where we can get the bundle from the caller's
    // class loader, for this one we don't want to expose the resource directory
    // to the next class.  That way we're invoking the LoadItUp2Invoker class
    // from this class that does have access to the resources (two levels
    // up the call stack), but the Invoker itself won't have access to resource
    urls[0] = Paths.get(testDir, "resources").toUri().toURL();
    urls[1] = Paths.get(testClassesDir).toUri().toURL();
    // Make sure we can find it via the URLClassLoader
    URLClassLoader yetAnotherResourceCL = new URLClassLoader(urls, null);
    Class<?> loadItUp2InvokerClazz = Class.forName("LoadItUp2Invoker", true, yetAnotherResourceCL);
    ClassLoader actual = loadItUp2InvokerClazz.getClassLoader();
    if (actual != yetAnotherResourceCL) {
        throw new Exception("LoadItUp2Invoker was loaded by an unexpected CL: " + actual);
    }
    Object loadItUp2Invoker = loadItUp2InvokerClazz.newInstance();
    Method setupMethod = loadItUp2InvokerClazz.getMethod("setup", urls.getClass(), String.class);
    try {
        // For the next class loader we create, we want to leave off
        // the resources.  That way loadItUp2Invoker will have access to
        // them, but the next class won't.
        URL[] noResourceUrl = new URL[1];
        // from above -- just the test classes
        noResourceUrl[0] = urls[1];
        setupMethod.invoke(loadItUp2Invoker, noResourceUrl, rbName);
    } catch (InvocationTargetException ex) {
        throw ex.getTargetException();
    }
    Method testMethod = loadItUp2InvokerClazz.getMethod("test");
    try {
        return (Boolean) testMethod.invoke(loadItUp2Invoker);
    } catch (InvocationTargetException ex) {
        throw ex.getTargetException();
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Method(java.lang.reflect.Method) URL(java.net.URL) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 79 with URLClassLoader

use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.

the class ResourceBundleSearchTest method runTests.

private void runTests() throws Throwable {
    // ensure we are using en as the default Locale so we can find the resource
    Locale.setDefault(Locale.ENGLISH);
    ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
    // Find out where we are running from so we can setup the URLClassLoader URL
    String userDir = System.getProperty("user.dir");
    String testDir = System.getProperty("test.src", userDir);
    URL[] urls = new URL[1];
    urls[0] = Paths.get(testDir, "resources").toUri().toURL();
    URLClassLoader rbClassLoader = new URLClassLoader(urls);
    int testnb = 1;
    System.out.println("ResourceBundleSearchTest starting test #" + (testnb++) + ": " + getTimeStamp());
    // Test 1 - can we find a Logger bundle from doing a stack search?
    // We shouldn't be able to
    assertFalse(testGetBundleFromStackSearch(), "1-testGetBundleFromStackSearch");
    System.out.println("ResourceBundleSearchTest starting test #" + (testnb++) + ": " + getTimeStamp());
    // Test 2 - can we find a Logger bundle off of the Thread context class
    // loader? We should be able to.
    assertTrue(testGetBundleFromTCCL(TCCL_TEST_BUNDLE, rbClassLoader), "2-testGetBundleFromTCCL");
    System.out.println("ResourceBundleSearchTest starting test #" + (testnb++) + ": " + getTimeStamp());
    // can see it there.
    if (isOnClassPath(PROP_RB_NAME, myClassLoader)) {
        debug("We should be able to see " + PROP_RB_NAME + " on the classpath");
        assertTrue(testGetBundleFromSystemClassLoader(PROP_RB_NAME), "3-testGetBundleFromSystemClassLoader");
    } else {
        throw new Exception("TEST SETUP FAILURE: Cannot see " + PROP_RB_NAME + " on the classpath");
    }
    System.out.println("ResourceBundleSearchTest starting test #" + (testnb++) + ": " + getTimeStamp());
    // Test 4 - we should be able to find a bundle from the caller's
    // classloader, but only one level up.
    assertTrue(testGetBundleFromCallersClassLoader(), "4-testGetBundleFromCallersClassLoader");
    System.out.println("ResourceBundleSearchTest starting test #" + (testnb++) + ": " + getTimeStamp());
    // Test 5 - this ensures that getAnonymousLogger(String rbName)
    // can find the bundle from the caller's classloader
    assertTrue(testGetAnonymousLogger(), "5-testGetAnonymousLogger");
    System.out.println("ResourceBundleSearchTest starting test #" + (testnb++) + ": " + getTimeStamp());
    // Test 6 - first call getLogger("myLogger").
    // Then call getLogger("myLogger","bundleName") from a different ClassLoader
    // Make sure we find the bundle
    assertTrue(testGetBundleFromSecondCallersClassLoader(), "6-testGetBundleFromSecondCallersClassLoader");
    System.out.println("ResourceBundleSearchTest generating report: " + getTimeStamp());
    report();
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) MissingResourceException(java.util.MissingResourceException)

Example 80 with URLClassLoader

use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.

the class IndirectlyLoadABundle method getLoggerWithNewCL.

private boolean getLoggerWithNewCL(URL[] urls, String loggerName, String bundleName) throws Throwable {
    Logger result = null;
    ;
    // Test getLogger("foo"); getLogger("foo", "rbName");
    // First do the getLogger() call with no bundle name
    URLClassLoader getLoggerCL = new URLClassLoader(urls, null);
    Class<?> loadItUpClazz1 = Class.forName("LoadItUp1", true, getLoggerCL);
    ClassLoader actual = loadItUpClazz1.getClassLoader();
    if (actual != getLoggerCL) {
        throw new Exception("LoadItUp1 was loaded by an unexpected CL: " + actual);
    }
    Object loadItUp1 = loadItUpClazz1.newInstance();
    if (bundleName != null) {
        Method getLoggerMethod = loadItUpClazz1.getMethod("getLogger", String.class, String.class);
        try {
            result = (Logger) getLoggerMethod.invoke(loadItUp1, loggerName, bundleName);
        } catch (InvocationTargetException ex) {
            throw ex.getTargetException();
        }
    } else {
        Method getLoggerMethod = loadItUpClazz1.getMethod("getLogger", String.class);
        try {
            result = (Logger) getLoggerMethod.invoke(loadItUp1, loggerName);
        } catch (InvocationTargetException ex) {
            throw ex.getTargetException();
        }
    }
    return result != null;
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Method(java.lang.reflect.Method) Logger(java.util.logging.Logger) MalformedURLException(java.net.MalformedURLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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