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

Java example source code file (IdenticalMBeanInfoTest.java)

This example Java source code file (IdenticalMBeanInfoTest.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

broadcaster, broadcastermbean, exception, management, mbeaninfo, mbeannotificationinfo, mbeans, mbeanserver, notificationbroadcasters, objectname, standardmbean, string, two, whatsit, whatsitmbean

The IdenticalMBeanInfoTest.java Java example source code

/*
 * Copyright (c) 2005, 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 5042004
 * @summary Check that MBeans with the same class have identical MBeanInfo
 * unless they are NotificationBroadcasters
 * @author Eamonn McManus
 * @run clean IdenticalMBeanInfoTest
 * @run build IdenticalMBeanInfoTest
 * @run main IdenticalMBeanInfoTest
 */

import javax.management.*;

/* What we test here is not required by the spec.  It is an
   optimization that can save a considerable amount of memory when
   there are many MBeans of the same type.  There is no reason why two
   Standard MBeans of the same type should have different MBeanInfo
   objects, unless they are NotificationBroadcasters and return
   different arrays from getNotificationInfo().  Note that two MBeans
   that share the same MBean interface but are of different classes
   cannot have the same MBeanInfo because the MBeanInfo includes the
   implementation class name.  */
public class IdenticalMBeanInfoTest {
    private static String failure = null;

    public static interface WhatsitMBean {
        public int getReadOnly();
        public int getReadWrite();
        public void setReadWrite(int x);
        public int op(int x, int y);
    }

    public static class Whatsit implements WhatsitMBean {
        public Whatsit() {}
        public Whatsit(int irrelevant) {}

        public int getReadOnly() { return 0; }
        public int getReadWrite() { return 0; }
        public void setReadWrite(int x) {}
        public int op(int x, int y) { return 0; }
    }

    public static interface BroadcasterMBean extends WhatsitMBean {
    }

    public static class Broadcaster extends Whatsit
            implements BroadcasterMBean, NotificationBroadcaster {
        private static int nextId = 1;
        private int id = nextId++;

        public void addNotificationListener(NotificationListener l,
                                            NotificationFilter f,
                                            Object h) {}

        public void removeNotificationListener(NotificationListener l) {}

        public MBeanNotificationInfo[] getNotificationInfo() {
            return new MBeanNotificationInfo[] {
                new MBeanNotificationInfo(new String[] {"type" + id},
                                          "something",
                                          "a something")
            };
        }
    }

    public static void main(String[] args) throws Exception {
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
        ObjectName on1 = new ObjectName("d:type=Whatsit,number=1");
        ObjectName on2 = new ObjectName("d:type=Whatsit,number=2");
        ObjectName on3 = new ObjectName("d:type=Whatsit,number=3");
        ObjectName on4 = new ObjectName("d:type=Whatsit,number=4");
        ObjectName on5 = new ObjectName("d:type=Whatsit,number=5");

        mbs.registerMBean(new Whatsit(), on1);
        mbs.createMBean(Whatsit.class.getName(), on2);
        DynamicMBean mbean =
            new StandardMBean(new Whatsit(), WhatsitMBean.class);
        mbs.registerMBean(mbean, on3);
        mbs.registerMBean(new Broadcaster(), on4);
        mbs.createMBean(Broadcaster.class.getName(), on5);
        MBeanInfo mbi1 = mbs.getMBeanInfo(on1);
        MBeanInfo mbi2 = mbs.getMBeanInfo(on2);
        MBeanInfo mbi3 = mbs.getMBeanInfo(on3);
        MBeanInfo mbi4 = mbs.getMBeanInfo(on4);
        MBeanInfo mbi5 = mbs.getMBeanInfo(on5);

        if (mbi1 != mbi2) {
            fail("Two MBeans of the same class should have identical " +
                 "MBeanInfo");
        }

        if (mbi2 != mbi3) {
            if (true)
                System.out.println("IGNORING StandardMBean(...) failure");
            else
                fail("Plain Standard MBean should have identical MBeanInfo " +
                     "to StandardMBean(...) with same class as resource");
        }

        if (mbi4 == mbi5 || mbi4.equals(mbi5)) {
            fail("Two MBeans of the same class should NOT have identical " +
                 "MBeanInfo if they are NotificationBroadcasters and "+
                 "do not return the same MBeanNotificationInfo[]");
        }

        if (failure == null) {
            System.out.println("Test passed");
            return;
        }

        throw new Exception("TEST FAILED: " + failure);
    }

    private static void fail(String why) {
        System.out.println("FAILURE: " + why);
        failure = why;
    }
}

Other Java examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.