Validate the MBean represented by the interface class and object name. - Java javax.management

Java examples for javax.management:MBean

Description

Validate the MBean represented by the interface class and object name.

Demo Code

/**/*w w w .  j  ava  2s. c  om*/
 * 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.io.IOException;

import javax.management.InstanceNotFoundException;
import javax.management.JMX;
import javax.management.MBeanException;

import javax.management.MBeanServerConnection;

import javax.management.ObjectName;

public class Main {
    /**
     * Validate the MBean represented by the interface class and object name.
     *
     * @param  interfaceClass         the MBean interface class.
     * @param  objectName             the MBean object name.
     * @param  mBeanServerConnection  the MBean server connection.
     *
     * @throws  IllegalArgumentException  if the MBean is invalid.
     * @throws  IOException               if unable to validate the MBean.
     */
    protected static void validateMBean(Class interfaceClass,
            ObjectName objectName,
            MBeanServerConnection mBeanServerConnection)
            throws MBeanException {

        try {

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

            // Check if the interface class is not a MXBean interface.
            if (!JMX.isMXBeanInterface(interfaceClass)) {
                throw new IllegalArgumentException("The interface class "
                        + interfaceClass.getName()
                        + " is not a MXBean interface.");
            }

            // Check if the object name is not registered.
            if (!mBeanServerConnection.isRegistered(objectName)) {
                throw new IllegalArgumentException("The object name "
                        + objectName.getCanonicalName()
                        + " is not registered.");
            }

            // Check if the object name is not an instance of the interface class.
            if (!mBeanServerConnection.isInstanceOf(objectName,
                    interfaceClass.getName())) {
                throw new IllegalArgumentException("The object name "
                        + objectName.getCanonicalName()
                        + " is not an instance of the interface class "
                        + interfaceClass.getName() + ".");
            }
        } catch (InstanceNotFoundException e) {
            throw new IllegalArgumentException("The object name "
                    + objectName.getCanonicalName() + " is not found.");
        } catch (IOException e) {
            throw new MBeanException(e,
                    "Unable to validate the MBean represented by the interface class "
                            + interfaceClass.getName()
                            + " and object name "
                            + objectName.getCanonicalName() + ".");
        }
    }
}

Related Tutorials