Java MBean resolveServerClassLoader(Map env, MBeanServer mbs)

Here you can find the source of resolveServerClassLoader(Map env, MBeanServer mbs)

Description

Get the Connector Server default class loader.

License

Open Source License

Parameter

Parameter Description
env Environment attributes.
mbs The MBeanServer for which the connector server provides remote access.

Return

the connector server's default class loader.

Declaration

public static ClassLoader resolveServerClassLoader(Map env, MBeanServer mbs) throws InstanceNotFoundException 

Method Source Code

//package com.java2s;
/*// ww  w . j  a v a  2s  . c om
 * @(#)EnvHelp.java   1.33 04/02/13
 * 
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.util.Map;

import javax.management.ObjectName;
import javax.management.MBeanServer;
import javax.management.InstanceNotFoundException;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServerFactory;

public class Main {
    /**
     * <p>Name of the attribute that specifies a default class loader 
     * object.
     * The value associated with this attribute is a ClassLoader object</p>
     */
    private static final String DEFAULT_CLASS_LOADER = JMXConnectorFactory.DEFAULT_CLASS_LOADER;
    /**
     * <p>Name of the attribute that specifies a default class loader 
     *    ObjectName.
     * The value associated with this attribute is an ObjectName object</p>
     */
    private static final String DEFAULT_CLASS_LOADER_NAME = JMXConnectorServerFactory.DEFAULT_CLASS_LOADER_NAME;

    /**
     * Get the Connector Server default class loader.
     * <p>
     * Returns:
     * <p>
     * <ul>
     * <li>
     *     The ClassLoader object found in <var>env</var> for
     *     <tt>jmx.remote.default.class.loader</tt>, if any.
     * </li>
     * <li>
     *     The ClassLoader pointed to by the ObjectName found in
     *     <var>env</var> for <tt>jmx.remote.default.class.loader.name</tt>,
     *     and registered in <var>mbs</var> if any.
     * </li>
     * <li>
     *     The current thread's context classloader otherwise.
     * </li>
     * </ul>
     *
     * @param env Environment attributes.
     * @param mbs The MBeanServer for which the connector server provides
     * remote access.
     *
     * @return the connector server's default class loader.
     *
     * @exception IllegalArgumentException if one of the following is true:
     * <ul>
     * <li>both
     *     <tt>jmx.remote.default.class.loader</tt> and
     *     <tt>jmx.remote.default.class.loader.name</tt> are specified,
     * </li>
     * <li>or
     *     <tt>jmx.remote.default.class.loader</tt> is not
     *     an instance of {@link ClassLoader},
     * </li>
     * <li>or
     *     <tt>jmx.remote.default.class.loader.name</tt> is not
     *     an instance of {@link ObjectName},
     * </li>
     * <li>or
     *     <tt>jmx.remote.default.class.loader.name</tt> is specified
     *     but <var>mbs</var> is null.
     * </li>
     * @exception InstanceNotFoundException if
     * <tt>jmx.remote.default.class.loader.name</tt> is specified
     * and the ClassLoader MBean is not found in <var>mbs</var>.
     */
    public static ClassLoader resolveServerClassLoader(Map env, MBeanServer mbs) throws InstanceNotFoundException {

        if (env == null)
            return Thread.currentThread().getContextClassLoader();

        Object loader = env.get(DEFAULT_CLASS_LOADER);
        Object name = env.get(DEFAULT_CLASS_LOADER_NAME);

        if (loader != null && name != null) {
            final String msg = "Only one of " + DEFAULT_CLASS_LOADER + " or " + DEFAULT_CLASS_LOADER_NAME
                    + " should be specified.";
            throw new IllegalArgumentException(msg);
        }

        if (loader == null && name == null)
            return Thread.currentThread().getContextClassLoader();

        if (loader != null) {
            if (loader instanceof ClassLoader) {
                return (ClassLoader) loader;
            } else {
                final String msg = "ClassLoader object is not an instance of " + ClassLoader.class.getName() + " : "
                        + loader.getClass().getName();
                throw new IllegalArgumentException(msg);
            }
        }

        ObjectName on;
        if (name instanceof ObjectName) {
            on = (ObjectName) name;
        } else {
            final String msg = "ClassLoader name is not an instance of " + ObjectName.class.getName() + " : "
                    + name.getClass().getName();
            throw new IllegalArgumentException(msg);
        }

        if (mbs == null)
            throw new IllegalArgumentException("Null MBeanServer object");

        return mbs.getClassLoader(on);
    }
}

Related

  1. readAttribute(MBeanServer mbeanServer, String mbeanName, String attributeName)
  2. registerMBean(MBeanServer mbs, Object object, ObjectName name, boolean unreg)
  3. registerMBean(MBeanServer server, String agentName, Map attrs, Object mbeanObject)
  4. registerMBean(Object bean, MBeanServer mBeanServer, ObjectName objectName)
  5. removeTimerNotification(ObjectName timer, Integer id, MBeanServer server)
  6. setQueryExpServer(QueryExp query, MBeanServer toSet)
  7. toString(MBeanParameterInfo[] pinfo)
  8. unregisterMBean(MBeanServer mbs, ObjectName name)