1 /*
   2  * Copyright (c) 2005, 2016, 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     6320211
  27  * @summary Check that java.lang.management MXBeans have the same behavior
  28  *          as user MXBeans
  29  * @author  Eamonn McManus
  30  * @modules jdk.management
  31  * @run     main/othervm MXBeanBehavior
  32  */
  33 
  34 import java.lang.management.*;
  35 import java.lang.reflect.*;
  36 import java.util.*;
  37 import javax.management.*;
  38 
  39 public class MXBeanBehavior {
  40     // Exclude list: list of platform MBeans that are not MXBeans
  41     public static final HashSet<String> excludeList = new HashSet<>(
  42             Arrays.asList("com.sun.management:type=DiagnosticCommand"));
  43 
  44     public static void main(String[] args) throws Exception {
  45         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  46 
  47         /* Test that all the MBeans in the java.* and com.sun.management*
  48            domains are MXBeans with the appropriate behavior.  */
  49         Set<ObjectName> names = mbs.queryNames(new ObjectName("java.*:*"),
  50                                                null);
  51         names.addAll(mbs.queryNames(new ObjectName("com.sun.management*:*"),
  52                                     null));
  53         for (ObjectName name : names)
  54             test(mbs, name);
  55 
  56         /* Now do some rudimentary testing of inter-MXBean references.
  57            It should be possible for a user MXBean to return e.g.  the
  58            CompilationMXBean from the platform from an attribute of
  59            type CompilationMXBean, and have the MXBean infrastructure
  60            map this into that MXBean's standard ObjectName.  It should
  61            also be possible for a proxy for this user MXBean to have
  62            this attribute's value mapped back into a CompilationMXBean
  63            instance, which however will be another proxy rather than
  64            the original object.  Finally, it should be possible to set
  65            the attribute in the user's MXBean through a proxy, giving
  66            the real CompilationMXBean as an argument, and have this be
  67            translated into that MXBean's standard ObjectName.  The
  68            user's MXBean will receive a proxy in this case, though we
  69            don't check that.  */
  70         ObjectName refName = new ObjectName("d:type=CompilationRef");
  71         mbs.registerMBean(new CompilationImpl(), refName);
  72         CompilationRefMXBean refProxy =
  73             JMX.newMXBeanProxy(mbs, refName, CompilationRefMXBean.class);
  74         refProxy.getCompilationMXBean();
  75         refProxy.setCompilationMXBean(ManagementFactory.getCompilationMXBean());
  76         ObjectName on =
  77             (ObjectName) mbs.getAttribute(refName, "CompilationMXBean");
  78         checkEqual(on, new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME),
  79                    "Referenced object name");
  80         mbs.setAttribute(refName, new Attribute("CompilationMXBean", on));
  81 
  82         System.out.println("TEST PASSED");
  83     }
  84 
  85     /* Check the behavior of this MXBean to ensure that it conforms to
  86        what is expected of all MXBeans as detailed in
  87        javax.management.MXBean.  Its MBeanInfo should have a
  88        Descriptor with the fields mxbean and interfaceClassName, and
  89        furthermore we know that our implementation sets immutableInfo
  90        here.  Each attribute should have Descriptor with the fields
  91        openType and originalType that have appropriate values.  We
  92        don't currently check operations though the same considerations
  93        would apply there.  (If the MBeanInfo and MBeanAttributeInfo
  94        tests pass we can reasonably suppose that this MXBean will
  95        behave the same as all other MXBeans, so MBeanOperationInfo,
  96        MBeanNotificationInfo, and MBeanConstructorInfo will be covered
  97        by generic MXBean tests.
  98      */
  99     private static void test(MBeanServer mbs, ObjectName name) throws Exception {
 100         if(excludeList.contains(name.getCanonicalName())) {
 101             // Skipping not MXBean objects.
 102             return;
 103         }
 104         System.out.println("Testing: " + name);
 105 
 106         MBeanInfo mbi = mbs.getMBeanInfo(name);
 107         Descriptor mbid = mbi.getDescriptor();
 108         Object[] values = mbid.getFieldValues("immutableInfo",
 109                                               "interfaceClassName",
 110                                               "mxbean");
 111         checkEqual(values[0], "true", name + " immutableInfo field");
 112         checkEqual(values[2], "true", name + " mxbean field");
 113         String interfaceClassName = (String) values[1];
 114         if (!mbs.isInstanceOf(name, interfaceClassName)) {
 115             throw new RuntimeException(name + " not instance of " +
 116                                        interfaceClassName);
 117         }
 118         Class interfaceClass = Class.forName(interfaceClassName);
 119         for (MBeanAttributeInfo mbai : mbi.getAttributes()) {
 120             Descriptor mbaid = mbai.getDescriptor();
 121             Object[] avalues = mbaid.getFieldValues("openType",
 122                                                     "originalType");
 123             if (avalues[0] == null || avalues[1] == null) {
 124                 throw new RuntimeException("Null attribute descriptor fields: " +
 125                                            Arrays.toString(avalues));
 126             }
 127             if (mbai.isReadable()) {
 128                 String mname = (mbai.isIs() ? "is" : "get") + mbai.getName();
 129                 Method m = interfaceClass.getMethod(mname);
 130                 Type t = m.getGenericReturnType();
 131                 String ret =
 132                     (t instanceof Class) ? ((Class) t).getName() : t.toString();
 133                 if (!ret.equals(avalues[1])) {
 134                     final String msg =
 135                         name + " attribute " + mbai.getName() + " has wrong " +
 136                         "originalType: " + avalues[1] + " vs " + ret;
 137                     throw new RuntimeException(msg);
 138                 }
 139             }
 140         }
 141     }
 142 
 143     private static void checkEqual(Object x, Object y, String what) {
 144         final boolean eq;
 145         if (x == y)
 146             eq = true;
 147         else if (x == null)
 148             eq = false;
 149         else
 150             eq = x.equals(y);
 151         if (!eq)
 152             throw new RuntimeException(what + " should be " + y + ", is " + x);
 153     }
 154 
 155     public static interface CompilationRefMXBean {
 156         public CompilationMXBean getCompilationMXBean();
 157         public void setCompilationMXBean(CompilationMXBean mxb);
 158     }
 159 
 160     public static class CompilationImpl implements CompilationRefMXBean {
 161         public CompilationMXBean getCompilationMXBean() {
 162             return ManagementFactory.getCompilationMXBean();
 163         }
 164 
 165         public void setCompilationMXBean(CompilationMXBean mxb) {
 166             if (mxb == ManagementFactory.getCompilationMXBean())
 167                 return;
 168             MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler)
 169                 Proxy.getInvocationHandler(mxb);
 170             ObjectName expectedName;
 171             try {
 172                 expectedName =
 173                     new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME);
 174             } catch (MalformedObjectNameException e) {
 175                 throw new RuntimeException(e);
 176             }
 177             checkEqual(mbsih.getObjectName(), expectedName,
 178                        "Proxy name in setCompilationMXBean");
 179         }
 180     }
 181 }