Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/MBeanInfo/NotificationInfoTest.java
38840 views
/*1* Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 501263426* @summary Test that JMX classes use fully-qualified class names27* in MBeanNotificationInfo28* @author Eamonn McManus29* @run clean NotificationInfoTest30* @run build NotificationInfoTest31* @run main NotificationInfoTest32*/3334import java.io.*;35import java.lang.management.*;36import java.lang.reflect.*;37import java.net.*;38import java.security.CodeSource;39import java.util.*;40import java.util.jar.*;41import javax.management.*;42import javax.management.relation.*;43import javax.management.remote.*;44import javax.management.remote.rmi.*;4546/*47* This test finds all classes in the same code-base as the JMX48* classes that look like Standard MBeans, and checks that if they are49* NotificationBroadcasters they declare existent notification types.50* A class looks like a Standard MBean if both Thing and ThingMBean51* classes exist. So for example javax.management.timer.Timer looks52* like a Standard MBean because javax.management.timer.TimerMBean53* exists. Timer is instanceof NotificationBroadcaster, so we expect54* that ((NotificationBroadcaster) timer).getNotificationInfo() will55* return an array of MBeanNotificationInfo where each entry has a56* getName() that names an existent Java class that is a Notification.57*58* An MBean is "suspicious" if it is a NotificationBroadcaster but its59* MBeanNotificationInfo[] is empty. This is legal, but surprising.60*61* In order to call getNotificationInfo(), we need an instance of the62* class. We attempt to make one by calling a public no-arg63* constructor. But the "construct" method below can be extended to64* construct specific MBean classes for which the no-arg constructor65* doesn't exist.66*67* The test is obviously not exhaustive, but does catch the cases that68* failed in 5012634.69*/70public class NotificationInfoTest {71// class or object names where the test failed72private static final Set<String> failed = new TreeSet<String>();7374// class or object names where there were no MBeanNotificationInfo entries75private static final Set<String> suspicious = new TreeSet<String>();7677public static void main(String[] args) throws Exception {78System.out.println("Checking that all known MBeans that are " +79"NotificationBroadcasters have sane " +80"MBeanInfo.getNotifications()");8182System.out.println("Checking platform MBeans...");83checkPlatformMBeans();8485CodeSource cs =86javax.management.MBeanServer.class.getProtectionDomain()87.getCodeSource();88URL codeBase;89if (cs == null) {90String javaHome = System.getProperty("java.home");91String[] candidates = {"/lib/rt.jar", "/classes/"};92codeBase = null;93for (String candidate : candidates) {94File file = new File(javaHome + candidate);95if (file.exists()) {96codeBase = file.toURI().toURL();97break;98}99}100if (codeBase == null) {101throw new Exception(102"Could not determine codeBase for java.home=" + javaHome);103}104} else105codeBase = cs.getLocation();106107System.out.println();108System.out.println("Looking for standard MBeans...");109String[] classes = findStandardMBeans(codeBase);110111System.out.println("Testing standard MBeans...");112for (int i = 0; i < classes.length; i++) {113String name = classes[i];114Class<?> c;115try {116c = Class.forName(name);117} catch (Throwable e) {118System.out.println(name + ": cannot load (not public?): " + e);119continue;120}121if (!NotificationBroadcaster.class.isAssignableFrom(c)) {122System.out.println(name + ": not a NotificationBroadcaster");123continue;124}125if (Modifier.isAbstract(c.getModifiers())) {126System.out.println(name + ": abstract class");127continue;128}129130NotificationBroadcaster mbean;131Constructor<?> constr;132try {133constr = c.getConstructor();134} catch (Exception e) {135System.out.println(name + ": no public no-arg constructor: "136+ e);137continue;138}139try {140mbean = (NotificationBroadcaster) constr.newInstance();141} catch (Exception e) {142System.out.println(name + ": no-arg constructor failed: " + e);143continue;144}145146check(mbean);147}148149System.out.println();150System.out.println("Testing some explicit cases...");151152check(new RelationService(false));153/*154We can't do this:155check(new RequiredModelMBean());156because the Model MBean spec more or less forces us to use the157names GENERIC and ATTRIBUTE_CHANGE for its standard notifs.158*/159checkRMIConnectorServer();160161System.out.println();162if (!suspicious.isEmpty())163System.out.println("SUSPICIOUS CLASSES: " + suspicious);164165if (failed.isEmpty())166System.out.println("TEST PASSED");167else {168System.out.println("TEST FAILED: " + failed);169System.exit(1);170}171}172173private static void check(NotificationBroadcaster mbean)174throws Exception {175System.out.print(mbean.getClass().getName() + ": ");176177check(mbean.getClass().getName(), mbean.getNotificationInfo());178}179180private static void checkPlatformMBeans() throws Exception {181MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();182Set<ObjectName> mbeanNames = mbs.queryNames(null, null);183for (ObjectName name : mbeanNames) {184if (!mbs.isInstanceOf(name,185NotificationBroadcaster.class.getName())) {186System.out.println(name + ": not a NotificationBroadcaster");187} else {188MBeanInfo mbi = mbs.getMBeanInfo(name);189check(name.toString(), mbi.getNotifications());190}191}192}193194private static void checkRMIConnectorServer() throws Exception {195JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");196RMIConnectorServer connector = new RMIConnectorServer(url, null);197check(connector);198}199200private static void check(String what, MBeanNotificationInfo[] mbnis) {201System.out.print(what + ": checking notification info: ");202203if (mbnis.length == 0) {204System.out.println("NONE (suspicious)");205suspicious.add(what);206return;207}208209// Each MBeanNotificationInfo.getName() should be an existent210// Java class that is Notification or a subclass of it211for (int j = 0; j < mbnis.length; j++) {212String notifClassName = mbnis[j].getName();213Class notifClass;214try {215notifClass = Class.forName(notifClassName);216} catch (Exception e) {217System.out.print("FAILED(" + notifClassName + ": " + e +218") ");219failed.add(what);220continue;221}222if (!Notification.class.isAssignableFrom(notifClass)) {223System.out.print("FAILED(" + notifClassName +224": not a Notification) ");225failed.add(what);226continue;227}228System.out.print("OK(" + notifClassName + ") ");229}230System.out.println();231}232233private static String[] findStandardMBeans(URL codeBase)234throws Exception {235Set<String> names;236if (codeBase.getProtocol().equalsIgnoreCase("file")237&& codeBase.toString().endsWith("/"))238names = findStandardMBeansFromDir(codeBase);239else240names = findStandardMBeansFromJar(codeBase);241242Set<String> standardMBeanNames = new TreeSet<String>();243for (String name : names) {244if (name.endsWith("MBean")) {245String prefix = name.substring(0, name.length() - 5);246if (names.contains(prefix))247standardMBeanNames.add(prefix);248}249}250return standardMBeanNames.toArray(new String[0]);251}252253private static Set<String> findStandardMBeansFromJar(URL codeBase)254throws Exception {255InputStream is = codeBase.openStream();256JarInputStream jis = new JarInputStream(is);257Set<String> names = new TreeSet<String>();258JarEntry entry;259while ((entry = jis.getNextJarEntry()) != null) {260String name = entry.getName();261if (!name.endsWith(".class"))262continue;263name = name.substring(0, name.length() - 6);264name = name.replace('/', '.');265names.add(name);266}267return names;268}269270private static Set<String> findStandardMBeansFromDir(URL codeBase)271throws Exception {272File dir = new File(new URI(codeBase.toString()));273Set<String> names = new TreeSet<String>();274scanDir(dir, "", names);275return names;276}277278private static void scanDir(File dir, String prefix, Set<String> names)279throws Exception {280File[] files = dir.listFiles();281if (files == null)282return;283for (int i = 0; i < files.length; i++) {284File f = files[i];285String name = f.getName();286String p = (prefix.equals("")) ? name : prefix + "." + name;287if (f.isDirectory())288scanDir(f, p, names);289else if (name.endsWith(".class")) {290p = p.substring(0, p.length() - 6);291names.add(p);292}293}294}295}296297298