Register the MBean represented by the interface class. - Java javax.management

Java examples for javax.management:MBean

Description

Register the MBean represented by the interface class.

Demo Code

/**//from  w w w  .ja  v a2s . c  o m
 * Copyright 2010-2013 lazydog.org.
 *
 * This file is part of repository.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

import java.lang.management.ManagementFactory;
import java.util.Hashtable;

import java.util.ServiceLoader;

import javax.management.MBeanException;
import javax.management.MBeanServer;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectInstance;
import javax.management.ObjectName;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class interfaceClass = String.class;
        System.out.println(register(interfaceClass));
    }

    /**
     * Register the MBean represented by the interface class.  The MBean object
     * name is set to the object name returned by the
     * getObjectName(Class interfaceClass) method.
     *
     * @param  interfaceClass  the MBean interface class.
     *
     * @return  the MBean object name.
     *
     * @throws  IllegalArgumentException  if the interface class is invalid or
     *                                    unable to get the MBean object name.
     * @throws  MBeanException            if unable to register the MBean.
     */
    public static <T> ObjectName register(Class<T> interfaceClass)
            throws MBeanException {
        return register(interfaceClass, getObjectName(interfaceClass));
    }

    /**
     * Register the MBean represented by the interface class.  The MBean object 
     * name is set to the object name returned by the 
     * getObjectName(Class interfaceClass, String key, String value) method.
     *
     * @param  interfaceClass  the MBean interface class.
     * @param  key             the property key.
     * @param  value           the property value.
     *
     * @return  the MBean object name.
     *
     * @throws  IllegalArgumentException  if the interface class is invalid or
     *                                    unable to get the MBean object name.
     * @throws  MBeanException            if unable to register the MBean.
     */
    public static <T> ObjectName register(Class<T> interfaceClass,
            String key, String value) throws MBeanException {
        return register(interfaceClass,
                getObjectName(interfaceClass, key, value));
    }

    /**
     * Register the MBean represented by the interface class.  The MBean object
     * name is set to the object name returned by the
     * getObjectName(Class interfaceClass, Hashtable<String,String> table)
     * method.
     *
     * @param  interfaceClass  the MBean interface class.
     * @param  table           the properties table.
     *
     * @return  the MBean object name.
     *
     * @throws  IllegalArgumentException  if the interface class is invalid or
     *                                    unable to get the MBean object name.
     * @throws  MBeanException            if unable to register the MBean.
     */
    public static <T> ObjectName register(Class<T> interfaceClass,
            Hashtable<String, String> table) throws MBeanException {
        return register(interfaceClass,
                getObjectName(interfaceClass, table));
    }

    /**
     * Register the MBean represented by the interface class.
     *
     * @param  interfaceClass  the MBean interface class.
     * @param  objectName      the MBean object name.
     *
     * @return  the MBean object name.
     *
     * @throws  IllegalArgumentException  if the interface class is invalid.
     * @throws  MBeanException            if unable to register the MBean.
     */
    public static <T> ObjectName register(Class<T> interfaceClass,
            ObjectName objectName) throws MBeanException {

        // Check if the interface class is valid.
        if (interfaceClass == null) {
            throw new IllegalArgumentException(
                    "The interface class is invalid.");
        }

        try {

            // Get the MBean server.
            MBeanServer mBeanServer = ManagementFactory
                    .getPlatformMBeanServer();

            // Check if the MBean is not registered with the MBean server.
            if (!mBeanServer.isRegistered(objectName)) {

                // Register the MBean with the MBean server.
                ObjectInstance objectInstance = mBeanServer.registerMBean(
                        createObject(interfaceClass), objectName);

                // Get the object name for the registered MBean.
                objectName = objectInstance.getObjectName();
            }
        } catch (Exception e) {
            throw new MBeanException(e, "Unable to register the MBean.");
        }

        return objectName;
    }

    /**
     * Get the MBean object name.
     *
     * @param  interfaceClass  the MBean interface class.
     *
     * @return  the MBean object name.
     *
     * @throws  IllegalArgumentException  if the interface class is invalid or
     *                                    unable to get the MBean object name.
     */
    public static ObjectName getObjectName(Class interfaceClass) {
        return getObjectName(interfaceClass, null);
    }

    /**
     * Get the MBean object name.
     *
     * @param  interfaceClass  the MBean interface class.
     * @param  key             the property key.
     * @param  value           the property value.
     *
     * @return  the MBean object name.
     *
     * @throws  IllegalArgumentException  if the interface class is invalid or
     *                                    unable to get the MBean object name.
     */
    public static ObjectName getObjectName(Class interfaceClass,
            String key, String value) {

        // Set the table.
        Hashtable<String, String> table = new Hashtable<String, String>();
        table.put(key, value);

        return getObjectName(interfaceClass, table);
    }

    /**
     * Get the MBean object name.
     *
     * @param  interfaceClass  the MBean interface class.
     * @param  table           the properties table.
     *
     * @return  the MBean object name.
     *
     * @throws  IllegalArgumentException  if the interface class is invalid or
     *                                    unable to get the MBean object name.
     */
    public static ObjectName getObjectName(Class interfaceClass,
            Hashtable<String, String> table) {

        // Check if the interface class is valid.
        if (interfaceClass == null) {
            throw new IllegalArgumentException(
                    "The interface class is invalid.");
        }

        ObjectName objectName;

        try {

            // Check if the table is null.
            if (table == null) {

                // Initialize the table.
                table = new Hashtable<String, String>();
            }

            // Add the "type" to the table.
            table.put("type", interfaceClass.getSimpleName());

            // Get the MBean object name.
            objectName = ObjectName.getInstance(interfaceClass.getPackage()
                    .getName(), table);
        } catch (MalformedObjectNameException e) {
            throw new IllegalArgumentException(
                    "Unable to get the MBean object name for "
                            + interfaceClass.getName() + ".", e);
        }

        return objectName;
    }

    /**
     * Create the MBean object.
     *
     * @param  interfaceClass  the MBean interface class.
     *
     * @return  the MBean object.
     */
    private static <T> T createObject(Class<T> interfaceClass) {

        ServiceLoader<T> loader = ServiceLoader.load(interfaceClass);

        T object = null;

        // Loop through the services.
        for (T loadedObject : loader) {

            // Check if a factory has not been found.
            if (object == null) {

                // Set the factory.
                object = loadedObject;
            } else {
                throw new IllegalArgumentException(
                        "More than one MBean object found.");
            }
        }

        // Check if a object has not been found.
        if (object == null) {
            throw new IllegalArgumentException("No MBean object found.");
        }

        return object;
    }
}

Related Tutorials