Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/registry/classPathCodebase/ClassPathCodebase.java
38828 views
/*1* Copyright (c) 1999, 2013, 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* @build TestLibrary Dummy33* @run main/othervm/policy=security.policy34* -Djava.rmi.server.useCodebaseOnly=false ClassPathCodebase35*/3637import java.io.*;38import java.net.*;39import java.rmi.*;40import java.rmi.server.*;41import java.rmi.registry.*;42import java.util.Arrays;4344public class ClassPathCodebase {4546/** wait 10 seconds for the registry process to be ready to call */47private final static long REGISTRY_WAIT = 15000;4849private final static String dummyClassName = "Dummy";5051private final static String dummyBinding = "DummyObject";5253private final static String importCodebase = "codebase_IMPORT_";54private final static String exportCodebase = "codebase_EXPORT_";5556public static void main(String[] args) {5758System.err.println("\nRegression test for bug 4242317\n");5960TestLibrary.suggestSecurityManager("java.lang.SecurityManager");6162Process rmiregistry = null;6364try {65/*66* Install a dummy class in two codebases: one that will be in67* the rmiregistry's CLASSPATH (the "import" codebase) and one68* that will be in the rmiregistry's "java.rmi.server.codebase"69* property (the "export" codebase).70*/71URL importCodebaseURL = TestLibrary.installClassInCodebase(72dummyClassName, importCodebase, false);73URL exportCodebaseURL = TestLibrary.installClassInCodebase(74dummyClassName, exportCodebase, true);7576/*77* Spawn an rmiregistry in the "import" codebase directory.78*/79File rmiregistryDir =80new File(System.getProperty("user.dir", "."), importCodebase);8182String rmiregistryCommand =83System.getProperty("java.home") + File.separator +84"bin" + File.separator + "rmiregistry";8586int port = TestLibrary.getUnusedRandomPort();87String cmdarray[] = new String[] {88rmiregistryCommand,89"-J-Denv.class.path=.",90"-J-Djava.rmi.server.codebase=" + exportCodebaseURL,91Integer.toString(port) };9293System.err.println("\nCommand used to spawn rmiregistry process:");94System.err.println("\t" + Arrays.asList(cmdarray).toString());9596rmiregistry = Runtime.getRuntime().exec(cmdarray, null, rmiregistryDir);9798// pipe rmiregistry output to our output, for debugging failures99StreamPipe.plugTogether(rmiregistry.getInputStream(), System.err);100StreamPipe.plugTogether(rmiregistry.getErrorStream(), System.err);101102/*103* Wait for the registry to initialize and be ready to call.104*/105Thread.sleep(REGISTRY_WAIT);106System.err.println();107108/*109* Create an instance of the dummy class, finding it from the110* "import" codebase.111*/112ClassLoader loader = URLClassLoader.newInstance(113new URL[] { importCodebaseURL });114Class dummyClass = Class.forName(dummyClassName, false, loader);115Remote dummyObject = (Remote) dummyClass.newInstance();116117/*118* Find the registry that we created and bind the119* dummy object to it.120*/121Registry registry = LocateRegistry.getRegistry(122"localhost", port);123124try {125registry.bind(dummyBinding, dummyObject);126System.err.println("Bound dummy object in registry");127} catch (java.rmi.ConnectException e) {128System.err.println("Error: rmiregistry not started in time");129throw e;130} catch (ServerException e) {131if (e.detail instanceof UnmarshalException &&132((UnmarshalException) e.detail).detail instanceof133ClassNotFoundException)134{135System.err.println(136"Error: another registry running on port " +137port + "?");138}139throw e;140}141142/*143* Look up the dummy object from our registry and make sure144* that its class was annotated with the "export" codebase.145*/146Remote dummyLookup = registry.lookup(dummyBinding);147System.err.println(148"Looked up dummy object from registry: " + dummyLookup);149Class dummyLookupClass = dummyLookup.getClass();150String dummyLookupAnnotation =151RMIClassLoader.getClassAnnotation(dummyLookupClass);152System.err.println(153"Class annotation from registry: " + dummyLookupAnnotation);154155System.err.println();156if (dummyLookupAnnotation.indexOf(exportCodebase) >= 0) {157System.err.println("TEST PASSED");158} else if (dummyLookupAnnotation.indexOf(importCodebase) >= 0) {159throw new RuntimeException(160"rmiregistry annotated with CLASSPATH element URL");161} else {162throw new RuntimeException(163"rmiregistry used unexpected annotation: \"" +164dummyLookupAnnotation + "\"");165}166167} catch (Exception e) {168e.printStackTrace();169throw new RuntimeException("TEST FAILED: " + e.toString());170} finally {171if (rmiregistry != null) {172rmiregistry.destroy();173}174}175}176}177178179