1 /*
   2  * Copyright (c) 2004, 2015, 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  * @test
  26  * @bug 5016705
  27  * @summary Tests the use of the RMIExporter class.
  28  * @author Luis-Miguel Alventosa
  29  * @modules java.management/com.sun.jmx.remote.internal
  30  * @run clean RMIExporterTest
  31  * @run build RMIExporterTest
  32  * @run main RMIExporterTest
  33  */
  34 
  35 import java.rmi.NoSuchObjectException;
  36 import java.rmi.Remote;
  37 import java.rmi.RemoteException;
  38 import java.rmi.server.RMIClientSocketFactory;
  39 import java.rmi.server.RMIServerSocketFactory;
  40 import java.rmi.server.UnicastRemoteObject;
  41 import java.util.HashMap;
  42 import javax.management.MBeanServer;
  43 import javax.management.MBeanServerFactory;
  44 import javax.management.remote.JMXConnector;
  45 import javax.management.remote.JMXConnectorFactory;
  46 import javax.management.remote.JMXConnectorServer;
  47 import javax.management.remote.JMXConnectorServerFactory;
  48 import javax.management.remote.JMXServiceURL;
  49 import com.sun.jmx.remote.internal.RMIExporter;
  50 
  51 public class RMIExporterTest {
  52 
  53     public static class CustomRMIExporter implements RMIExporter {
  54 
  55         public boolean rmiServerExported = false;
  56         public boolean rmiServerUnexported = false;
  57         public boolean rmiConnectionExported = false;
  58         public boolean rmiConnectionUnexported = false;
  59 
  60         public Remote exportObject(Remote obj,
  61                                    int port,
  62                                    RMIClientSocketFactory csf,
  63                                    RMIServerSocketFactory ssf)
  64             throws RemoteException {
  65             System.out.println("CustomRMIExporter::exportObject():: " +
  66                                "Remote = " + obj);
  67             if (obj.toString().startsWith(
  68                     "javax.management.remote.rmi.RMIJRMPServerImpl"))
  69                 rmiServerExported = true;
  70             if (obj.toString().startsWith(
  71                     "javax.management.remote.rmi.RMIConnectionImpl"))
  72                 rmiConnectionExported = true;
  73             return UnicastRemoteObject.exportObject(obj, port, csf, ssf);
  74         }
  75 
  76         public boolean unexportObject(Remote obj, boolean force)
  77             throws NoSuchObjectException {
  78             System.out.println("CustomRMIExporter::unexportObject():: " +
  79                                "Remote = " + obj);
  80             if (obj.toString().startsWith(
  81                     "javax.management.remote.rmi.RMIJRMPServerImpl"))
  82                 rmiServerUnexported = true;
  83             if (obj.toString().startsWith(
  84                     "javax.management.remote.rmi.RMIConnectionImpl"))
  85                 rmiConnectionUnexported = true;
  86             return UnicastRemoteObject.unexportObject(obj, force);
  87         }
  88     }
  89 
  90     public static void main(String[] args) {
  91 
  92         try {
  93             // Instantiate the MBean server
  94             //
  95             System.out.println("Create the MBean server");
  96             MBeanServer mbs = MBeanServerFactory.createMBeanServer();
  97 
  98             // Initialize environment map to be passed to the connector server
  99             //
 100             System.out.println("Initialize environment map");
 101             HashMap env = new HashMap();
 102             CustomRMIExporter exporter = new CustomRMIExporter();
 103             env.put(RMIExporter.EXPORTER_ATTRIBUTE, exporter);
 104 
 105             // Create an RMI connector server
 106             //
 107             System.out.println("Create an RMI connector server");
 108             JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
 109             JMXConnectorServer cs =
 110                 JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
 111             cs.start();
 112 
 113             // Create an RMI connector client
 114             //
 115             System.out.println("Create an RMI connector client");
 116             JMXConnector cc =
 117                 JMXConnectorFactory.connect(cs.getAddress(), null);
 118 
 119             // Close RMI connector client
 120             //
 121             System.out.println("Close the RMI connector client");
 122             cc.close();
 123 
 124             // Stop RMI connector server
 125             //
 126             System.out.println("Stop the RMI connector server");
 127             cs.stop();
 128 
 129             // Check if remote objects were exported/unexported successfully
 130             //
 131             int errorCount = 0;
 132 
 133             if (exporter.rmiServerExported) {
 134                 System.out.println("RMIServer exported OK!");
 135             } else {
 136                 System.out.println("RMIServer exported KO!");
 137                 errorCount++;
 138             }
 139 
 140             if (exporter.rmiServerUnexported) {
 141                 System.out.println("RMIServer unexported OK!");
 142             } else {
 143                 System.out.println("RMIServer unexported KO!");
 144                 errorCount++;
 145             }
 146 
 147             if (exporter.rmiConnectionExported) {
 148                 System.out.println("RMIConnection exported OK!");
 149             } else {
 150                 System.out.println("RMIConnection exported KO!");
 151                 errorCount++;
 152             }
 153 
 154             if (exporter.rmiConnectionUnexported) {
 155                 System.out.println("RMIConnection unexported OK!");
 156             } else {
 157                 System.out.println("RMIConnection unexported KO!");
 158                 errorCount++;
 159             }
 160 
 161             System.out.println("Bye! Bye!");
 162 
 163             if (errorCount > 0) {
 164                 System.out.println("RMIExporterTest FAILED!");
 165                 System.exit(1);
 166             } else {
 167                 System.out.println("RMIExporterTest PASSED!");
 168             }
 169         } catch (Exception e) {
 170             System.out.println("Unexpected exception caught = " + e);
 171             e.printStackTrace();
 172             System.exit(1);
 173         }
 174     }
 175 }