changeset 644:394f8798ad69

Handle SIGTERM/SIGINT in Swing GUI. This fixes PR1181. It allows us to kill ourself - via the "Kill Application" context menu - without nasty sideeffects such as throwing exceptions. Also, CTRL+C in the terminal where client started is properly handled with this. Reviewed-by: vanaltj, omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-September/003468.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Fri, 28 Sep 2012 15:54:19 +0200
parents b1f98c2a17d5
children 9702e8f0da26
files client/core/src/main/java/com/redhat/thermostat/client/ui/MainWindow.java
diffstat 1 files changed, 15 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/MainWindow.java	Fri Sep 28 16:40:23 2012 +0200
+++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/MainWindow.java	Fri Sep 28 15:54:19 2012 +0200
@@ -91,6 +91,8 @@
 import javax.swing.tree.TreePath;
 import javax.swing.tree.TreeSelectionModel;
 
+import sun.misc.Signal;
+
 import com.redhat.thermostat.client.internal.MainView;
 import com.redhat.thermostat.client.locale.LocaleResources;
 import com.redhat.thermostat.client.osgi.service.BasicView;
@@ -325,6 +327,7 @@
             new DefaultMutableTreeNode(localize(LocaleResources.MAIN_WINDOW_TREE_ROOT_NAME));
     private final DefaultTreeModel publishedTreeModel = new DefaultTreeModel(publishedRoot);
 
+    @SuppressWarnings("restriction")
     public MainWindow() {
         super();
 
@@ -376,6 +379,10 @@
         setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
         addWindowListener(shutdownAction);
 
+        // Handle SIGTERM/SIGINT properly
+        Signal.handle(new Signal("TERM"), shutdownAction);
+        Signal.handle(new Signal("INT"), shutdownAction);
+        
         addComponentListener(new ComponentAdapter() {
 
             @Override
@@ -536,7 +543,8 @@
         return result;
     }
 
-    public class ShutdownClient extends WindowAdapter implements java.awt.event.ActionListener {
+    @SuppressWarnings("restriction")
+    public class ShutdownClient extends WindowAdapter implements java.awt.event.ActionListener, sun.misc.SignalHandler {
 
         @Override
         public void windowClosing(WindowEvent e) {
@@ -547,11 +555,17 @@
         public void actionPerformed(java.awt.event.ActionEvent e) {
             shutdown();
         }
+        
+        @Override
+        public void handle(Signal arg0) {
+            shutdown();
+        }
 
         private void shutdown() {
             dispose();
             fireViewAction(Action.SHUTDOWN);
         }
+
     }
 
     private static class AgentVmTreeCellRenderer extends DefaultTreeCellRenderer {