001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (c) 2004-2008 Paul Ferraro
004 * 
005 * This library is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published by the 
007 * Free Software Foundation; either version 2.1 of the License, or (at your 
008 * option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this library; if not, write to the Free Software Foundation, 
017 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018 * 
019 * Contact: ferraro@users.sourceforge.net
020 */
021package net.sf.hajdbc.util.servlet;
022
023import java.lang.management.ManagementFactory;
024
025import javax.management.JMException;
026import javax.management.MBeanServer;
027import javax.management.ObjectName;
028import javax.servlet.ServletContext;
029import javax.servlet.ServletContextEvent;
030import javax.servlet.ServletContextListener;
031
032import net.sf.hajdbc.DatabaseClusterFactory;
033
034/**
035 * Utility to automatically shutdown a list of database clusters scoped to a servlet context.
036 * The clusters to shutdown are defined in a comma delimited init parameter: <code>ha-jdbc.clusters</code>
037 * 
038 * @author Paul Ferraro
039 */
040public class DatabaseClusterShutdownContextListener implements ServletContextListener
041{
042        private static final String CLUSTERS = "ha-jdbc.clusters";
043        private static final String DELIMITER = ",";
044        
045        /**
046         * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
047         */
048        @Override
049        public void contextDestroyed(ServletContextEvent event)
050        {
051                ServletContext context = event.getServletContext();
052                
053                String clusters = context.getInitParameter(CLUSTERS);
054                
055                if (clusters != null)
056                {
057                        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
058                        
059                        for (String cluster: clusters.split(DELIMITER))
060                        {
061                                try
062                                {
063                                        ObjectName name = DatabaseClusterFactory.getObjectName(cluster.trim());
064                                        
065                                        if (server.isRegistered(name))
066                                        {
067                                                server.unregisterMBean(name);
068                                        }
069                                }
070                                catch (JMException e)
071                                {
072                                        event.getServletContext().log(e.getMessage(), e);
073                                }
074                        }
075                }
076        }
077
078        /**
079         * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
080         */
081        @Override
082        public void contextInitialized(ServletContextEvent event)
083        {
084                // Do nothing
085        }
086}