0N/A/*
2362N/A * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6448042
0N/A * @summary Test that MXBeans can define their own names in preRegister
0N/A * @author Eamonn McManus
0N/A */
0N/A
0N/Aimport java.lang.management.ManagementFactory;
0N/Aimport java.lang.reflect.Constructor;
0N/Aimport javax.management.MBeanRegistration;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.ObjectInstance;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.StandardMBean;
0N/A
0N/A/*
0N/A * Test that an MXBean can decide its name by returning a value from
0N/A * the preRegister method. Also test the same thing for Standard MBeans
0N/A * for good measure.
0N/A */
0N/Apublic class PreRegisterNameTest {
0N/A public static interface SpumeMXBean {}
0N/A
0N/A public static class Spume implements SpumeMXBean, MBeanRegistration {
0N/A private ObjectName realName;
0N/A
0N/A public Spume(ObjectName realName) {
0N/A this.realName = realName;
0N/A }
0N/A
0N/A public void preDeregister() throws Exception {
0N/A }
0N/A
0N/A public void postDeregister() {
0N/A }
0N/A
0N/A public void postRegister(Boolean registrationDone) {
0N/A }
0N/A
0N/A public ObjectName preRegister(MBeanServer server, ObjectName name) {
0N/A return realName;
0N/A }
0N/A }
0N/A
0N/A public static interface ThingMBean {
0N/A public boolean getNoddy();
0N/A }
0N/A
0N/A public static class Thing implements ThingMBean, MBeanRegistration {
0N/A private ObjectName realName;
0N/A
0N/A public Thing(ObjectName realName) {
0N/A this.realName = realName;
0N/A }
0N/A
0N/A public ObjectName preRegister(MBeanServer mbs, ObjectName name) {
0N/A return realName;
0N/A }
0N/A
0N/A public void postRegister(Boolean done) {}
0N/A
0N/A public void preDeregister() {}
0N/A
0N/A public void postDeregister() {}
0N/A
0N/A public boolean getNoddy() {
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A public static class XThing extends StandardMBean implements ThingMBean {
0N/A private ObjectName realName;
0N/A
0N/A public XThing(ObjectName realName) {
0N/A super(ThingMBean.class, false);
0N/A this.realName = realName;
0N/A }
0N/A
0N/A @Override
0N/A public ObjectName preRegister(MBeanServer server, ObjectName name) {
0N/A return realName;
0N/A }
0N/A
0N/A public boolean getNoddy() {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A public static class XSpume extends StandardMBean implements SpumeMXBean {
0N/A private ObjectName realName;
0N/A
0N/A public XSpume(ObjectName realName) {
0N/A super(SpumeMXBean.class, true);
0N/A this.realName = realName;
0N/A }
0N/A
0N/A @Override
0N/A public ObjectName preRegister(MBeanServer server, ObjectName name)
0N/A throws Exception {
0N/A super.preRegister(server, realName);
0N/A return realName;
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
0N/A for (Class<?> c : new Class<?>[] {
0N/A Spume.class, Thing.class, XSpume.class, XThing.class
0N/A }) {
0N/A for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) {
0N/A System.out.println("Class " + c.getName() + " with name " + n +
0N/A "...");
0N/A ObjectName realName = new ObjectName("a:type=" + c.getName());
0N/A Constructor<?> constr = c.getConstructor(ObjectName.class);
0N/A Object mbean = constr.newInstance(realName);
0N/A ObjectInstance oi;
0N/A String what =
0N/A "Registering MBean of type " + c.getName() + " under name " +
0N/A "<" + n + ">: ";
0N/A try {
0N/A oi = mbs.registerMBean(mbean, n);
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A fail(what + " got " + e);
0N/A continue;
0N/A }
0N/A ObjectName registeredName = oi.getObjectName();
0N/A if (!registeredName.equals(realName))
0N/A fail(what + " registered as " + registeredName);
0N/A if (!mbs.isRegistered(realName)) {
0N/A fail(what + " not registered as expected");
0N/A }
0N/A mbs.unregisterMBean(registeredName);
0N/A }
0N/A }
0N/A System.err.flush();
0N/A if (failures == 0)
0N/A System.out.println("TEST PASSED");
0N/A else
0N/A throw new Exception("TEST FAILED: " + failure);
0N/A }
0N/A
0N/A private static void fail(String msg) {
0N/A System.err.println("FAILED: " + msg);
0N/A failure = msg;
0N/A failures++;
0N/A }
0N/A
0N/A private static int failures;
0N/A private static String failure;
0N/A}