alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (PreRegisterNameTest.java)

This example Java source code file (PreRegisterNameTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

class, exception, failed, management, mbeanregistration, mbeanserver, objectname, override, preregisternametest, reflection, spumemxbean, standardmbean, string, thingmbean, xspume, xthing

The PreRegisterNameTest.java Java example source code

/*
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 6448042
 * @summary Test that MXBeans can define their own names in preRegister
 * @author Eamonn McManus
 */

import java.lang.management.ManagementFactory;
import java.lang.reflect.Constructor;
import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.StandardMBean;

/*
 * Test that an MXBean can decide its name by returning a value from
 * the preRegister method.  Also test the same thing for Standard MBeans
 * for good measure.
 */
public class PreRegisterNameTest {
    public static interface SpumeMXBean {}

    public static class Spume implements SpumeMXBean, MBeanRegistration {
        private ObjectName realName;

        public Spume(ObjectName realName) {
            this.realName = realName;
        }

        public void preDeregister() throws Exception {
        }

        public void postDeregister() {
        }

        public void postRegister(Boolean registrationDone) {
        }

        public ObjectName preRegister(MBeanServer server, ObjectName name) {
            return realName;
        }
    }

    public static interface ThingMBean {
        public boolean getNoddy();
    }

    public static class Thing implements ThingMBean, MBeanRegistration {
        private ObjectName realName;

        public Thing(ObjectName realName) {
            this.realName = realName;
        }

        public ObjectName preRegister(MBeanServer mbs, ObjectName name) {
            return realName;
        }

        public void postRegister(Boolean done) {}

        public void preDeregister() {}

        public void postDeregister() {}

        public boolean getNoddy() {
            return true;
        }
    }

    public static class XThing extends StandardMBean implements ThingMBean {
        private ObjectName realName;

        public XThing(ObjectName realName) {
            super(ThingMBean.class, false);
            this.realName = realName;
        }

        @Override
        public ObjectName preRegister(MBeanServer server, ObjectName name) {
            return realName;
        }

        public boolean getNoddy() {
            return false;
        }
    }

    public static class XSpume extends StandardMBean implements SpumeMXBean {
        private ObjectName realName;

        public XSpume(ObjectName realName) {
            super(SpumeMXBean.class, true);
            this.realName = realName;
        }

        @Override
        public ObjectName preRegister(MBeanServer server, ObjectName name)
        throws Exception {
            super.preRegister(server, realName);
            return realName;
        }
    }

    public static void main(String[] args) throws Exception {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        for (Class<?> c : new Class[] {
                Spume.class, Thing.class, XSpume.class, XThing.class
             }) {
            for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) {
                System.out.println("Class " + c.getName() + " with name " + n +
                        "...");
                ObjectName realName = new ObjectName("a:type=" + c.getName());
                Constructor<?> constr = c.getConstructor(ObjectName.class);
                Object mbean = constr.newInstance(realName);
                ObjectInstance oi;
                String what =
                    "Registering MBean of type " + c.getName() + " under name " +
                    "<" + n + ">: ";
                try {
                    oi = mbs.registerMBean(mbean, n);
                } catch (Exception e) {
                    e.printStackTrace();
                    fail(what + " got " + e);
                    continue;
                }
                ObjectName registeredName = oi.getObjectName();
                if (!registeredName.equals(realName))
                    fail(what + " registered as " + registeredName);
                if (!mbs.isRegistered(realName)) {
                    fail(what + " not registered as expected");
                }
                mbs.unregisterMBean(registeredName);
            }
        }
        System.err.flush();
        if (failures == 0)
            System.out.println("TEST PASSED");
        else
            throw new Exception("TEST FAILED: " + failure);
    }

    private static void fail(String msg) {
        System.err.println("FAILED: " + msg);
        failure = msg;
        failures++;
    }

    private static int failures;
    private static String failure;
}

Other Java examples (source code examples)

Here is a short list of links related to this Java PreRegisterNameTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.