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.ComponentAdapter;
016    import org.picocontainer.PicoContainer;
017    
018    
019    /**
020     * A MBeanInfoProvider that searches for a MBeanInfo instance in the PicoContainer. Keys of both components follow
021     * naming conventions.
022     * @author Jörg Schaible
023     */
024    public class ComponentKeyConventionMBeanInfoProvider extends AbstractNamingConventionMBeanInfoProvider {
025    
026        /**
027         * Use the key of the component to search for a MBeanInfo in the PicoContainer. The matching MBeanInfo must be
028         * stored in the PicoContainer. The key of the MBeanInfo follows the naming scheme
029         * "<ComponentKey>MBeanInfo". The the component's key is a type, the class name is used as prefix
030         * otherwise the string representation of the key. The key part may already end with "MBean" as it would
031         * for components registered with the management interface as key, that follow the JMX naming conventions. As last
032         * resort the calculated key of the MBeanInfo is turned into a type that is used again as lookup key.
033         * @see org.picocontainer.gems.jmx.MBeanInfoProvider#provide(org.picocontainer.PicoContainer,
034         *      org.picocontainer.ComponentAdapter)
035         */
036        public MBeanInfo provide(final PicoContainer picoContainer, final ComponentAdapter componentAdapter) {
037            final Object key = componentAdapter.getComponentKey();
038            final String prefix = key instanceof Class ? ((Class)key).getName() : key.toString();
039            final String mBeanInfoName = prefix + (prefix.endsWith("MBean") ? "Info" : "MBeanInfo");
040            return instantiateMBeanInfo(mBeanInfoName, picoContainer, componentAdapter.getComponentImplementation()
041                    .getClassLoader());
042        }
043    
044    }