1 /*
   2  * Copyright (c) 1998, 2012, 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             int registryPort = Integer.parseInt(System.getProperty("rmi.registry.port"));
  62             tell = (TellServerName) Naming.lookup("rmi://:" +
  63                                                   registryPort
  64                                                   + "/CheckFQDN");
  65             tell.tellServerName(hostname);
  66             System.err.println("client has exited");
  67 
  68         } catch (Exception e ) {
  69             throw new RuntimeException(e.getMessage());
  70         }
  71         System.exit(0);
  72     }
  73 
  74     /* what is the rmi hostname for this vm? */
  75     public static String retrieveServerName () {
  76         try {
  77 
  78             CheckFQDNClient chk = new CheckFQDNClient();
  79 
  80             synchronized(chk) {
  81                 (new Thread (chk)).start();
  82                 chk.wait(NAME_SERVICE_TIME_OUT);
  83             }
  84 
  85             if (ep == null) {
  86                 throw new RuntimeException
  87                     ("Timeout getting the local endpoint.");
  88             }
  89 
  90             // this is the name used by rmi for the client hostname
  91             return ep.getHost();
  92 
  93         }catch (Exception e){
  94             throw new RuntimeException (e.getMessage());
  95         }
  96     }
  97 
  98     /* thread to geth the rmi hostname of this vm */
  99     public void run () {
 100         try {
 101             synchronized(this) {
 102                 ep = TCPEndpoint.getLocalEndpoint(0);
 103             }
 104         } catch (Exception e) {
 105             throw new RuntimeException();
 106         } finally {
 107             synchronized(this) {
 108                 this.notify();
 109             }
 110         }
 111     }
 112 }