J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-1999 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @bug 4112682 |
| 26 | * @summary RMIClassLoader's loadClass() method that takes explicit URLs |
| 27 | * should load classes from a class loader that delegates to the current |
| 28 | * thread's context class loader (not just the base class loader always). |
| 29 | * @author Peter Jones |
| 30 | * |
| 31 | * @library ../../../testlibrary |
| 32 | * @build DelegateToContextLoader Dummy |
| 33 | * @run main/othervm/policy=security.policy/timeout=120 DelegateToContextLoader |
| 34 | */ |
| 35 | |
| 36 | import java.io.*; |
| 37 | import java.net.*; |
| 38 | import java.security.*; |
| 39 | import java.rmi.RMISecurityManager; |
| 40 | import java.rmi.server.RMIClassLoader; |
| 41 | |
| 42 | public class DelegateToContextLoader { |
| 43 | |
| 44 | /** name of target class to attempt to load */ |
| 45 | private static final String className = "Dummy"; |
| 46 | |
| 47 | /** name of file containing definition for target class */ |
| 48 | private static final String classFileName = className + ".class"; |
| 49 | |
| 50 | public static void main(String[] args) throws Exception { |
| 51 | |
| 52 | /* |
| 53 | * Set a security manager so that RMI class loading will not |
| 54 | * be disabled. |
| 55 | */ |
| 56 | TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager"); |
| 57 | |
| 58 | |
| 59 | URL codebaseURL = TestLibrary. |
| 60 | installClassInCodebase(className, "codebase"); |
| 61 | |
| 62 | /* Create a URLClassLoader to load from the codebase and set it |
| 63 | * as this thread's context class loader. We do not use the |
| 64 | * URLClassLoader.newInstance() method so that the test will |
| 65 | * compile with more early versions of the JDK. |
| 66 | * |
| 67 | * We can get away with creating a class loader like this |
| 68 | * because there is no security manager set yet. |
| 69 | */ |
| 70 | ClassLoader codebaseLoader = |
| 71 | new URLClassLoader(new URL[] { codebaseURL } ); |
| 72 | Thread.currentThread().setContextClassLoader(codebaseLoader); |
| 73 | |
| 74 | File srcDir = new File(TestLibrary.getProperty("test.classes", ".")); |
| 75 | |
| 76 | URL dummyURL = new URL("file", "", |
| 77 | srcDir.getAbsolutePath().replace(File.separatorChar, '/') + |
| 78 | "/x-files/"); |
| 79 | |
| 80 | try { |
| 81 | /* |
| 82 | * Attempt to load the target class from the dummy URL; |
| 83 | * it should be found in the context class loader. |
| 84 | */ |
| 85 | Class cl = RMIClassLoader.loadClass(dummyURL, className); |
| 86 | System.err.println("TEST PASSED: loaded class: " + cl); |
| 87 | } catch (ClassNotFoundException e) { |
| 88 | throw new RuntimeException( |
| 89 | "TEST FAILED: target class in context class loader " + |
| 90 | "not found using RMIClassLoader"); |
| 91 | } |
| 92 | } |
| 93 | } |