DelegateToContextLoader.java revision 12057:5d60882157c9
1/* 2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * 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 * @modules java.rmi/sun.rmi.registry 33 * java.rmi/sun.rmi.server 34 * java.rmi/sun.rmi.transport 35 * java.rmi/sun.rmi.transport.tcp 36 * @build TestLibrary Dummy 37 * @run main/othervm/policy=security.policy/timeout=120 DelegateToContextLoader 38 */ 39 40import java.io.*; 41import java.net.*; 42import java.security.*; 43import java.rmi.RMISecurityManager; 44import java.rmi.server.RMIClassLoader; 45 46public class DelegateToContextLoader { 47 48 /** name of target class to attempt to load */ 49 private static final String className = "Dummy"; 50 51 /** name of file containing definition for target class */ 52 private static final String classFileName = className + ".class"; 53 54 public static void main(String[] args) throws Exception { 55 56 /* 57 * Set a security manager so that RMI class loading will not 58 * be disabled. 59 */ 60 TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager"); 61 62 63 URL codebaseURL = TestLibrary. 64 installClassInCodebase(className, "codebase"); 65 66 /* Create a URLClassLoader to load from the codebase and set it 67 * as this thread's context class loader. We do not use the 68 * URLClassLoader.newInstance() method so that the test will 69 * compile with more early versions of the JDK. 70 * 71 * We can get away with creating a class loader like this 72 * because there is no security manager set yet. 73 */ 74 ClassLoader codebaseLoader = 75 new URLClassLoader(new URL[] { codebaseURL } ); 76 Thread.currentThread().setContextClassLoader(codebaseLoader); 77 78 File srcDir = new File(TestLibrary.getProperty("test.classes", ".")); 79 80 URL dummyURL = new URL("file", "", 81 srcDir.getAbsolutePath().replace(File.separatorChar, '/') + 82 "/x-files/"); 83 84 try { 85 /* 86 * Attempt to load the target class from the dummy URL; 87 * it should be found in the context class loader. 88 */ 89 Class cl = RMIClassLoader.loadClass(dummyURL, className); 90 System.err.println("TEST PASSED: loaded class: " + cl); 91 } catch (ClassNotFoundException e) { 92 throw new RuntimeException( 93 "TEST FAILED: target class in context class loader " + 94 "not found using RMIClassLoader"); 95 } 96 } 97} 98