1 /* 2 * Copyright (c) 2006, 2013, 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 6403794 27 * @summary Test that all the platform MXBeans are wrapped in StandardMBean so 28 * an MBeanServer which does not have support for MXBeans can be used. 29 * @author Luis-Miguel Alventosa 30 * @run clean MBeanServerMXBeanUnsupportedTest 31 * @run build MBeanServerMXBeanUnsupportedTest 32 * @run main/othervm MBeanServerMXBeanUnsupportedTest 33 */ 34 35 import java.lang.management.ManagementFactory; 36 import java.lang.reflect.InvocationHandler; 37 import java.lang.reflect.Method; 38 import java.lang.reflect.Proxy; 39 import java.util.Arrays; 40 import java.util.HashSet; 41 import javax.management.MBeanServer; 42 import javax.management.MBeanServerBuilder; 43 import javax.management.MBeanServerDelegate; 44 import javax.management.ObjectName; 45 import javax.management.StandardMBean; 46 import javax.management.remote.MBeanServerForwarder; 47 48 public class MBeanServerMXBeanUnsupportedTest { 49 50 /** 51 * An MBeanServerBuilder that returns an MBeanServer which throws a 52 * RuntimeException if MXBeans are not converted into StandardMBean. 53 */ 54 public static class MBeanServerBuilderImpl extends MBeanServerBuilder { 55 56 private final MBeanServerBuilder inner; 57 58 public MBeanServerBuilderImpl() { 59 inner = new MBeanServerBuilder(); 60 } 61 62 public MBeanServer newMBeanServer( 63 String defaultDomain, 64 MBeanServer outer, 65 MBeanServerDelegate delegate) { 66 final MBeanServerForwarder mbsf = 67 MBeanServerForwarderInvocationHandler.newProxyInstance(); 68 69 final MBeanServer innerMBeanServer = 70 inner.newMBeanServer(defaultDomain, 71 (outer == null ? mbsf : outer), 72 delegate); 73 74 mbsf.setMBeanServer(innerMBeanServer); 75 return mbsf; 76 } 77 } 78 79 /** 80 * An MBeanServerForwarderInvocationHandler that throws a 81 * RuntimeException if we try to register a non StandardMBean. 82 */ 83 public static class MBeanServerForwarderInvocationHandler 84 implements InvocationHandler { 85 86 public static final HashSet<String> excludeList = new HashSet<String>( 87 Arrays.asList("com.sun.management:type=DiagnosticCommand")); 88 89 public static MBeanServerForwarder newProxyInstance() { 90 91 final InvocationHandler handler = 92 new MBeanServerForwarderInvocationHandler(); 93 94 final Class[] interfaces = 95 new Class[] {MBeanServerForwarder.class}; 96 97 Object proxy = Proxy.newProxyInstance( 98 MBeanServerForwarder.class.getClassLoader(), 99 interfaces, 100 handler); 101 102 return MBeanServerForwarder.class.cast(proxy); 103 } 104 105 public Object invoke(Object proxy, Method method, Object[] args) 106 throws Throwable { 107 108 final String methodName = method.getName(); 109 110 if (methodName.equals("getMBeanServer")) { 111 return mbs; 112 } 113 114 if (methodName.equals("setMBeanServer")) { 115 if (args[0] == null) 116 throw new IllegalArgumentException("Null MBeanServer"); 117 if (mbs != null) 118 throw new IllegalArgumentException("MBeanServer object " + 119 "already initialized"); 120 mbs = (MBeanServer) args[0]; 121 return null; 122 } 123 124 if (methodName.equals("registerMBean")) { 125 Object mbean = args[0]; 126 ObjectName name = (ObjectName) args[1]; 127 String domain = name.getDomain(); 128 System.out.println("registerMBean: class=" + 129 mbean.getClass().getName() + "\tname=" + name); 130 Object result = method.invoke(mbs, args); 131 if (domain.equals("java.lang") || 132 domain.equals("java.util.logging") || 133 domain.equals("com.sun.management")) { 134 if(!excludeList.contains(name.getCanonicalName())) { 135 String mxbean = (String) 136 mbs.getMBeanInfo(name).getDescriptor().getFieldValue("mxbean"); 137 if (mxbean == null || !mxbean.equals("true")) { 138 throw new RuntimeException( 139 "Platform MBeans must be MXBeans!"); 140 } 141 if (!(mbean instanceof StandardMBean)) { 142 throw new RuntimeException( 143 "MXBeans must be wrapped in StandardMBean!"); 144 } 145 } 146 } 147 return result; 148 } 149 150 return method.invoke(mbs, args); 151 } 152 153 private MBeanServer mbs; 154 } 155 156 /* 157 * Standalone entry point. 158 * 159 * Run the test and report to stdout. 160 */ 161 public static void main(String args[]) throws Exception { 162 System.setProperty("javax.management.builder.initial", 163 MBeanServerBuilderImpl.class.getName()); 164 try { 165 ManagementFactory.getPlatformMBeanServer(); 166 } catch (RuntimeException e) { 167 System.out.println(">>> Unhappy Bye, Bye!"); 168 throw e; 169 } 170 System.out.println(">>> Happy Bye, Bye!"); 171 } 172 }