diff -r 000000000000 -r 1090e2141798 org/sonews/daemon/ShutdownHook.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/org/sonews/daemon/ShutdownHook.java Wed Jul 01 10:48:22 2009 +0200 @@ -0,0 +1,83 @@ +/* + * SONEWS News Server + * see AUTHORS for the list of contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sonews.daemon; + +import java.sql.SQLException; +import java.util.Map; + +/** + * Will force all other threads to shutdown cleanly. + * @author Christian Lins + * @since sonews/0.5.0 + */ +class ShutdownHook extends Thread +{ + + /** + * Called when the JVM exits. + */ + @Override + public void run() + { + System.out.println("sonews: Trying to shutdown all threads..."); + + Map threadsMap = Thread.getAllStackTraces(); + for(Thread thread : threadsMap.keySet()) + { + // Interrupt the thread if it's a AbstractDaemon + AbstractDaemon daemon; + if(thread instanceof AbstractDaemon && thread.isAlive()) + { + try + { + daemon = (AbstractDaemon)thread; + daemon.shutdownNow(); + } + catch(SQLException ex) + { + System.out.println("sonews: " + ex); + } + } + } + + for(Thread thread : threadsMap.keySet()) + { + AbstractDaemon daemon; + if(thread instanceof AbstractDaemon && thread.isAlive()) + { + daemon = (AbstractDaemon)thread; + System.out.println("sonews: Waiting for " + daemon + " to exit..."); + try + { + daemon.join(500); + } + catch(InterruptedException ex) + { + System.out.println(ex.getLocalizedMessage()); + } + } + } + + // We have notified all not-sleeping AbstractDaemons of the shutdown; + // all other threads can be simply purged on VM shutdown + + System.out.println("sonews: Clean shutdown."); + } + +}