Path: blob/master/test/jdk/java/rmi/registry/classPathCodebase/ClassPathCodebase.java
41155 views
/*1* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 424231725* @summary When a class that can be found in the CLASSPATH of the rmiregistry26* tool is marshalled via RMI, it should be annotated with the value of the27* java.rmi.server.codebase property, not the list of "file:" URLs for the28* actual elements of the CLASSPATH.29* @author Peter Jones30*31* @library ../../testlibrary32* @modules java.rmi/sun.rmi.registry33* java.rmi/sun.rmi.server34* java.rmi/sun.rmi.transport35* java.rmi/sun.rmi.transport.tcp36* @build TestLibrary Dummy RegistryVM RMIRegistryRunner37* @run main/othervm/policy=security.policy38* -Djava.rmi.server.useCodebaseOnly=false ClassPathCodebase39*/4041import java.io.*;42import java.net.*;43import java.rmi.*;44import java.rmi.server.*;45import java.rmi.registry.*;46import java.util.Arrays;4748public class ClassPathCodebase {4950/** wait dozens of seconds for the registry process to be ready to call */51private static final long REGISTRY_WAIT =52(long)(10000 * TestLibrary.getTimeoutFactor());5354private final static String dummyClassName = "Dummy";5556private final static String dummyBinding = "DummyObject";5758private final static String importCodebase = "codebase_IMPORT_";59private final static String exportCodebase = "codebase_EXPORT_";6061public static void main(String[] args) {6263System.err.println("\nRegression test for bug 4242317\n");6465TestLibrary.suggestSecurityManager("java.lang.SecurityManager");6667RegistryVM rmiregistry = null;6869try {70/*71* Install a dummy class in two codebases: one that will be in72* the rmiregistry's CLASSPATH (the "import" codebase) and one73* that will be in the rmiregistry's "java.rmi.server.codebase"74* property (the "export" codebase).75*/76URL importCodebaseURL = TestLibrary.installClassInCodebase(77dummyClassName, importCodebase, false);78URL exportCodebaseURL = TestLibrary.installClassInCodebase(79dummyClassName, exportCodebase, true);8081/*82* Spawn an rmiregistry in the "import" codebase directory.83*/84File rmiregistryDir =85new File(System.getProperty("user.dir", "."), importCodebase);86rmiregistry = RegistryVM.createRegistryVMWithRunner("RMIRegistryRunner",87" -Denv.class.path=. -Djava.security.manager=allow"88+ " -Djava.rmi.server.codebase=" + exportCodebaseURL89+ " -Duser.dir=" + rmiregistryDir.getAbsolutePath());90rmiregistry.start();91int port = rmiregistry.getPort();9293/*94* Wait for the registry to initialize and be ready to call.95*/96Thread.sleep(REGISTRY_WAIT);97System.err.println();9899/*100* Create an instance of the dummy class, finding it from the101* "import" codebase.102*/103ClassLoader loader = URLClassLoader.newInstance(104new URL[] { importCodebaseURL });105Class dummyClass = Class.forName(dummyClassName, false, loader);106Remote dummyObject = (Remote) dummyClass.newInstance();107108/*109* Find the registry that we created and bind the110* dummy object to it.111*/112Registry registry = LocateRegistry.getRegistry(113"localhost", port);114115try {116registry.bind(dummyBinding, dummyObject);117System.err.println("Bound dummy object in registry");118} catch (java.rmi.ConnectException e) {119System.err.println("Error: rmiregistry not started in time");120throw e;121} catch (ServerException e) {122if (e.detail instanceof UnmarshalException &&123((UnmarshalException) e.detail).detail instanceof124ClassNotFoundException)125{126System.err.println(127"Error: another registry running on port " +128port + "?");129}130throw e;131}132133/*134* Look up the dummy object from our registry and make sure135* that its class was annotated with the "export" codebase.136*/137Remote dummyLookup = registry.lookup(dummyBinding);138System.err.println(139"Looked up dummy object from registry: " + dummyLookup);140Class dummyLookupClass = dummyLookup.getClass();141String dummyLookupAnnotation =142RMIClassLoader.getClassAnnotation(dummyLookupClass);143System.err.println(144"Class annotation from registry: " + dummyLookupAnnotation);145146System.err.println();147if (dummyLookupAnnotation.indexOf(exportCodebase) >= 0) {148System.err.println("TEST PASSED");149} else if (dummyLookupAnnotation.indexOf(importCodebase) >= 0) {150throw new RuntimeException(151"rmiregistry annotated with CLASSPATH element URL");152} else {153throw new RuntimeException(154"rmiregistry used unexpected annotation: \"" +155dummyLookupAnnotation + "\"");156}157158} catch (Exception e) {159e.printStackTrace();160throw new RuntimeException("TEST FAILED: " + e.toString());161} finally {162if (rmiregistry != null) {163rmiregistry.cleanup();164}165}166}167}168169170