Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/registry/reexport/Reexport.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 412032925* @summary RMI registry creation is impossible if first attempt fails.26* @library ../../testlibrary27* @build TestLibrary JavaVM RegistryRunner RegistryRunner_Stub28* @run main/othervm Reexport29*/3031/*32* If a VM could not create an RMI registry because another registry33* usually in another process, was using the registry port, the next34* time the VM tried to create a registry (after the other registry35* was brought down) the attempt would fail. The second try to create36* a registry would fail because the registry ObjID would still be in37* use when it should never have been allocated.38*39* The test creates this conflict using Runtime.exec and ensures that40* a registry can still be created after the conflict is resolved.41*/4243import java.io.*;44import java.rmi.*;45import java.rmi.registry.*;46import java.rmi.server.*;4748public class Reexport {49static public void main(String[] argv) {5051Registry reg = null;52int regPort = TestLibrary.getUnusedRandomPort();5354try {55System.err.println("\nregression test for 4120329\n");5657// establish the registry (we hope)58System.err.println("Starting registry on port " + regPort);59Reexport.makeRegistry(regPort);6061// Get a handle to the registry62System.err.println("Creating duplicate registry, this should fail...");63reg = createReg(true, regPort);6465if (reg != null) {66TestLibrary.bomb("failed was able to duplicate the registry?!?");67}6869// Kill the first registry.70System.err.println("Bringing down the first registry");71try {72Reexport.killRegistry(regPort);73} catch (Exception foo) {74}7576// start another registry now that the first is gone; this should work77System.err.println("Trying again to start our own " +78"registry... this should work");7980reg = createReg(false, regPort);8182if (reg == null) {83TestLibrary.bomb("Could not create registry on second try");84}8586System.err.println("Test passed");8788} catch (Exception e) {89TestLibrary.bomb(e);90} finally {91// dont leave the registry around to affect other tests.92killRegistry(regPort);9394reg = null;95}96}9798static Registry createReg(boolean remoteOk, int port) {99Registry reg = null;100101try {102reg = LocateRegistry.createRegistry(port);103} catch (Throwable e) {104if (remoteOk) {105System.err.println("EXPECTING PORT IN USE EXCEPTION:");106System.err.println(e.getMessage());107e.printStackTrace();108} else {109TestLibrary.bomb((Exception) e);110}111}112113return reg;114}115116public static void makeRegistry(int p) {117// sadly, we can't kill a registry if we have too-close control118// over it. We must make it in a subprocess, and then kill the119// subprocess when it has served our needs.120121try {122JavaVM jvm = new JavaVM("RegistryRunner", "", Integer.toString(p));123jvm.start();124Reexport.subreg = jvm;125} catch (IOException e) {126// one of these is summarily dropped, can't remember which one127System.out.println ("Test setup failed - cannot run rmiregistry");128TestLibrary.bomb("Test setup failed - cannot run test", e);129}130// Slop - wait for registry to come up. This is stupid.131try {132Thread.sleep (5000);133} catch (Exception whatever) {134}135}136137private static JavaVM subreg = null;138139public static void killRegistry(int port) {140if (Reexport.subreg != null) {141142RegistryRunner.requestExit(port);143144try {145Reexport.subreg.waitFor();146} catch (InterruptedException ie) {147}148}149Reexport.subreg = null;150}151}152153154