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