001    /*****************************************************************************
002     * Copyright (C) NanoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *                                                                           *
008     * Original code by Joerg Schaible                                           *
009     *****************************************************************************/
010    
011    package org.picocontainer.gems.jmx;
012    
013    import javax.management.MBeanInfo;
014    
015    import org.picocontainer.PicoContainer;
016    
017    
018    /**
019     * Abstract base class for MBeanInfoProvider that search MBeanInfo in the PicoContainer registered with a key that
020     * follows naming conventions.
021     * @author Jörg Schaible
022     */
023    public abstract class AbstractNamingConventionMBeanInfoProvider implements MBeanInfoProvider {
024    
025        /**
026         * Locate a MBeanInfo as component in a PicoContainer. If no component is registered using the name of the MBeanInfo
027         * as key, the method turns the name into a type and searches again.
028         * @param mBeanInfoName The name of the {@link MBeanInfo} used as key.
029         * @param picoContainer The {@link PicoContainer} used for the lookup.
030         * @param classLoader The {@link ClassLoader} used to load the type of the key.
031         * @return Returns the MBeanInfo instance or <code>null</code>.
032         */
033        protected MBeanInfo instantiateMBeanInfo(
034                final String mBeanInfoName, final PicoContainer picoContainer, ClassLoader classLoader) {
035            MBeanInfo mBeanInfo = null;
036            try {
037                mBeanInfo = (MBeanInfo)picoContainer.getComponent(mBeanInfoName);
038            } catch (final ClassCastException e) {
039                // wrong type, search goes on
040            }
041            if (mBeanInfo == null) {
042                if (classLoader == null) {
043                    classLoader = Thread.currentThread().getContextClassLoader();
044                }
045                try {
046                    final Class mBeanInfoType = classLoader.loadClass(mBeanInfoName);
047                    if (MBeanInfo.class.isAssignableFrom(mBeanInfoType)) {
048                        mBeanInfo = (MBeanInfo)picoContainer.getComponent(mBeanInfoType);
049                    }
050                } catch (final ClassNotFoundException e) {
051                    // no such class
052                }
053            }
054            return mBeanInfo;
055        }
056    
057    }