Acquires the configured or default Helios target MBeanServer. - Java javax.management

Java examples for javax.management:MBean

Description

Acquires the configured or default Helios target MBeanServer.

Demo Code

/**/* w ww .j av a  2  s  .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. 
 *
 */
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.DynamicMBean;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerFactory;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.util.regex.*;
import javax.management.*;

public class Main{
    /** The property name where the jmx default domain is referenced */
    public static final String JMX_DOMAIN_PROPERTY = "org.helios.jmx.domain";
    /**
     * Acquires the configured or default Helios target MBeanServer.
     * @return An MBeanServer.
     */
    public static MBeanServer getHeliosMBeanServer() {
        MBeanServer server = null;
        String jmxDomain = ConfigurationHelper.getEnvThenSystemProperty(
                JMX_DOMAIN_PROPERTY, null);
        if (jmxDomain != null) {
            server = getLocalMBeanServer(jmxDomain, true);
        }
        if (server == null) {
            return ManagementFactory.getPlatformMBeanServer();
        }

        return server;
    }
    /**
     * Returns an MBeanConnection for an in-vm MBeanServer that has the specified default domain.
     * @param domain The default domain of the requested MBeanServer.
     * @return The located MBeanServerConnection or null if one cannot be located. 
     */
    public static MBeanServer getLocalMBeanServer(String domain) {
        return getLocalMBeanServer(domain, true);
    }
    /**
     * Searches for a matching MBeanServer in the passed list of domains and returns the first located.
     * If one cannot be located a null will be returned. 
     * @param domains The default domain of the requested MBeanServer.
     * @return The located MBeanServerConnection or null if one cannot be found.
     */
    public static MBeanServer getLocalMBeanServer(String... domains) {
        return getLocalMBeanServer(true, domains);
    }
    /**
     * Searches for a matching MBeanServer in the passed list of domains and returns the first located.
     * If one cannot be located, returnNullIfNotFound will either cause a null to be returned, or a RuntimeException. 
     * @param returnNullIfNotFound If true, returns a null if a matching MBeanServer cannot be found. Otherwise, throws a RuntimeException.
     * @param domains The default domain of the requested MBeanServer.
     * @return The located MBeanServerConnection or null if one cannot be found and returnNullIfNotFound is true.
     */
    public static MBeanServer getLocalMBeanServer(
            boolean returnNullIfNotFound, String... domains) {
        MBeanServer server = null;
        StringBuilder buff = new StringBuilder();
        for (String domain : domains) {
            server = getLocalMBeanServer(domain);
            buff.append(domain).append(",");
            if (server != null)
                return server;
        }
        if (returnNullIfNotFound) {
            return null;
        }
        throw new RuntimeException("No MBeanServer located for domains ["
                + buff.toString() + "]");
    }
    /**
     * Returns an MBeanConnection for an in-vm MBeanServer that has the specified default domain.
     * @param domain The default domain of the requested MBeanServer.
     * @param returnNullIfNotFound If true, returns a null if a matching MBeanServer cannot be found. Otherwise, throws a RuntimeException. 
     * @return The located MBeanServerConnection or null if one cannot be found and returnNullIfNotFound is true. 
     */
    public static MBeanServer getLocalMBeanServer(String domain,
            boolean returnNullIfNotFound) {
        if (domain == null || domain.equals("")
                || domain.equalsIgnoreCase("DefaultDomain")
                || domain.equalsIgnoreCase("Default")) {
            return ManagementFactory.getPlatformMBeanServer();
        }
        List<MBeanServer> servers = MBeanServerFactory
                .findMBeanServer(null);
        for (MBeanServer server : servers) {
            if (server.getDefaultDomain().equals(domain))
                return server;
        }
        if (returnNullIfNotFound) {
            return null;
        }
        throw new RuntimeException("No MBeanServer located for domain ["
                + domain + "]");
    }
}

Related Tutorials