1 /*
   2  * Copyright (c) 1998, 2014, 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 4099660 4102938
  26  * @summary Remote classes not extending RemoteObject should be able to
  27  *          implement hashCode() and equals() methods so that instances
  28  *          can be successfully compared to RemoteObject instances
  29  *          (specifically: stubs) that contain the instance's RemoteRef.
  30  * @author Peter Jones
  31  *
  32  * @build NotExtending_Stub NotExtending_Skel
  33  * @run main/othervm/timeout=240 NotExtending
  34  */
  35 
  36 
  37 import java.rmi.*;
  38 import java.rmi.server.*;
  39 
  40 public class NotExtending implements Remote {
  41 
  42     /** remote stub for this server instance */
  43     private Remote stub;
  44     /** value of stub's hash code */
  45     private int hashValue;
  46     /** true if the hashValue field has been initialized */
  47     private boolean hashValueInitialized = false;
  48 
  49     public NotExtending() throws RemoteException {
  50         stub = UnicastRemoteObject.exportObject(this);
  51         setHashValue(stub.hashCode());
  52     }
  53 
  54     private void setHashValue(int value) {
  55         hashValue = value;
  56         hashValueInitialized = true;
  57     }
  58 
  59     public int hashCode() {
  60         /*
  61          * Test fails with a RuntimeException if the hashCode() method is
  62          * called (during the export procedure) before the correct hash
  63          * value has been initialized.
  64          */
  65         if (!hashValueInitialized) {
  66             throw new RuntimeException(
  67                 "hashCode() invoked before hashValue initialized");
  68         }
  69         return hashValue;
  70     }
  71 
  72     public boolean equals(Object obj) {
  73         return stub.equals(obj);
  74     }
  75 
  76     public static void main(String[] args) throws Exception {
  77         /*
  78          * The following line is required with the JDK 1.2 VM so that the
  79          * VM can exit gracefully when this test completes.  Otherwise, the
  80          * conservative garbage collector will find a handle to the server
  81          * object on the native stack and not clear the weak reference to
  82          * it in the RMI runtime's object table.
  83          */
  84         Object dummy = new Object();
  85 
  86         NotExtending server;
  87         try {
  88             /*
  89              * Verify that hashCode() is not invoked before it is
  90              * initialized.  Tests bugid 4102938.
  91              */
  92             server = new NotExtending();
  93             System.err.println("Server exported without invoking hashCode().");
  94 
  95             /*
  96              * Verify that passing stub to server's equals() method
  97              * returns true.
  98              */
  99             if (server.equals(server.stub)) {
 100                 System.err.println(
 101                     "Passing stub to server's equals() method succeeded.");
 102             } else {
 103                 throw new RuntimeException(
 104                     "passing stub to server's equals() method failed");
 105             }
 106 
 107             /*
 108              * Verify that passing server to stub's equals() method
 109              * returns true.  Tests bugid 4099660.
 110              */
 111             if (server.stub.equals(server)) {
 112                 System.err.println(
 113                     "Passing server to stub's equals() method succeeded.");
 114             } else {
 115                 throw new RuntimeException(
 116                     "passing server to stub's equals() method failed");
 117             }
 118 
 119         } finally {
 120             server = null;
 121             flushCachedRefs();
 122         }
 123     }
 124 
 125     /**
 126      * Force desperate garbage collection so that soft references
 127      * will be cleared.
 128      *
 129      * This method is required with the JDK 1.1.x RMI runtime so that the
 130      * VM can exit gracefully when this test completes.  See bugid 4006356.
 131      */
 132     public static void flushCachedRefs() {
 133         java.util.Vector chain = new java.util.Vector();
 134         try {
 135             while (true) {
 136                 int[] hungry = new int[65536];
 137                 chain.addElement(hungry);
 138             }
 139         } catch (OutOfMemoryError e) {
 140         }
 141     }
 142 }