1 /*
   2  * Copyright (c) 2013, 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 import java.lang.management.ManagementFactory;
  25 import java.lang.ref.WeakReference;
  26 import java.lang.reflect.Field;
  27 import java.util.Collections;
  28 import java.util.Map;
  29 import javax.management.MBeanServer;
  30 import javax.management.MBeanServerConnection;
  31 import javax.management.remote.JMXConnector;
  32 import javax.management.remote.JMXConnectorFactory;
  33 import javax.management.remote.JMXConnectorServer;
  34 import javax.management.remote.JMXConnectorServerFactory;
  35 import javax.management.remote.JMXPrincipal;
  36 import javax.management.remote.JMXServiceURL;
  37 import javax.management.remote.rmi.RMIConnector;
  38 import javax.security.auth.Subject;
  39 
  40 /*
  41  * @test
  42  * @bug 6566891
  43  * @summary Check no memory leak on RMIConnector's rmbscMap
  44  * @author Shanliang JIANG
  45  * @run clean RMIConnectorInternalMapTest
  46  * @run build RMIConnectorInternalMapTest
  47  * @run main RMIConnectorInternalMapTest
  48  */
  49 
  50 public class RMIConnectorInternalMapTest {
  51     public static void main(String[] args) throws Exception {
  52         System.out.println("---RMIConnectorInternalMapTest starting...");
  53 
  54         JMXConnectorServer connectorServer = null;
  55         JMXConnector connectorClient = null;
  56 
  57         try {
  58             MBeanServer mserver = ManagementFactory.getPlatformMBeanServer();
  59             JMXServiceURL serverURL = new JMXServiceURL("rmi", "localhost", 0);
  60             connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(serverURL, null, mserver);
  61             connectorServer.start();
  62 
  63             JMXServiceURL serverAddr = connectorServer.getAddress();
  64             connectorClient = JMXConnectorFactory.connect(serverAddr, null);
  65             connectorClient.connect();
  66 
  67             Field rmbscMapField = RMIConnector.class.getDeclaredField("rmbscMap");
  68             rmbscMapField.setAccessible(true);
  69             Map<Subject, WeakReference<MBeanServerConnection>> map =
  70                     (Map<Subject, WeakReference<MBeanServerConnection>>) rmbscMapField.get(connectorClient);
  71             if (map != null && !map.isEmpty()) { // failed
  72                 throw new RuntimeException("RMIConnector's rmbscMap must be empty at the initial time.");
  73             }
  74 
  75             Subject delegationSubject =
  76                     new Subject(true,
  77                     Collections.singleton(new JMXPrincipal("delegate")),
  78                     Collections.EMPTY_SET,
  79                     Collections.EMPTY_SET);
  80             MBeanServerConnection mbsc1 =
  81                     connectorClient.getMBeanServerConnection(delegationSubject);
  82             MBeanServerConnection mbsc2 =
  83                     connectorClient.getMBeanServerConnection(delegationSubject);
  84 
  85             if (mbsc1 == null) {
  86                 throw new RuntimeException("Got null connection.");
  87             }
  88             if (mbsc1 != mbsc2) {
  89                 throw new RuntimeException("Not got same connection with a same subject.");
  90             }
  91 
  92             map = (Map<Subject, WeakReference<MBeanServerConnection>>) rmbscMapField.get(connectorClient);
  93             if (map == null || map.isEmpty()) { // failed
  94                 throw new RuntimeException("RMIConnector's rmbscMap has wrong size "
  95                         + "after creating a delegated connection.");
  96             }
  97 
  98             delegationSubject = null;
  99             mbsc1 = null;
 100             mbsc2 = null;
 101 
 102             int i = 0;
 103             while (!map.isEmpty() && i++ < 60) {
 104                 System.gc();
 105                 Thread.sleep(100);
 106             }
 107             System.out.println("---GC times: " + i);
 108 
 109             if (!map.isEmpty()) {
 110                 throw new RuntimeException("Failed to clean RMIConnector's rmbscMap");
 111             } else {
 112                 System.out.println("---RMIConnectorInternalMapTest: PASSED!");
 113             }
 114         } finally {
 115             try {
 116                 connectorClient.close();
 117                 connectorServer.stop();
 118             } catch (Exception e) {
 119             }
 120         }
 121     }
 122 }