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