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