1 /*
   2  * Copyright (c) 2001, 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 4308492
  26  * @summary In addition to keeping the VM alive (with a non-daeman thread)
  27  * while there are remote objects exported, the RMI runtime should also
  28  * keep it alive while there remain calls in progress (to remote objects
  29  * the have presumably been unexported), so that a remote object can more
  30  * conveniently implement a graceful remote shutdown method (that unexports
  31  * the object).
  32  * @author Peter Jones
  33  *
  34  * @library ../../../testlibrary
  35  * @build TestLibrary
  36  * @build JavaVM
  37  * @build KeepAliveDuringCall
  38  * @build KeepAliveDuringCall_Stub
  39  * @build ShutdownMonitor
  40  * @build Shutdown
  41  * @build ShutdownImpl
  42  * @build ShutdownImpl_Stub
  43  * @run main/othervm KeepAliveDuringCall
  44  */
  45 
  46 import java.rmi.Remote;
  47 import java.rmi.RemoteException;
  48 import java.rmi.registry.LocateRegistry;
  49 import java.rmi.registry.Registry;
  50 import java.rmi.server.UnicastRemoteObject;
  51 
  52 public class KeepAliveDuringCall implements ShutdownMonitor {
  53 
  54     public static final String BINDING = "KeepAliveDuringCall";
  55     private static final int TIMEOUT = 20000;
  56 
  57     private Object lock = new Object();
  58     private Shutdown shutdown = null;
  59     private boolean stillAlive = false;
  60 
  61     public void submitShutdown(Shutdown shutdown) {
  62         synchronized (lock) {
  63             this.shutdown = shutdown;
  64             lock.notifyAll();
  65         }
  66     }
  67 
  68     public void declareStillAlive() {
  69         synchronized (lock) {
  70             stillAlive = true;
  71             lock.notifyAll();
  72         }
  73     }
  74 
  75     public static void main(String[] args) {
  76 
  77         System.err.println("\nRegression test for bug 4308492\n");
  78 
  79         KeepAliveDuringCall obj = new KeepAliveDuringCall();
  80 
  81         try {
  82             UnicastRemoteObject.exportObject(obj);
  83             System.err.println("exported shutdown monitor");
  84 
  85             Registry localRegistry =
  86                 LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
  87             System.err.println("created local registry");
  88 
  89             localRegistry.bind(BINDING, obj);
  90             System.err.println("bound shutdown monitor in local registry");
  91 
  92             System.err.println("starting remote ShutdownImpl VM...");
  93             (new JavaVM("ShutdownImpl")).start();
  94 
  95             Shutdown s;
  96             synchronized (obj.lock) {
  97                 System.err.println(
  98                     "waiting for submission of object to shutdown...");
  99                 while ((s = obj.shutdown) == null) {
 100                     obj.lock.wait(TIMEOUT);
 101                 }
 102                 if (s == null) {
 103                     throw new RuntimeException(
 104                         "TEST FAILED: timeout waiting for shutdown object " +
 105                         "to make initial contact");
 106                 }
 107                 System.err.println("shutdown object submitted: " + s);
 108             }
 109 
 110             try {
 111                 s.shutdown();
 112             } catch (RemoteException e) {
 113                 throw new RuntimeException(
 114                     "TEST FAILED: shutdown method threw remote exception", e);
 115             }
 116 
 117             synchronized (obj.lock) {
 118                 if (!obj.stillAlive) {
 119                     throw new RuntimeException("TEST FAILED: " +
 120                         "shutdown object not detected alive after unexport");
 121                 }
 122             }
 123 
 124             System.err.println("TEST PASSED: " +
 125                 "shutdown object detected still alive after unexport");
 126 
 127         } catch (Exception e) {
 128             if (e instanceof RuntimeException) {
 129                 throw (RuntimeException) e;
 130             } else {
 131                 throw new RuntimeException(
 132                     "TEST FAILED: unexpected exception", e);
 133             }
 134         } finally {
 135             try {
 136                 UnicastRemoteObject.unexportObject(obj, true);
 137             } catch (RemoteException e) {
 138             }
 139         }
 140     }
 141 }