1 /* 2 * Copyright (c) 2012, 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 7120365 27 * @summary test on Concurrent Modification 28 * @author Shanliang JIANG 29 * @run main ConcurrentModificationTest 30 */ 31 32 import java.net.MalformedURLException; 33 import java.util.ConcurrentModificationException; 34 import javax.management.MBeanServer; 35 import javax.management.MBeanServerConnection; 36 import javax.management.MBeanServerFactory; 37 import javax.management.Notification; 38 import javax.management.NotificationListener; 39 import javax.management.ObjectName; 40 import javax.management.remote.JMXConnector; 41 import javax.management.remote.JMXConnectorFactory; 42 import javax.management.remote.JMXConnectorServer; 43 import javax.management.remote.JMXConnectorServerFactory; 44 import javax.management.remote.JMXServiceURL; 45 46 /** 47 * 48 */ 49 public class ConcurrentModificationTest { 50 private static final String[] protocols = {"rmi", "iiop", "jmxmp"}; 51 private static int number = 100; 52 53 private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer(); 54 private static ObjectName delegateName; 55 private static ObjectName[] timerNames = new ObjectName[number]; 56 private static NotificationListener[] listeners = new NotificationListener[number]; 57 58 private static Throwable uncaughtException = null; 59 60 public static void main(String[] args) throws Exception { 61 System.out.println(">>> test on Concurrent Modification."); 62 63 Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { 64 @Override 65 public void uncaughtException(Thread t, Throwable e) { 66 e.printStackTrace(); 67 if (e instanceof ConcurrentModificationException) { 68 uncaughtException = e; 69 } 70 } 71 }); 72 73 delegateName = new ObjectName("JMImplementation:type=MBeanServerDelegate"); 74 for (int i=0; i<number; i++) { 75 timerNames[i] = new ObjectName("MBean:name=Timer"+i); 76 listeners[i] = new NotificationListener() { 77 @Override 78 public void handleNotification(Notification notification, Object handback) { 79 // nothing 80 } 81 }; 82 } 83 String errors = ""; 84 85 for (int i = 0; i < protocols.length; i++) { 86 uncaughtException = null; 87 System.out.println(">>> Test for protocol " + protocols[i]); 88 test(protocols[i]); 89 if (uncaughtException != null) { 90 if ("".equals(errors)) { 91 errors = "Failed to " + protocols[i] + ": "+uncaughtException; 92 } else { 93 errors = errors+", failed to " + protocols[i] + ": "+uncaughtException; 94 } 95 System.out.println(">>> FAILED for protocol " + protocols[i]); 96 } else { 97 System.out.println(">>> PASSED for protocol " + protocols[i]); 98 } 99 } 100 101 if ("".equals(errors)) { 102 System.out.println("All Passed!"); 103 } else { 104 System.out.println("!!!!!! Failed."); 105 106 throw new RuntimeException(errors); 107 } 108 } 109 110 private static void test(String proto) throws Exception { 111 JMXServiceURL u = new JMXServiceURL(proto, null, 0); 112 JMXConnectorServer server; 113 JMXConnector client; 114 115 try { 116 server = 117 JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs); 118 server.start(); 119 JMXServiceURL addr = server.getAddress(); 120 client = JMXConnectorFactory.connect(addr, null); 121 } catch (MalformedURLException e) { 122 System.out.println(">>> not support: " + proto); 123 return; 124 } 125 126 final MBeanServerConnection mserver = client.getMBeanServerConnection(); 127 128 int count = 0; 129 boolean adding = true; 130 while (uncaughtException == null && count++ < 10) { 131 for (int i = 0; i < number; i++) { 132 listenerOp(mserver, listeners[i], adding); 133 mbeanOp(mserver, timerNames[i], adding); 134 } 135 adding = !adding; 136 } 137 138 if (uncaughtException != null) { // clean 139 for (int i = 0; i < number; i++) { 140 try { 141 mbeanOp(mserver, timerNames[i], false); 142 } catch (Exception e) { 143 } 144 } 145 } 146 client.close(); 147 server.stop(); 148 } 149 150 private static void mbeanOp(MBeanServerConnection mserver, ObjectName name, boolean adding) 151 throws Exception { 152 if (adding) { 153 mserver.createMBean("javax.management.timer.Timer", name); 154 } else { 155 mserver.unregisterMBean(name); 156 } 157 } 158 159 private static void listenerOp(MBeanServerConnection mserver, NotificationListener listener, boolean adding) 160 throws Exception { 161 if (adding) { 162 mserver.addNotificationListener(delegateName, listener, null, null); 163 } else { 164 mserver.removeNotificationListener(delegateName, listener); 165 } 166 } 167 }