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