Path: blob/master/test/jdk/java/lang/management/ManagementFactory/MBeanServerMXBeanUnsupportedTest.java
41152 views
/*1* Copyright (c) 2006, 2016, 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 640379426* @summary Test that all the platform MXBeans are wrapped in StandardMBean so27* an MBeanServer which does not have support for MXBeans can be used.28* @author Luis-Miguel Alventosa29*30* @run clean MBeanServerMXBeanUnsupportedTest31* @run build MBeanServerMXBeanUnsupportedTest32* @run main/othervm MBeanServerMXBeanUnsupportedTest33*/3435import java.lang.management.ManagementFactory;36import java.lang.reflect.InvocationHandler;37import java.lang.reflect.Method;38import java.lang.reflect.Proxy;39import java.util.Arrays;40import java.util.HashSet;41import javax.management.MBeanServer;42import javax.management.MBeanServerBuilder;43import javax.management.MBeanServerDelegate;44import javax.management.ObjectName;45import javax.management.StandardMBean;46import javax.management.remote.MBeanServerForwarder;4748public class MBeanServerMXBeanUnsupportedTest {4950/**51* An MBeanServerBuilder that returns an MBeanServer which throws a52* RuntimeException if MXBeans are not converted into StandardMBean.53*/54public static class MBeanServerBuilderImpl extends MBeanServerBuilder {5556private final MBeanServerBuilder inner;5758public MBeanServerBuilderImpl() {59inner = new MBeanServerBuilder();60}6162public MBeanServer newMBeanServer(63String defaultDomain,64MBeanServer outer,65MBeanServerDelegate delegate) {66final MBeanServerForwarder mbsf =67MBeanServerForwarderInvocationHandler.newProxyInstance();6869final MBeanServer innerMBeanServer =70inner.newMBeanServer(defaultDomain,71(outer == null ? mbsf : outer),72delegate);7374mbsf.setMBeanServer(innerMBeanServer);75return mbsf;76}77}7879/**80* An MBeanServerForwarderInvocationHandler that throws a81* RuntimeException if we try to register a non StandardMBean.82*/83public static class MBeanServerForwarderInvocationHandler84implements InvocationHandler {8586public static final HashSet<String> excludeList = new HashSet<String>(87Arrays.asList("com.sun.management:type=DiagnosticCommand"));8889public static MBeanServerForwarder newProxyInstance() {9091final InvocationHandler handler =92new MBeanServerForwarderInvocationHandler();9394final Class[] interfaces =95new Class[] {MBeanServerForwarder.class};9697Object proxy = Proxy.newProxyInstance(98MBeanServerForwarder.class.getClassLoader(),99interfaces,100handler);101102return MBeanServerForwarder.class.cast(proxy);103}104105public Object invoke(Object proxy, Method method, Object[] args)106throws Throwable {107108final String methodName = method.getName();109110if (methodName.equals("getMBeanServer")) {111return mbs;112}113114if (methodName.equals("setMBeanServer")) {115if (args[0] == null)116throw new IllegalArgumentException("Null MBeanServer");117if (mbs != null)118throw new IllegalArgumentException("MBeanServer object " +119"already initialized");120mbs = (MBeanServer) args[0];121return null;122}123124if (methodName.equals("registerMBean")) {125Object mbean = args[0];126ObjectName name = (ObjectName) args[1];127String domain = name.getDomain();128System.out.println("registerMBean: class=" +129mbean.getClass().getName() + "\tname=" + name);130Object result = method.invoke(mbs, args);131if (domain.equals("java.lang") ||132domain.equals("java.util.logging") ||133domain.equals("com.sun.management")) {134if(!excludeList.contains(name.getCanonicalName())) {135String mxbean = (String)136mbs.getMBeanInfo(name).getDescriptor().getFieldValue("mxbean");137if (mxbean == null || !mxbean.equals("true")) {138throw new RuntimeException(139"Platform MBeans must be MXBeans!");140}141if (!(mbean instanceof StandardMBean)) {142throw new RuntimeException(143"MXBeans must be wrapped in StandardMBean!");144}145}146}147return result;148}149150return method.invoke(mbs, args);151}152153private MBeanServer mbs;154}155156/*157* Standalone entry point.158*159* Run the test and report to stdout.160*/161public static void main(String args[]) throws Exception {162System.setProperty("javax.management.builder.initial",163MBeanServerBuilderImpl.class.getName());164try {165ManagementFactory.getPlatformMBeanServer();166} catch (RuntimeException e) {167System.out.println(">>> Unhappy Bye, Bye!");168throw e;169}170System.out.println(">>> Happy Bye, Bye!");171}172}173174175