1 /* 2 * Copyright (c) 2003, 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 7654321 27 * @summary Tests the use of the custom RMI socket factories. 28 * @author Luis-Miguel Alventosa 29 * 30 * @run clean RMISocketFactoriesTest 31 * @run build RMISocketFactoriesTest RMIClientFactory RMIServerFactory 32 * @run main RMISocketFactoriesTest test_server_factory 33 * @run main RMISocketFactoriesTest test_client_factory 34 */ 35 36 import java.io.IOException; 37 import java.rmi.RemoteException; 38 import java.rmi.registry.Registry; 39 import java.rmi.registry.LocateRegistry; 40 import java.util.HashMap; 41 42 import javax.management.MBeanServer; 43 import javax.management.MBeanServerConnection; 44 import javax.management.MBeanServerFactory; 45 import javax.management.remote.JMXConnector; 46 import javax.management.remote.JMXConnectorFactory; 47 import javax.management.remote.JMXConnectorServer; 48 import javax.management.remote.JMXConnectorServerFactory; 49 import javax.management.remote.JMXServiceURL; 50 import javax.management.remote.rmi.RMIConnectorServer; // for constants 51 52 public class RMISocketFactoriesTest { 53 54 public static void main(String[] args) { 55 System.out.println("Test RMI factories : " + args[0]); 56 try { 57 // Create an RMI registry 58 // 59 System.out.println("Start RMI registry..."); 60 Registry reg = null; 61 int port = 5800; 62 while (port++ < 6000) { 63 try { 64 reg = LocateRegistry.createRegistry(port); 65 System.out.println("RMI registry running on port " + port); 66 break; 67 } catch (RemoteException e) { 68 // Failed to create RMI registry... 69 System.out.println("Failed to create RMI registry " + 70 "on port " + port); 71 } 72 } 73 if (reg == null) { 74 System.exit(1); 75 } 76 77 // Instantiate the MBean server 78 // 79 System.out.println("Create the MBean server"); 80 MBeanServer mbs = MBeanServerFactory.createMBeanServer(); 81 // Initialize environment map to be passed to the connector server 82 // 83 System.out.println("Initialize environment map"); 84 HashMap env = new HashMap(); 85 env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, 86 new RMIServerFactory(args[0])); 87 env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, 88 new RMIClientFactory(args[0])); 89 // Create an RMI connector server 90 // 91 System.out.println("Create an RMI connector server"); 92 JMXServiceURL url = 93 new JMXServiceURL("rmi", null, 0, 94 "/jndi/rmi://:" + port + "/server" + port); 95 JMXConnectorServer rcs = 96 JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs); 97 rcs.start(); 98 99 // Create an RMI connector client 100 // 101 System.out.println("Create an RMI connector client"); 102 JMXConnector jmxc = JMXConnectorFactory.connect(url, new HashMap()); 103 104 // If this line is executed then the test failed 105 // 106 System.exit(1); 107 } catch (Exception e) { 108 if (e.getMessage().equals(args[0])) { 109 System.out.println("Expected exception caught = " + e); 110 System.out.println("Bye! Bye!"); 111 } else { 112 System.out.println("Unexpected exception caught = " + e); 113 e.printStackTrace(); 114 System.exit(1); 115 } 116 } 117 } 118 }