jdk/test/javax/management/MBeanInfo/NotificationInfoTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1155 a9a142fcf1b5
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 5012634
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that JMX classes use fully-qualified class names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * in MBeanNotificationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Eamonn McManus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean NotificationInfoTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build NotificationInfoTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main NotificationInfoTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.modelmbean.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.relation.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This test finds all classes in the same code-base as the JMX
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * classes that look like Standard MBeans, and checks that if they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * NotificationBroadcasters they declare existent notification types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * A class looks like a Standard MBean if both Thing and ThingMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * classes exist.  So for example javax.management.timer.Timer looks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * like a Standard MBean because javax.management.timer.TimerMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * exists.  Timer is instanceof NotificationBroadcaster, so we expect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * that ((NotificationBroadcaster) timer).getNotificationInfo() will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * return an array of MBeanNotificationInfo where each entry has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * getName() that names an existent Java class that is a Notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * An MBean is "suspicious" if it is a NotificationBroadcaster but its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * MBeanNotificationInfo[] is empty.  This is legal, but surprising.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * In order to call getNotificationInfo(), we need an instance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * class.  We attempt to make one by calling a public no-arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * constructor.  But the "construct" method below can be extended to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * construct specific MBean classes for which the no-arg constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * doesn't exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * The test is obviously not exhaustive, but does catch the cases that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * failed in 5012634.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
public class NotificationInfoTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // class or object names where the test failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final Set/*<String>*/ failed = new TreeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // class or object names where there were no MBeanNotificationInfo entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final Set/*<String>*/ suspicious = new TreeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.out.println("Checking that all known MBeans that are " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                           "NotificationBroadcasters have sane " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                           "MBeanInfo.getNotifications()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        System.out.println("Checking platform MBeans...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        checkPlatformMBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        CodeSource cs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            javax.management.MBeanServer.class.getProtectionDomain()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            .getCodeSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        URL codeBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (cs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            codeBase = new URL("file:" + System.getProperty("java.home") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                               "/lib/rt.jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            codeBase = cs.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.out.println("Looking for standard MBeans...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        String[] classes = findStandardMBeans(codeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        System.out.println("Testing standard MBeans...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (int i = 0; i < classes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            String name = classes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            Class c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                c = Class.forName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                System.out.println(name + ": cannot load (not public?): " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (!NotificationBroadcaster.class.isAssignableFrom(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                System.out.println(name + ": not a NotificationBroadcaster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            NotificationBroadcaster mbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            Constructor constr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                constr = c.getConstructor(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                System.out.println(name + ": no public no-arg constructor: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                   + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                mbean = (NotificationBroadcaster) constr.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                System.out.println(name + ": no-arg constructor failed: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            check(mbean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        System.out.println("Testing some explicit cases...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        check(new RelationService(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
          We can't do this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            check(new RequiredModelMBean());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
          because the Model MBean spec more or less forces us to use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
          names GENERIC and ATTRIBUTE_CHANGE for its standard notifs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        checkRMIConnectorServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (!suspicious.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            System.out.println("SUSPICIOUS CLASSES: " + suspicious);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (failed.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            System.out.println("TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            System.out.println("TEST FAILED: " + failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static void check(NotificationBroadcaster mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        System.out.print(mbean.getClass().getName() + ": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        check(mbean.getClass().getName(), mbean.getNotificationInfo());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static void checkPlatformMBeans() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        Class managementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            managementFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                Class.forName("java.lang.management.ManagementFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            System.out.println("...no ManagementFactory, assuming pre-Tiger: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                               + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        Method getPlatformMBeanServer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            managementFactory.getMethod("getPlatformMBeanServer", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        MBeanServer mbs = (MBeanServer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            getPlatformMBeanServer.invoke(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        Set mbeanNames = mbs.queryNames(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        for (Iterator it = mbeanNames.iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            ObjectName name = (ObjectName) it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (!mbs.isInstanceOf(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                  NotificationBroadcaster.class.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                System.out.println(name + ": not a NotificationBroadcaster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                MBeanInfo mbi = mbs.getMBeanInfo(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                check(name.toString(), mbi.getNotifications());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private static void checkRMIConnectorServer() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        Class rmiConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            rmiConnectorServer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                Class.forName("javax.management.remote.rmi.RMIConnectorServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            System.out.println("No RMIConnectorServer class, skipping: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        Class jmxServiceURL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            Class.forName("javax.management.remote.JMXServiceURL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        Constructor jmxServiceURLConstructor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            jmxServiceURL.getConstructor(new Class[] {String.class});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Object url =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            jmxServiceURLConstructor.newInstance(new Object[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                "service:jmx:rmi://"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        Constructor rmiConnectorServerConstructor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            rmiConnectorServer.getConstructor(new Class[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                jmxServiceURL, Map.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        Object connector =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            rmiConnectorServerConstructor.newInstance(new Object[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                url, null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        check((NotificationBroadcaster) connector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private static void check(String what, MBeanNotificationInfo[] mbnis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        System.out.print(what + ": checking notification info: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (mbnis.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            System.out.println("NONE (suspicious)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            suspicious.add(what);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // Each MBeanNotificationInfo.getName() should be an existent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // Java class that is Notification or a subclass of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        for (int j = 0; j < mbnis.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            String notifClassName = mbnis[j].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                Class notifClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    notifClass = Class.forName(notifClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    System.out.print("FAILED(" + notifClassName + ": " + e +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                     ") ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    failed.add(what);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                if (!Notification.class.isAssignableFrom(notifClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    System.out.print("FAILED(" + notifClassName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                     ": not a Notification) ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    failed.add(what);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                System.out.print("OK(" + notifClassName + ") ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private static String[] findStandardMBeans(URL codeBase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        Set names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (codeBase.getProtocol().equalsIgnoreCase("file")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            && codeBase.toString().endsWith("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            names = findStandardMBeansFromDir(codeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            names = findStandardMBeansFromJar(codeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        Set standardMBeanNames = new TreeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        for (Iterator it = names.iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            String name = (String) it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (name.endsWith("MBean")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                String prefix = name.substring(0, name.length() - 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                if (names.contains(prefix))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    standardMBeanNames.add(prefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return (String[]) standardMBeanNames.toArray(new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private static Set findStandardMBeansFromJar(URL codeBase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        InputStream is = codeBase.openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        JarInputStream jis = new JarInputStream(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        Set names = new TreeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        JarEntry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        while ((entry = jis.getNextJarEntry()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            String name = entry.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (!name.endsWith(".class"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            name = name.substring(0, name.length() - 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            name = name.replace('/', '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            names.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private static Set findStandardMBeansFromDir(URL codeBase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        File dir = new File(new URI(codeBase.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        Set names = new TreeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        scanDir(dir, "", names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private static void scanDir(File dir, String prefix, Set names)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        File[] files = dir.listFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (files == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        for (int i = 0; i < files.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            File f = files[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            String name = f.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            String p = (prefix.equals("")) ? name : prefix + "." + name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (f.isDirectory())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                scanDir(f, p, names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            else if (name.endsWith(".class")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                p = p.substring(0, p.length() - 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                names.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
}