Example usage for javax.management MBeanServer getClassLoaderFor

List of usage examples for javax.management MBeanServer getClassLoaderFor

Introduction

In this page you can find the example usage for javax.management MBeanServer getClassLoaderFor.

Prototype

public ClassLoader getClassLoaderFor(ObjectName mbeanName) throws InstanceNotFoundException;

Source Link

Document

Return the java.lang.ClassLoader that was used for loading the class of the named MBean.

Usage

From source file:com.meltmedia.cadmium.servlets.ClassLoaderLeakPreventor.java

/**
 * Unregister MBeans loaded by the web application class loader
 *///from   www  .j  ava2 s.c om
protected void unregisterMBeans() {
    try {
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        final Set<ObjectName> allMBeanNames = mBeanServer.queryNames(new ObjectName("*:*"), null);
        for (ObjectName objectName : allMBeanNames) {
            try {
                final ClassLoader mBeanClassLoader = mBeanServer.getClassLoaderFor(objectName);
                if (isWebAppClassLoaderOrChild(mBeanClassLoader)) { // MBean loaded in web application
                    warn("MBean '" + objectName + "' was loaded in web application; unregistering");
                    mBeanServer.unregisterMBean(objectName);
                }
            } catch (Exception e) { // MBeanRegistrationException / InstanceNotFoundException
                error(e);
            }
        }
    } catch (Exception e) { // MalformedObjectNameException
        error(e);
    }
}

From source file:se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor.java

/** Unregister MBeans loaded by the web application class loader */
protected void unregisterMBeans() {
    try {//w  w w.  j a  v a2  s . c  om
        JettyJMXRemover jettyJMXRemover = null;
        // If you enable jmx support in jetty 8 or 9  some mbeans (e.g. for the servletholder or sessionmanager) are instanciated in the web application thread 
        // and a reference to the WebappClassloader is stored in a private ObjectMBean._loader which is unfortunatly not the classloader that loaded the class.
        // So for unregisterMBeans to work even for the jetty mbeans we need to access the MBeanContainer class of the jetty container.  
        try {
            jettyJMXRemover = new JettyJMXRemover(getWebApplicationClassLoader());
        } catch (Exception ex) {
            error(ex);
        }

        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        final Set<ObjectName> allMBeanNames = mBeanServer.queryNames(new ObjectName("*:*"), null);
        for (ObjectName objectName : allMBeanNames) {
            try {
                if (jettyJMXRemover != null && jettyJMXRemover.unregisterJettyJMXBean(objectName)) {
                    continue;
                }
                final ClassLoader mBeanClassLoader = mBeanServer.getClassLoaderFor(objectName);
                if (isWebAppClassLoaderOrChild(mBeanClassLoader)) { // MBean loaded in web application
                    warn("MBean '" + objectName + "' was loaded in web application; unregistering");
                    mBeanServer.unregisterMBean(objectName);
                }
            } catch (Exception e) { // MBeanRegistrationException / InstanceNotFoundException
                error(e);
            }
        }
    } catch (Exception e) { // MalformedObjectNameException
        error(e);
    }
}