Get the MBean object name. - Java javax.management

Java examples for javax.management:MBean

Description

Get the MBean object name.

Demo Code

/**/*from w  w  w  .j  ava 2  s .  co 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.util.Hashtable;

import javax.management.MalformedObjectNameException;

import javax.management.ObjectName;

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

    /**
     * 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;
    }
}

Related Tutorials