[jetrix-cvs] SF.net SVN: jetrix:[789] jetrix/trunk/src/java/net/jetrix
Brought to you by:
smanux
From: <sm...@us...> - 2009-02-13 14:30:21
|
Revision: 789 http://jetrix.svn.sourceforge.net/jetrix/?rev=789&view=rev Author: smanux Date: 2009-02-13 14:30:13 +0000 (Fri, 13 Feb 2009) Log Message: ----------- Intercept the INT and TERM signals to shutdown gracefully the server Modified Paths: -------------- jetrix/trunk/src/java/net/jetrix/Server.java Added Paths: ----------- jetrix/trunk/src/java/net/jetrix/SystemSignal.java Modified: jetrix/trunk/src/java/net/jetrix/Server.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/Server.java 2009-02-13 12:53:56 UTC (rev 788) +++ jetrix/trunk/src/java/net/jetrix/Server.java 2009-02-13 14:30:13 UTC (rev 789) @@ -52,7 +52,7 @@ private Server() { // add the stop hook - Runtime.getRuntime().addShutdownHook(new Thread("StopHook") + Thread hook = new Thread("StopHook") { public void run() { @@ -62,7 +62,18 @@ instance.stop(); } } - }); + }; + Runtime.getRuntime().addShutdownHook(hook); + + try + { + SystemSignal.handle("INT", hook); + SystemSignal.handle("TERM", hook); + } + catch (Throwable e) + { + log.warning("Unable to hook the system signals: " + e.getMessage()); + } } /** Added: jetrix/trunk/src/java/net/jetrix/SystemSignal.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/SystemSignal.java (rev 0) +++ jetrix/trunk/src/java/net/jetrix/SystemSignal.java 2009-02-13 14:30:13 UTC (rev 789) @@ -0,0 +1,49 @@ +/** + * Jetrix TetriNET Server + * Copyright (C) 2009 Emmanuel Bourg + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.jetrix; + +import sun.misc.Signal; +import sun.misc.SignalHandler; + +/** + * Wrapper around the sun.misc.Signal class to prevent linkage errors. + * + * @author Emmanuel Bourg + * @version $Revision$, $Date$ + */ +class SystemSignal +{ + /** + * Intercepts a system signal and executes the specified hook. + * + * @param signal the signal to be intercepted (INT, KILL, HUP...) + * @param hook the code to be executed + */ + public static void handle(String signal, final Runnable hook) + { + Signal.handle(new Signal(signal), new SignalHandler() + { + public void handle(Signal signal) + { + hook.run(); + } + }); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |