1 /* 2 * Copyright (c) 2006, 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 6448042 27 * @summary Test that MXBeans can define their own names in preRegister 28 * @author Eamonn McManus 29 */ 30 31 import java.lang.management.ManagementFactory; 32 import java.lang.reflect.Constructor; 33 import javax.management.MBeanRegistration; 34 import javax.management.MBeanServer; 35 import javax.management.ObjectInstance; 36 import javax.management.ObjectName; 37 import javax.management.StandardMBean; 38 39 /* 40 * Test that an MXBean can decide its name by returning a value from 41 * the preRegister method. Also test the same thing for Standard MBeans 42 * for good measure. 43 */ 44 public class PreRegisterNameTest { 45 public static interface SpumeMXBean {} 46 47 public static class Spume implements SpumeMXBean, MBeanRegistration { 48 private ObjectName realName; 49 50 public Spume(ObjectName realName) { 51 this.realName = realName; 52 } 53 54 public void preDeregister() throws Exception { 55 } 56 57 public void postDeregister() { 58 } 59 60 public void postRegister(Boolean registrationDone) { 61 } 62 63 public ObjectName preRegister(MBeanServer server, ObjectName name) { 64 return realName; 65 } 66 } 67 68 public static interface ThingMBean { 69 public boolean getNoddy(); 70 } 71 72 public static class Thing implements ThingMBean, MBeanRegistration { 73 private ObjectName realName; 74 75 public Thing(ObjectName realName) { 76 this.realName = realName; 77 } 78 79 public ObjectName preRegister(MBeanServer mbs, ObjectName name) { 80 return realName; 81 } 82 83 public void postRegister(Boolean done) {} 84 85 public void preDeregister() {} 86 87 public void postDeregister() {} 88 89 public boolean getNoddy() { 90 return true; 91 } 92 } 93 94 public static class XThing extends StandardMBean implements ThingMBean { 95 private ObjectName realName; 96 97 public XThing(ObjectName realName) { 98 super(ThingMBean.class, false); 99 this.realName = realName; 100 } 101 102 @Override 103 public ObjectName preRegister(MBeanServer server, ObjectName name) { 104 return realName; 105 } 106 107 public boolean getNoddy() { 108 return false; 109 } 110 } 111 112 public static class XSpume extends StandardMBean implements SpumeMXBean { 113 private ObjectName realName; 114 115 public XSpume(ObjectName realName) { 116 super(SpumeMXBean.class, true); 117 this.realName = realName; 118 } 119 120 @Override 121 public ObjectName preRegister(MBeanServer server, ObjectName name) 122 throws Exception { 123 super.preRegister(server, realName); 124 return realName; 125 } 126 } 127 128 public static void main(String[] args) throws Exception { 129 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 130 for (Class<?> c : new Class<?>[] { 131 Spume.class, Thing.class, XSpume.class, XThing.class 132 }) { 133 for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) { 134 System.out.println("Class " + c.getName() + " with name " + n + 135 "..."); 136 ObjectName realName = new ObjectName("a:type=" + c.getName()); 137 Constructor<?> constr = c.getConstructor(ObjectName.class); 138 Object mbean = constr.newInstance(realName); 139 ObjectInstance oi; 140 String what = 141 "Registering MBean of type " + c.getName() + " under name " + 142 "<" + n + ">: "; 143 try { 144 oi = mbs.registerMBean(mbean, n); 145 } catch (Exception e) { 146 e.printStackTrace(); 147 fail(what + " got " + e); 148 continue; 149 } 150 ObjectName registeredName = oi.getObjectName(); 151 if (!registeredName.equals(realName)) 152 fail(what + " registered as " + registeredName); 153 if (!mbs.isRegistered(realName)) { 154 fail(what + " not registered as expected"); 155 } 156 mbs.unregisterMBean(registeredName); 157 } 158 } 159 System.err.flush(); 160 if (failures == 0) 161 System.out.println("TEST PASSED"); 162 else 163 throw new Exception("TEST FAILED: " + failure); 164 } 165 166 private static void fail(String msg) { 167 System.err.println("FAILED: " + msg); 168 failure = msg; 169 failures++; 170 } 171 172 private static int failures; 173 private static String failure; 174 }