Reexport.java revision 16360:6f246db971c0
1/* 2 * Copyright (c) 1999, 2016, 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 4120329 26 * @summary RMI registry creation is impossible if first attempt fails. 27 * @library ../../testlibrary 28 * @modules java.rmi/sun.rmi.registry 29 * java.rmi/sun.rmi.server 30 * java.rmi/sun.rmi.transport 31 * java.rmi/sun.rmi.transport.tcp 32 * @build TestLibrary REGISTRY RegistryRunner 33 * @run main/othervm Reexport 34 */ 35 36/* 37 * If a VM could not create an RMI registry because another registry 38 * usually in another process, was using the registry port, the next 39 * time the VM tried to create a registry (after the other registry 40 * was brought down) the attempt would fail. The second try to create 41 * a registry would fail because the registry ObjID would still be in 42 * use when it should never have been allocated. 43 * 44 * The test creates this conflict using Runtime.exec and ensures that 45 * a registry can still be created after the conflict is resolved. 46 */ 47 48import java.io.*; 49import java.rmi.*; 50import java.rmi.registry.*; 51import java.rmi.server.*; 52 53public class Reexport { 54 static public void main(String[] argv) { 55 56 Registry reg = null; 57 try { 58 System.err.println("\nregression test for 4120329\n"); 59 60 // establish the registry (we hope) 61 makeRegistry(); 62 63 // Get a handle to the registry 64 System.err.println("Creating duplicate registry, this should fail..."); 65 reg = createReg(true); 66 67 // Kill the first registry. 68 System.err.println("Bringing down the first registry"); 69 try { 70 killRegistry(); 71 } catch (Exception foo) { 72 } 73 74 // start another registry now that the first is gone; this should work 75 System.err.println("Trying again to start our own " + 76 "registry... this should work"); 77 78 reg = createReg(false); 79 80 if (reg == null) { 81 TestLibrary.bomb("Could not create registry on second try"); 82 } 83 84 System.err.println("Test passed"); 85 86 } catch (Exception e) { 87 TestLibrary.bomb(e); 88 } finally { 89 // dont leave the registry around to affect other tests. 90 killRegistry(); 91 reg = null; 92 } 93 } 94 95 static Registry createReg(boolean remoteOk) { 96 Registry reg = null; 97 98 try { 99 reg = LocateRegistry.createRegistry(port); 100 if (remoteOk) { 101 TestLibrary.bomb("Remote registry is up, an Exception is expected!"); 102 } 103 } catch (Throwable e) { 104 if (remoteOk) { 105 System.err.println("EXPECTING PORT IN USE EXCEPTION:"); 106 System.err.println(e.getMessage()); 107 e.printStackTrace(); 108 } else { 109 TestLibrary.bomb((Exception) e); 110 } 111 } 112 return reg; 113 } 114 115 public static void makeRegistry() { 116 try { 117 subreg = REGISTRY.createREGISTRY(); 118 subreg.start(); 119 port = subreg.getPort(); 120 System.out.println("Starting registry on port " + port); 121 } catch (IOException e) { 122 // one of these is summarily dropped, can't remember which one 123 System.out.println ("Test setup failed - cannot run rmiregistry"); 124 TestLibrary.bomb("Test setup failed - cannot run test", e); 125 } 126 } 127 128 private static REGISTRY subreg = null; 129 private static int port = -1; 130 131 public static void killRegistry() { 132 if (subreg != null) { 133 subreg.shutdown(); 134 subreg = null; 135 } 136 } 137} 138