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