Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ManagementFactory/MBeanServerMXBeanUnsupportedTest.java
38828 views
/*1* Copyright (c) 2006, 2013, 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* @run clean MBeanServerMXBeanUnsupportedTest30* @run build MBeanServerMXBeanUnsupportedTest31* @run main/othervm MBeanServerMXBeanUnsupportedTest32*/3334import java.lang.management.ManagementFactory;35import java.lang.reflect.InvocationHandler;36import java.lang.reflect.Method;37import java.lang.reflect.Proxy;38import java.util.Arrays;39import java.util.HashSet;40import javax.management.MBeanServer;41import javax.management.MBeanServerBuilder;42import javax.management.MBeanServerDelegate;43import javax.management.ObjectName;44import javax.management.StandardMBean;45import javax.management.remote.MBeanServerForwarder;4647public class MBeanServerMXBeanUnsupportedTest {4849/**50* An MBeanServerBuilder that returns an MBeanServer which throws a51* RuntimeException if MXBeans are not converted into StandardMBean.52*/53public static class MBeanServerBuilderImpl extends MBeanServerBuilder {5455private final MBeanServerBuilder inner;5657public MBeanServerBuilderImpl() {58inner = new MBeanServerBuilder();59}6061public MBeanServer newMBeanServer(62String defaultDomain,63MBeanServer outer,64MBeanServerDelegate delegate) {65final MBeanServerForwarder mbsf =66MBeanServerForwarderInvocationHandler.newProxyInstance();6768final MBeanServer innerMBeanServer =69inner.newMBeanServer(defaultDomain,70(outer == null ? mbsf : outer),71delegate);7273mbsf.setMBeanServer(innerMBeanServer);74return mbsf;75}76}7778/**79* An MBeanServerForwarderInvocationHandler that throws a80* RuntimeException if we try to register a non StandardMBean.81*/82public static class MBeanServerForwarderInvocationHandler83implements InvocationHandler {8485public static final HashSet<String> excludeList = new HashSet<String>(86Arrays.asList("com.sun.management:type=DiagnosticCommand"));8788public static MBeanServerForwarder newProxyInstance() {8990final InvocationHandler handler =91new MBeanServerForwarderInvocationHandler();9293final Class[] interfaces =94new Class[] {MBeanServerForwarder.class};9596Object proxy = Proxy.newProxyInstance(97MBeanServerForwarder.class.getClassLoader(),98interfaces,99handler);100101return MBeanServerForwarder.class.cast(proxy);102}103104public Object invoke(Object proxy, Method method, Object[] args)105throws Throwable {106107final String methodName = method.getName();108109if (methodName.equals("getMBeanServer")) {110return mbs;111}112113if (methodName.equals("setMBeanServer")) {114if (args[0] == null)115throw new IllegalArgumentException("Null MBeanServer");116if (mbs != null)117throw new IllegalArgumentException("MBeanServer object " +118"already initialized");119mbs = (MBeanServer) args[0];120return null;121}122123if (methodName.equals("registerMBean")) {124Object mbean = args[0];125ObjectName name = (ObjectName) args[1];126String domain = name.getDomain();127System.out.println("registerMBean: class=" +128mbean.getClass().getName() + "\tname=" + name);129Object result = method.invoke(mbs, args);130if (domain.equals("java.lang") ||131domain.equals("java.util.logging") ||132domain.equals("com.sun.management")) {133if(!excludeList.contains(name.getCanonicalName())) {134String mxbean = (String)135mbs.getMBeanInfo(name).getDescriptor().getFieldValue("mxbean");136if (mxbean == null || !mxbean.equals("true")) {137throw new RuntimeException(138"Platform MBeans must be MXBeans!");139}140if (!(mbean instanceof StandardMBean)) {141throw new RuntimeException(142"MXBeans must be wrapped in StandardMBean!");143}144}145}146return result;147}148149return method.invoke(mbs, args);150}151152private MBeanServer mbs;153}154155/*156* Standalone entry point.157*158* Run the test and report to stdout.159*/160public static void main(String args[]) throws Exception {161System.setProperty("javax.management.builder.initial",162MBeanServerBuilderImpl.class.getName());163try {164ManagementFactory.getPlatformMBeanServer();165} catch (RuntimeException e) {166System.out.println(">>> Unhappy Bye, Bye!");167throw e;168}169System.out.println(">>> Happy Bye, Bye!");170}171}172173174