test/java/rmi/registry/reexport/Reexport.java

Print this page




  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 final int regport = TestLibrary.REGISTRY_PORT;
  53 
  54     static public void main(String[] argv) {
  55 
  56         Registry reg = null;

  57 
  58         try {
  59             System.err.println("\nregression test for 4120329\n");
  60 
  61             // establish the registry (we hope)
  62             System.err.println("Starting registry on port " + regport);
  63             Reexport.makeRegistry(regport);
  64 
  65             // Get a handle to the registry
  66             System.err.println("Creating duplicate registry, this should fail...");
  67             reg = createReg(true);
  68 
  69             if (reg != null) {
  70                 TestLibrary.bomb("failed was able to duplicate the registry?!?");
  71             }
  72 
  73             // Kill the first registry.
  74             System.err.println("Bringing down the first registry");
  75             try {
  76                 Reexport.killRegistry();
  77             } catch (Exception foo) {
  78             }
  79 
  80             // start another registry now that the first is gone; this should work
  81             System.err.println("Trying again to start our own " +
  82                                "registry... this should work");
  83 
  84             reg = createReg(false);
  85 
  86             if (reg == null) {
  87                 TestLibrary.bomb("Could not create registry on second try");
  88             }
  89 
  90             System.err.println("Test passed");
  91 
  92         } catch (Exception e) {
  93             TestLibrary.bomb(e);
  94         } finally {
  95             // dont leave the registry around to affect other tests.
  96             killRegistry();
  97 
  98             reg = null;
  99         }
 100     }
 101 
 102     static Registry createReg(boolean remoteOk) {
 103         Registry reg = null;
 104 
 105         try {
 106             reg = LocateRegistry.createRegistry(regport);
 107         } catch (Throwable e) {
 108             if (remoteOk) {
 109                 System.err.println("EXPECTING PORT IN USE EXCEPTION:");
 110                 System.err.println(e.getMessage());
 111                 e.printStackTrace();
 112             } else {
 113                 TestLibrary.bomb((Exception) e);
 114             }
 115         }
 116 
 117         return reg;
 118     }
 119 
 120     public static void makeRegistry(int p) {
 121         // sadly, we can't kill a registry if we have too-close control
 122         // over it.  We must make it in a subprocess, and then kill the
 123         // subprocess when it has served our needs.
 124 
 125         try {
 126             JavaVM jvm = new JavaVM("RegistryRunner", "", Integer.toString(p));
 127             jvm.start();
 128             Reexport.subreg = jvm.getVM();
 129 
 130         } catch (IOException e) {
 131             // one of these is summarily dropped, can't remember which one
 132             System.out.println ("Test setup failed - cannot run rmiregistry");
 133             TestLibrary.bomb("Test setup failed - cannot run test", e);
 134         }
 135         // Slop - wait for registry to come up.  This is stupid.
 136         try {
 137             Thread.sleep (5000);
 138         } catch (Exception whatever) {
 139         }
 140     }
 141     private static Process subreg = null;
 142 
 143     public static void killRegistry() {
 144         if (Reexport.subreg != null) {
 145 
 146             RegistryRunner.requestExit();
 147 
 148             try {
 149                 Reexport.subreg.waitFor();
 150             } catch (InterruptedException ie) {
 151             }
 152         }
 153         Reexport.subreg = null;
 154     }
 155 }


  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 }