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