Java Code Examples for java.rmi.server.RMIClassLoader#loadClass()

The following examples show how to use java.rmi.server.RMIClassLoader#loadClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassLoading.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Repeatedly load a class not found in classpath through RMIClassLoader.
 * Arguments: <# reps>
 */
public long run(String[] args) throws Exception {
    int reps = Integer.parseInt(args[0]);
    CodeSource csrc = getClass().getProtectionDomain().getCodeSource();
    String url = "jar:" + csrc.getLocation().toString() + ALTROOT;

    long start = System.currentTimeMillis();
    for (int i = 0; i < reps; i++)
        RMIClassLoader.loadClass(url, CLASSNAME);
    long time = System.currentTimeMillis() - start;

    return time;
}
 
Example 2
Source File: ClassLoading.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Repeatedly load a class not found in classpath through RMIClassLoader.
 * Arguments: <# reps>
 */
public long run(String[] args) throws Exception {
    int reps = Integer.parseInt(args[0]);
    CodeSource csrc = getClass().getProtectionDomain().getCodeSource();
    String url = "jar:" + csrc.getLocation().toString() + ALTROOT;

    long start = System.currentTimeMillis();
    for (int i = 0; i < reps; i++)
        RMIClassLoader.loadClass(url, CLASSNAME);
    long time = System.currentTimeMillis() - start;

    return time;
}
 
Example 3
Source File: ClassLoading.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Repeatedly load a class not found in classpath through RMIClassLoader.
 * Arguments: <# reps>
 */
public long run(String[] args) throws Exception {
    int reps = Integer.parseInt(args[0]);
    CodeSource csrc = getClass().getProtectionDomain().getCodeSource();
    String url = "jar:" + csrc.getLocation().toString() + ALTROOT;

    long start = System.currentTimeMillis();
    for (int i = 0; i < reps; i++)
        RMIClassLoader.loadClass(url, CLASSNAME);
    long time = System.currentTimeMillis() - start;

    return time;
}
 
Example 4
Source File: ClassLoading.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Repeatedly load a class not found in classpath through RMIClassLoader.
 * Arguments: <# reps>
 */
public long run(String[] args) throws Exception {
    int reps = Integer.parseInt(args[0]);
    CodeSource csrc = getClass().getProtectionDomain().getCodeSource();
    String url = "jar:" + csrc.getLocation().toString() + ALTROOT;

    long start = System.currentTimeMillis();
    for (int i = 0; i < reps; i++)
        RMIClassLoader.loadClass(url, CLASSNAME);
    long time = System.currentTimeMillis() - start;

    return time;
}
 
Example 5
Source File: DefaultProperty.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        ServiceConfiguration.installServiceConfigurationFile();

        System.setProperty(
            "java.rmi.server.RMIClassLoaderSpi", "default");

        String classname = "Foo";

        URL codebaseURL = null;
        try {
            codebaseURL = TestLibrary.installClassInCodebase(
                classname, "remote_codebase");
        } catch (MalformedURLException e) {
            TestLibrary.bomb(e);
        }

        TestLibrary.suggestSecurityManager(null);

        Class fooClass = RMIClassLoader.loadClass(codebaseURL, classname);
        if (!fooClass.getName().equals(classname)) {
            throw new RuntimeException(
                "wrong class name, expected: " + classname +
                ", received: " + fooClass.getName());
        }

        String annotation = RMIClassLoader.getClassAnnotation(fooClass);
        if (!annotation.equals(codebaseURL.toString())) {
            throw new RuntimeException(
                "wrong class annotation, expected: " + codebaseURL.toString() +
                ", received: " + annotation);
        }

        System.err.println("TEST PASSED");
    }
 
Example 6
Source File: DefaultProperty.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        ServiceConfiguration.installServiceConfigurationFile();

        System.setProperty(
            "java.rmi.server.RMIClassLoaderSpi", "default");

        String classname = "Foo";

        URL codebaseURL = null;
        try {
            codebaseURL = TestLibrary.installClassInCodebase(
                classname, "remote_codebase");
        } catch (MalformedURLException e) {
            TestLibrary.bomb(e);
        }

        TestLibrary.suggestSecurityManager(null);

        Class fooClass = RMIClassLoader.loadClass(codebaseURL, classname);
        if (!fooClass.getName().equals(classname)) {
            throw new RuntimeException(
                "wrong class name, expected: " + classname +
                ", received: " + fooClass.getName());
        }

        String annotation = RMIClassLoader.getClassAnnotation(fooClass);
        if (!annotation.equals(codebaseURL.toString())) {
            throw new RuntimeException(
                "wrong class annotation, expected: " + codebaseURL.toString() +
                ", received: " + annotation);
        }

        System.err.println("TEST PASSED");
    }
 
Example 7
Source File: CodebaseAwareObjectInputStream.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Class<?> resolveFallbackIfPossible(String className, ClassNotFoundException ex)
		throws IOException, ClassNotFoundException {

	// If codebaseUrl is set, try to load the class with the RMIClassLoader.
	// Else, propagate the ClassNotFoundException.
	if (this.codebaseUrl == null) {
		throw ex;
	}
	return RMIClassLoader.loadClass(this.codebaseUrl, className);
}
 
Example 8
Source File: ClassLoading.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Repeatedly load a class not found in classpath through RMIClassLoader.
 * Arguments: <# reps>
 */
public long run(String[] args) throws Exception {
    int reps = Integer.parseInt(args[0]);
    CodeSource csrc = getClass().getProtectionDomain().getCodeSource();
    String url = "jar:" + csrc.getLocation().toString() + ALTROOT;

    long start = System.currentTimeMillis();
    for (int i = 0; i < reps; i++)
        RMIClassLoader.loadClass(url, CLASSNAME);
    long time = System.currentTimeMillis() - start;

    return time;
}
 
Example 9
Source File: Activation.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private static Class<?> getRMIClass(String execPolicyClassName) throws Exception  {
    return RMIClassLoader.loadClass(execPolicyClassName);
}
 
Example 10
Source File: NoSecurityManager.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    /*
     * Specify the file to contain the class definition.
     * Make sure that there is a "classes" subdirectory underneath
     * the working directory to be used as the codebase for the
     * class definition to be located.
     */
    File dstDir = new File(System.getProperty("user.dir"), "codebase");
    if (!dstDir.exists()) {
        if (!dstDir.mkdir()) {
            throw new RuntimeException(
                "could not create codebase directory");
        }
    }
    File dstFile = new File(dstDir, classFileName);

    /*
     * Specify where we will copy the class definition from, if
     * necessary.  After the test is built, the class file can be
     * found in the "test.classes" directory.
     */
    File srcDir = new File(System.getProperty("test.classes", "."));
    File srcFile = new File(srcDir, classFileName);

    /*
     * If the class definition is not already located at the codebase,
     * copy it there from the test build area.
     */
    if (!dstFile.exists()) {
        if (!srcFile.exists()) {
            throw new RuntimeException(
                "could not find class file to install in codebase " +
                "(try rebuilding the test)");
        }
        if (!srcFile.renameTo(dstFile)) {
            throw new RuntimeException(
                "could not install class file in codebase");
        }
    }

    /*
     * After the class definition is successfully installed at the
     * codebase, delete it from the test's CLASSPATH, so that it will
     * not be found there first before the codebase is searched.
     */
    if (srcFile.exists()) {
        if (!srcFile.delete()) {
            throw new RuntimeException(
                "could not delete duplicate class file in CLASSPATH");
        }
    }

    /*
     * Obtain the URL for the codebase.
     */
    URL codebaseURL = new URL("file", "",
        dstDir.getAbsolutePath().replace(File.separatorChar, '/') + "/");

    /*
     * No security manager has been set: verify that we cannot load
     * a class from a specified codebase (that is not in the class path).
     */
    try {
        RMIClassLoader.loadClass(codebaseURL, className);
        throw new RuntimeException(
            "TEST FAILED: class loaded successfully from codebase");
    } catch (ClassNotFoundException e) {
        System.err.println(e.toString());
    }

    /*
     * No security manager has been set: verify that we can still
     * load a class available in the context class loader (class path).
     */
    RMIClassLoader.loadClass(codebaseURL, "LocalDummy");
    System.err.println("TEST PASSED: local class loaded successfully");

    /*
     * Verify that getClassLoader returns context class loader
     * if no security manager is set.
     */
    System.err.println("/nTest getClassLoader with no security manager set");
    ClassLoader loader = RMIClassLoader.getClassLoader("http://codebase");
    if (loader == Thread.currentThread().getContextClassLoader()) {
        System.err.println("TEST PASSED: returned context class loader");
    } else {
        throw new RuntimeException(
            "TEST FAILED: returned RMI-created class loader");
    }
}
 
Example 11
Source File: NoSecurityManager.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    /*
     * Specify the file to contain the class definition.
     * Make sure that there is a "classes" subdirectory underneath
     * the working directory to be used as the codebase for the
     * class definition to be located.
     */
    File dstDir = new File(System.getProperty("user.dir"), "codebase");
    if (!dstDir.exists()) {
        if (!dstDir.mkdir()) {
            throw new RuntimeException(
                "could not create codebase directory");
        }
    }
    File dstFile = new File(dstDir, classFileName);

    /*
     * Specify where we will copy the class definition from, if
     * necessary.  After the test is built, the class file can be
     * found in the "test.classes" directory.
     */
    File srcDir = new File(System.getProperty("test.classes", "."));
    File srcFile = new File(srcDir, classFileName);

    /*
     * If the class definition is not already located at the codebase,
     * copy it there from the test build area.
     */
    if (!dstFile.exists()) {
        if (!srcFile.exists()) {
            throw new RuntimeException(
                "could not find class file to install in codebase " +
                "(try rebuilding the test)");
        }
        if (!srcFile.renameTo(dstFile)) {
            throw new RuntimeException(
                "could not install class file in codebase");
        }
    }

    /*
     * After the class definition is successfully installed at the
     * codebase, delete it from the test's CLASSPATH, so that it will
     * not be found there first before the codebase is searched.
     */
    if (srcFile.exists()) {
        if (!srcFile.delete()) {
            throw new RuntimeException(
                "could not delete duplicate class file in CLASSPATH");
        }
    }

    /*
     * Obtain the URL for the codebase.
     */
    URL codebaseURL = new URL("file", "",
        dstDir.getAbsolutePath().replace(File.separatorChar, '/') + "/");

    /*
     * No security manager has been set: verify that we cannot load
     * a class from a specified codebase (that is not in the class path).
     */
    try {
        RMIClassLoader.loadClass(codebaseURL, className);
        throw new RuntimeException(
            "TEST FAILED: class loaded successfully from codebase");
    } catch (ClassNotFoundException e) {
        System.err.println(e.toString());
    }

    /*
     * No security manager has been set: verify that we can still
     * load a class available in the context class loader (class path).
     */
    RMIClassLoader.loadClass(codebaseURL, "LocalDummy");
    System.err.println("TEST PASSED: local class loaded successfully");

    /*
     * Verify that getClassLoader returns context class loader
     * if no security manager is set.
     */
    System.err.println("/nTest getClassLoader with no security manager set");
    ClassLoader loader = RMIClassLoader.getClassLoader("http://codebase");
    if (loader == Thread.currentThread().getContextClassLoader()) {
        System.err.println("TEST PASSED: returned context class loader");
    } else {
        throw new RuntimeException(
            "TEST FAILED: returned RMI-created class loader");
    }
}
 
Example 12
Source File: Activation.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private static Class<?> getRMIClass(String execPolicyClassName) throws Exception  {
    return RMIClassLoader.loadClass(execPolicyClassName);
}
 
Example 13
Source File: DelegateToContextLoader.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        /*
         * Set a security manager so that RMI class loading will not
         * be disabled.
         */
        TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");


        URL codebaseURL = TestLibrary.
            installClassInCodebase(className, "codebase");

         /* Create a URLClassLoader to load from the codebase and set it
          * as this thread's context class loader.  We do not use the
          * URLClassLoader.newInstance() method so that the test will
          * compile with more early versions of the JDK.
          *
          * We can get away with creating a class loader like this
          * because there is no security manager set yet.
          */
        ClassLoader codebaseLoader =
            new URLClassLoader(new URL[] { codebaseURL } );
        Thread.currentThread().setContextClassLoader(codebaseLoader);

        File srcDir = new File(TestLibrary.getProperty("test.classes", "."));

        URL dummyURL = new URL("file", "",
            srcDir.getAbsolutePath().replace(File.separatorChar, '/') +
            "/x-files/");

        try {
            /*
             * Attempt to load the target class from the dummy URL;
             * it should be found in the context class loader.
             */
            Class cl = RMIClassLoader.loadClass(dummyURL, className);
            System.err.println("TEST PASSED: loaded class: " + cl);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(
                "TEST FAILED: target class in context class loader " +
                "not found using RMIClassLoader");
        }
    }
 
Example 14
Source File: NoSecurityManager.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    /*
     * Specify the file to contain the class definition.
     * Make sure that there is a "classes" subdirectory underneath
     * the working directory to be used as the codebase for the
     * class definition to be located.
     */
    File dstDir = new File(System.getProperty("user.dir"), "codebase");
    if (!dstDir.exists()) {
        if (!dstDir.mkdir()) {
            throw new RuntimeException(
                "could not create codebase directory");
        }
    }
    File dstFile = new File(dstDir, classFileName);

    /*
     * Specify where we will copy the class definition from, if
     * necessary.  After the test is built, the class file can be
     * found in the "test.classes" directory.
     */
    File srcDir = new File(System.getProperty("test.classes", "."));
    File srcFile = new File(srcDir, classFileName);

    /*
     * If the class definition is not already located at the codebase,
     * copy it there from the test build area.
     */
    if (!dstFile.exists()) {
        if (!srcFile.exists()) {
            throw new RuntimeException(
                "could not find class file to install in codebase " +
                "(try rebuilding the test)");
        }
        if (!srcFile.renameTo(dstFile)) {
            throw new RuntimeException(
                "could not install class file in codebase");
        }
    }

    /*
     * After the class definition is successfully installed at the
     * codebase, delete it from the test's CLASSPATH, so that it will
     * not be found there first before the codebase is searched.
     */
    if (srcFile.exists()) {
        if (!srcFile.delete()) {
            throw new RuntimeException(
                "could not delete duplicate class file in CLASSPATH");
        }
    }

    /*
     * Obtain the URL for the codebase.
     */
    URL codebaseURL = new URL("file", "",
        dstDir.getAbsolutePath().replace(File.separatorChar, '/') + "/");

    /*
     * No security manager has been set: verify that we cannot load
     * a class from a specified codebase (that is not in the class path).
     */
    try {
        RMIClassLoader.loadClass(codebaseURL, className);
        throw new RuntimeException(
            "TEST FAILED: class loaded successfully from codebase");
    } catch (ClassNotFoundException e) {
        System.err.println(e.toString());
    }

    /*
     * No security manager has been set: verify that we can still
     * load a class available in the context class loader (class path).
     */
    RMIClassLoader.loadClass(codebaseURL, "LocalDummy");
    System.err.println("TEST PASSED: local class loaded successfully");

    /*
     * Verify that getClassLoader returns context class loader
     * if no security manager is set.
     */
    System.err.println("/nTest getClassLoader with no security manager set");
    ClassLoader loader = RMIClassLoader.getClassLoader("http://codebase");
    if (loader == Thread.currentThread().getContextClassLoader()) {
        System.err.println("TEST PASSED: returned context class loader");
    } else {
        throw new RuntimeException(
            "TEST FAILED: returned RMI-created class loader");
    }
}
 
Example 15
Source File: Activation.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private static Class<?> getRMIClass(String execPolicyClassName) throws Exception  {
    return RMIClassLoader.loadClass(execPolicyClassName);
}
 
Example 16
Source File: DelegateToContextLoader.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        /*
         * Set a security manager so that RMI class loading will not
         * be disabled.
         */
        TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");


        URL codebaseURL = TestLibrary.
            installClassInCodebase(className, "codebase");

         /* Create a URLClassLoader to load from the codebase and set it
          * as this thread's context class loader.  We do not use the
          * URLClassLoader.newInstance() method so that the test will
          * compile with more early versions of the JDK.
          *
          * We can get away with creating a class loader like this
          * because there is no security manager set yet.
          */
        ClassLoader codebaseLoader =
            new URLClassLoader(new URL[] { codebaseURL } );
        Thread.currentThread().setContextClassLoader(codebaseLoader);

        File srcDir = new File(TestLibrary.getProperty("test.classes", "."));

        URL dummyURL = new URL("file", "",
            srcDir.getAbsolutePath().replace(File.separatorChar, '/') +
            "/x-files/");

        try {
            /*
             * Attempt to load the target class from the dummy URL;
             * it should be found in the context class loader.
             */
            Class cl = RMIClassLoader.loadClass(dummyURL, className);
            System.err.println("TEST PASSED: loaded class: " + cl);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(
                "TEST FAILED: target class in context class loader " +
                "not found using RMIClassLoader");
        }
    }
 
Example 17
Source File: Activation.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private static Class<?> getRMIClass(String execPolicyClassName) throws Exception  {
    return RMIClassLoader.loadClass(execPolicyClassName);
}
 
Example 18
Source File: Activation.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private static Class<?> getRMIClass(String execPolicyClassName) throws Exception  {
    return RMIClassLoader.loadClass(execPolicyClassName);
}
 
Example 19
Source File: NoSecurityManager.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    /*
     * Specify the file to contain the class definition.
     * Make sure that there is a "classes" subdirectory underneath
     * the working directory to be used as the codebase for the
     * class definition to be located.
     */
    File dstDir = new File(System.getProperty("user.dir"), "codebase");
    if (!dstDir.exists()) {
        if (!dstDir.mkdir()) {
            throw new RuntimeException(
                "could not create codebase directory");
        }
    }
    File dstFile = new File(dstDir, classFileName);

    /*
     * Specify where we will copy the class definition from, if
     * necessary.  After the test is built, the class file can be
     * found in the "test.classes" directory.
     */
    File srcDir = new File(System.getProperty("test.classes", "."));
    File srcFile = new File(srcDir, classFileName);

    /*
     * If the class definition is not already located at the codebase,
     * copy it there from the test build area.
     */
    if (!dstFile.exists()) {
        if (!srcFile.exists()) {
            throw new RuntimeException(
                "could not find class file to install in codebase " +
                "(try rebuilding the test)");
        }
        if (!srcFile.renameTo(dstFile)) {
            throw new RuntimeException(
                "could not install class file in codebase");
        }
    }

    /*
     * After the class definition is successfully installed at the
     * codebase, delete it from the test's CLASSPATH, so that it will
     * not be found there first before the codebase is searched.
     */
    if (srcFile.exists()) {
        if (!srcFile.delete()) {
            throw new RuntimeException(
                "could not delete duplicate class file in CLASSPATH");
        }
    }

    /*
     * Obtain the URL for the codebase.
     */
    URL codebaseURL = new URL("file", "",
        dstDir.getAbsolutePath().replace(File.separatorChar, '/') + "/");

    /*
     * No security manager has been set: verify that we cannot load
     * a class from a specified codebase (that is not in the class path).
     */
    try {
        RMIClassLoader.loadClass(codebaseURL, className);
        throw new RuntimeException(
            "TEST FAILED: class loaded successfully from codebase");
    } catch (ClassNotFoundException e) {
        System.err.println(e.toString());
    }

    /*
     * No security manager has been set: verify that we can still
     * load a class available in the context class loader (class path).
     */
    RMIClassLoader.loadClass(codebaseURL, "LocalDummy");
    System.err.println("TEST PASSED: local class loaded successfully");

    /*
     * Verify that getClassLoader returns context class loader
     * if no security manager is set.
     */
    System.err.println("/nTest getClassLoader with no security manager set");
    ClassLoader loader = RMIClassLoader.getClassLoader("http://codebase");
    if (loader == Thread.currentThread().getContextClassLoader()) {
        System.err.println("TEST PASSED: returned context class loader");
    } else {
        throw new RuntimeException(
            "TEST FAILED: returned RMI-created class loader");
    }
}
 
Example 20
Source File: Activation.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private static Class<?> getRMIClass(String execPolicyClassName) throws Exception  {
    return RMIClassLoader.loadClass(execPolicyClassName);
}