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