1 /* 2 * Copyright (c) 1999, 2008, 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 * @build StreamPipe TestParams TestLibrary JavaVM 29 * @build RegistryRunner RegistryRunner_Stub 30 * @build Reexport 31 * @run main/othervm Reexport 32 */ 33 34 /* 35 * If a VM could not create an RMI registry because another registry 36 * usually in another process, was using the registry port, the next 37 * time the VM tried to create a registry (after the other registry 38 * was brought down) the attempt would fail. The second try to create 39 * a registry would fail because the registry ObjID would still be in 40 * use when it should never have been allocated. 41 * 42 * The test creates this conflict using Runtime.exec and ensures that 43 * a registry can still be created after the conflict is resolved. 44 */ 45 46 import java.io.*; 47 import java.rmi.*; 48 import java.rmi.registry.*; 49 import java.rmi.server.*; 50 51 public class Reexport { 52 static public void main(String[] argv) { 53 54 Registry reg = null; 55 int regPort = TestLibrary.getUnusedRandomPort(); 56 57 try { 58 System.err.println("\nregression test for 4120329\n"); 59 60 // establish the registry (we hope) 61 System.err.println("Starting registry on port " + regPort); 62 Reexport.makeRegistry(regPort); 63 64 // Get a handle to the registry 65 System.err.println("Creating duplicate registry, this should fail..."); 66 reg = createReg(true, regPort); 67 68 if (reg != null) { 69 TestLibrary.bomb("failed was able to duplicate the registry?!?"); 70 } 71 72 // Kill the first registry. 73 System.err.println("Bringing down the first registry"); 74 try { 75 Reexport.killRegistry(regPort); 76 } catch (Exception foo) { 77 } 78 79 // start another registry now that the first is gone; this should work 80 System.err.println("Trying again to start our own " + 81 "registry... this should work"); 82 83 reg = createReg(false, regPort); 84 85 if (reg == null) { 86 TestLibrary.bomb("Could not create registry on second try"); 87 } 88 89 System.err.println("Test passed"); 90 91 } catch (Exception e) { 92 TestLibrary.bomb(e); 93 } finally { 94 // dont leave the registry around to affect other tests. 95 killRegistry(regPort); 96 97 reg = null; 98 } 99 } 100 101 static Registry createReg(boolean remoteOk, int port) { 102 Registry reg = null; 103 104 try { 105 reg = LocateRegistry.createRegistry(port); 106 } catch (Throwable e) { 107 if (remoteOk) { 108 System.err.println("EXPECTING PORT IN USE EXCEPTION:"); 109 System.err.println(e.getMessage()); 110 e.printStackTrace(); 111 } else { 112 TestLibrary.bomb((Exception) e); 113 } 114 } 115 116 return reg; 117 } 118 119 public static void makeRegistry(int p) { 120 // sadly, we can't kill a registry if we have too-close control 121 // over it. We must make it in a subprocess, and then kill the 122 // subprocess when it has served our needs. 123 124 try { 125 JavaVM jvm = new JavaVM("RegistryRunner", "", Integer.toString(p)); 126 jvm.start(); 127 Reexport.subreg = jvm.getVM(); 128 129 } catch (IOException e) { 130 // one of these is summarily dropped, can't remember which one 131 System.out.println ("Test setup failed - cannot run rmiregistry"); 132 TestLibrary.bomb("Test setup failed - cannot run test", e); 133 } 134 // Slop - wait for registry to come up. This is stupid. 135 try { 136 Thread.sleep (5000); 137 } catch (Exception whatever) { 138 } 139 } 140 private static Process subreg = null; 141 142 public static void killRegistry(int port) { 143 if (Reexport.subreg != null) { 144 145 RegistryRunner.requestExit(port); 146 147 try { 148 Reexport.subreg.waitFor(); 149 } catch (InterruptedException ie) { 150 } 151 } 152 Reexport.subreg = null; 153 } 154 }