1 /*
   2  * Copyright (c) 1998, 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 /**
  25  *
  26  *
  27  * Client that will run in its own vm and tell the main CheckFQDN test
  28  * program what its rmi host name is, name obtained from TCPEndpoint.
  29  */
  30 
  31 import java.rmi.*;
  32 import java.rmi.server.*;
  33 import sun.rmi.transport.tcp.TCPEndpoint;
  34 
  35 /**
  36  * Get the local endpoint: make sure that this process does
  37  * not take too much time and that the resulting localhost
  38  * string is correct for the property set on an execed vm process.
  39  */
  40 public class CheckFQDNClient implements Runnable {
  41 
  42     final static int NAME_SERVICE_TIME_OUT = 12000;
  43 
  44     static TCPEndpoint ep = null;
  45 
  46     /**
  47      * main, lookup remote object and tell it the rmi
  48      * hostname of this client vm.
  49      */
  50     public static void main (String args[]) {
  51 
  52         // start a registry and register a copy of this in it.
  53         TellServerName tell;
  54         String hostname = null;
  55 
  56         try {
  57             hostname = retrieveServerName();
  58             System.err.println("Client host name: " +
  59                                hostname);
  60 
  61             tell = (TellServerName) Naming.lookup("rmi://:" +
  62                                                   TestLibrary.REGISTRY_PORT
  63                                                   + "/CheckFQDN");
  64             tell.tellServerName(hostname);
  65             System.err.println("client has exited");
  66 
  67         } catch (Exception e ) {
  68             throw new RuntimeException(e.getMessage());
  69         }
  70         System.exit(0);
  71     }
  72 
  73     /* what is the rmi hostname for this vm? */
  74     public static String retrieveServerName () {
  75         try {
  76 
  77             CheckFQDNClient chk = new CheckFQDNClient();
  78 
  79             synchronized(chk) {
  80                 (new Thread (chk)).start();
  81                 chk.wait(NAME_SERVICE_TIME_OUT);
  82             }
  83 
  84             if (ep == null) {
  85                 throw new RuntimeException
  86                     ("Timeout getting the local endpoint.");
  87             }
  88 
  89             // this is the name used by rmi for the client hostname
  90             return ep.getHost();
  91 
  92         }catch (Exception e){
  93             throw new RuntimeException (e.getMessage());
  94         }
  95     }
  96 
  97     /* thread to geth the rmi hostname of this vm */
  98     public void run () {
  99         try {
 100             synchronized(this) {
 101                 ep = TCPEndpoint.getLocalEndpoint(0);
 102             }
 103         } catch (Exception e) {
 104             throw new RuntimeException();
 105         } finally {
 106             synchronized(this) {
 107                 this.notify();
 108             }
 109         }
 110     }
 111 }