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