jdk/test/java/rmi/registry/reexport/Reexport.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 309 bda219d843f6
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/* 
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4120329
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary RMI registry creation is impossible if first attempt fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @library ../../testlibrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @build StreamPipe TestParams TestLibrary JavaVM 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @build RegistryRunner RegistryRunner_Stub
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @build Reexport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main/othervm Reexport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/* 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * If a VM could not create an RMI registry because another registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * usually in another process, was using the registry port, the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * time the VM tried to create a registry (after the other registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * was brought down) the attempt would fail.  The second try to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * a registry would fail because the registry ObjID would still be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * use when it should never have been allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The test creates this conflict using Runtime.exec and ensures that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * a registry can still be created after the conflict is resolved.  
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.rmi.registry.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.rmi.server.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class Reexport {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static public final int regport = TestLibrary.REGISTRY_PORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static public void main(String[] argv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
	Registry reg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
	    System.err.println("\nregression test for 4120329\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
	    // establish the registry (we hope)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
	    System.err.println("Starting registry on port " + regport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
	    Reexport.makeRegistry(regport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
	    // Get a handle to the registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
	    System.err.println("Creating duplicate registry, this should fail...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
	    reg = createReg(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
	    if (reg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
		TestLibrary.bomb("failed was able to duplicate the registry?!?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
	    // Kill the first registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
	    System.err.println("Bringing down the first registry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
	    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
		Reexport.killRegistry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
	    } catch (Exception foo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
	    // start another registry now that the first is gone; this should work
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
	    System.err.println("Trying again to start our own " + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
			       "registry... this should work");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
	    reg = createReg(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
	    if (reg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
		TestLibrary.bomb("Could not create registry on second try");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
	    System.err.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
	} catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
	    TestLibrary.bomb(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
	} finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
	    // dont leave the registry around to affect other tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
	    killRegistry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
	    reg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static Registry createReg(boolean remoteOk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
	Registry reg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
	    reg = LocateRegistry.createRegistry(regport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
	} catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
	    if (remoteOk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
		System.err.println("EXPECTING PORT IN USE EXCEPTION:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
		System.err.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
		e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
	    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
		TestLibrary.bomb((Exception) e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
	return reg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static void makeRegistry(int p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
	// sadly, we can't kill a registry if we have too-close control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
	// over it.  We must make it in a subprocess, and then kill the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
	// subprocess when it has served our needs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
	    JavaVM jvm = new JavaVM("RegistryRunner", "", Integer.toString(p));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
	    jvm.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
	    Reexport.subreg = jvm.getVM();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
	} catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
	    // one of these is summarily dropped, can't remember which one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
	    System.out.println ("Test setup failed - cannot run rmiregistry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
	    TestLibrary.bomb("Test setup failed - cannot run test", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
	// Slop - wait for registry to come up.  This is stupid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
	    Thread.sleep (5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
	} catch (Exception whatever) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private static Process subreg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public static void killRegistry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
	if (Reexport.subreg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
	    RegistryRunner.requestExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
	    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
		Reexport.subreg.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
	    } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
	Reexport.subreg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
}