001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.jmx;
023    
024    import javax.management.ObjectName;
025    import javax.servlet.ServletContext;
026    
027    import org.granite.config.ServletGraniteConfig;
028    import org.granite.config.flex.ServletServicesConfig;
029    import org.granite.logging.Logger;
030    
031    /**
032     * An utility class that register/unregister GraniteDS MBeans.
033     * 
034     * @author Franck WOLFF
035     */
036    public class GraniteMBeanInitializer {
037            
038            private static final Logger log = Logger.getLogger(GraniteMBeanInitializer.class);
039    
040            public static void registerMBeans(ServletContext context, ServletGraniteConfig gConfig, ServletServicesConfig sConfig) {
041                    String appName = ServletGraniteConfig.getConfig(context).getMBeanContextName();
042            try {
043                ObjectName name = new ObjectName("org.graniteds:type=GraniteDS,app=" + appName);
044                    log.info("Registering MBean: %s", name);
045                OpenMBean mBean = OpenMBean.createMBean(gConfig);
046                    MBeanServerLocator.getInstance().register(mBean, name);
047            }
048            catch (Exception e) {
049                    log.error(e, "Could not register GraniteDS MBean for context: %s", appName);
050            }
051            }       
052    
053            public static void unregisterMBeans(ServletContext context) {
054                    String appName = ServletGraniteConfig.getConfig(context).getMBeanContextName();
055            try {
056                ObjectName name = new ObjectName("org.graniteds:type=GraniteDS,app=" + appName);
057                    log.info("Unregistering MBean: %s", name);
058                    MBeanServerLocator.getInstance().unregister(name);
059            }
060            catch (Exception e) {
061                    log.error(e, "Could not unregister GraniteDS MBean for context: %s", appName);
062            }
063            }       
064    }