Retrieves MBeanInfo on the specified object name. - Java javax.management

Java examples for javax.management:MBean

Description

Retrieves MBeanInfo on the specified object name.

Demo Code

/**/* www  . ja  v  a  2s .  co  m*/
 * Helios, OpenSource Monitoring
 * Brought to you by the Helios Development Group
 *
 * Copyright 2007, Helios Development Group and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This 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 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 *
 */
//package com.java2s;

import java.util.Hashtable;

public class Main {
    /**
     * Retrieves MBeanInfo on the specified object name.
     * @param server The mbean server
     * @param on The object name
     * @return an MBeanInfo
     */
    public static MBeanInfo mbeanInfo(MBeanServerConnection server,
            CharSequence on) {
        try {
            return server.getMBeanInfo(objectName(on));
        } catch (Exception e) {
            throw new RuntimeException("Failed to get MBeanInfo", e);
        }
    }

    /**
     * Creates a new JMX object name.
     * @param on A string type representing the ObjectName string.
     * @return an ObjectName the created ObjectName
     */
    public static ObjectName objectName(CharSequence on) {
        try {
            return new ObjectName(on.toString().trim());
        } catch (Exception e) {
            throw new RuntimeException("Failed to create Object Name", e);
        }
    }

    /**
     * Creates a new JMX object name.
     * @param on An object representing the ObjectName
     * @return an ObjectName the created ObjectName
     */
    public static ObjectName objectName(Object on) {
        try {
            return new ObjectName(on.toString().trim());
        } catch (Exception e) {
            throw new RuntimeException("Failed to create Object Name", e);
        }
    }

    /**
     * Creates a new JMX object name by appending properties on the end of an existing name
     * @param on An existing ObjectName
     * @param props Appended properties in the for {@code key=value}
     * @return an ObjectName the created ObjectName
     */
    public static ObjectName objectName(ObjectName on,
            CharSequence... props) {
        StringBuilder b = new StringBuilder(on.toString());
        try {
            if (props != null) {
                for (CharSequence prop : props) {
                    b.append(",").append(prop);
                }
            }
            return new ObjectName(b.toString());
        } catch (Exception e) {
            throw new RuntimeException(
                    "Failed to create Object Name from [" + b + "]", e);
        }
    }

    /**
     * Creates a new JMX object name.
     * @param domain A string type representing the ObjectName domain
     * @param properties A hash table of the Object name's properties
     * @return an ObjectName the created ObjectName
     */
    public static ObjectName objectName(CharSequence domain,
            Hashtable<String, String> properties) {
        try {
            return new ObjectName(domain.toString(), properties);
        } catch (Exception e) {
            throw new RuntimeException("Failed to create Object Name", e);
        }
    }

    /**
     * Creates a new JMX object name.
     * @param domain The ObjectName domain
     * @param nameValuePairs an (even lengthed) array of name value pairs making up the key properties
     * @return an ObjectName the created ObjectName
     */
    public static ObjectName objectName(CharSequence domain,
            CharSequence... nameValuePairs) {
        if (domain == null || domain.toString().length() < 1)
            throw new IllegalArgumentException(
                    "Null or zero length domain name");
        if (nameValuePairs == null || nameValuePairs.length < 1
                || nameValuePairs.length % 2 != 0) {
            throw new IllegalArgumentException(
                    "Invalid number of namevaluepairs ["
                            + (nameValuePairs == null ? 0
                                    : nameValuePairs.length) + "]");
        }
        try {
            Hashtable<String, String> props = new Hashtable<String, String>();
            for (int i = 0; i < nameValuePairs.length; i++) {
                if (nameValuePairs[i] == null
                        || nameValuePairs[i].toString().length() < 1) {
                    throw new IllegalArgumentException(
                            "Null or blank nameValuePair entry at index ["
                                    + i + "]");
                }
                String key = nameValuePairs[i].toString();
                i++;
                if (nameValuePairs[i] == null
                        || nameValuePairs[i].toString().length() < 1) {
                    throw new IllegalArgumentException(
                            "Null or blank nameValuePair entry at index ["
                                    + i + "]");
                }
                String value = nameValuePairs[i].toString();
                props.put(key, value);
            }
            return new ObjectName(domain.toString(), props);
        } catch (IllegalArgumentException iae) {
            throw iae;
        } catch (Exception e) {
            throw new RuntimeException("Failed to create Object Name", e);
        }
    }
}

Related Tutorials