changeset 628:b3f8bfcea78b

Add kill vm error message pop-up. This patch adds error reporting if something fails during kill vm operations. Tests added accordingly. Reviewed-by: vanaltj, neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-September/003240.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Wed, 19 Sep 2012 13:11:57 +0200
parents 7252a5f21644
children bc879863f84e
files client/killvm/src/main/java/com/redhat/thermostat/client/killvm/internal/Activator.java client/killvm/src/main/java/com/redhat/thermostat/client/killvm/internal/KillVMAction.java client/killvm/src/main/java/com/redhat/thermostat/client/killvm/internal/SwingVMKilledListener.java client/killvm/src/main/java/com/redhat/thermostat/client/killvm/internal/VMKilledListener.java client/killvm/src/main/java/com/redhat/thermostat/client/killvm/locale/LocaleResources.java client/killvm/src/main/resources/com/redhat/thermostat/client/killvm/locale/strings.properties client/killvm/src/test/java/com/redhat/thermostat/client/killvm/internal/KillVMActionTest.java client/killvm/src/test/java/com/redhat/thermostat/client/killvm/internal/SwingVMKilledListenerTest.java
diffstat 8 files changed, 216 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- a/client/killvm/src/main/java/com/redhat/thermostat/client/killvm/internal/Activator.java	Wed Sep 19 10:08:17 2012 +0200
+++ b/client/killvm/src/main/java/com/redhat/thermostat/client/killvm/internal/Activator.java	Wed Sep 19 13:11:57 2012 +0200
@@ -45,7 +45,7 @@
 
     @Override
     public void start(BundleContext context) throws Exception {
-        context.registerService(VMContextAction.class.getName(), new KillVMAction(), null);
+        context.registerService(VMContextAction.class.getName(), new KillVMAction(new SwingVMKilledListener()), null);
     }
 
     @Override
--- a/client/killvm/src/main/java/com/redhat/thermostat/client/killvm/internal/KillVMAction.java	Wed Sep 19 10:08:17 2012 +0200
+++ b/client/killvm/src/main/java/com/redhat/thermostat/client/killvm/internal/KillVMAction.java	Wed Sep 19 13:11:57 2012 +0200
@@ -45,6 +45,7 @@
 import com.redhat.thermostat.common.appctx.ApplicationContext;
 import com.redhat.thermostat.common.command.Request;
 import com.redhat.thermostat.common.command.Request.RequestType;
+import com.redhat.thermostat.common.command.RequestResponseListener;
 import com.redhat.thermostat.common.dao.DAOFactory;
 import com.redhat.thermostat.common.dao.VmRef;
 import com.redhat.thermostat.common.locale.Translate;
@@ -60,10 +61,15 @@
     private static final String RECEIVER = "com.redhat.thermostat.agent.killvm.internal.KillVmReceiver";
     private final DAOFactory dao;
     private final Translate t;
+    private final RequestResponseListener listener;
 
-    public KillVMAction() {
+    public KillVMAction(RequestResponseListener listener) {
+        if (listener == null) {
+            throw new IllegalArgumentException("Listener can't be null");
+        }
         this.dao = ApplicationContext.getInstance().getDAOFactory();
         this.t = LocaleResources.createLocalizer();
+        this.listener = listener;
     }
 
     @Override
@@ -85,7 +91,7 @@
         Request murderer = getKillRequest(target);
         murderer.setParameter("vm-id", reference.getIdString());
         murderer.setReceiver(RECEIVER);
-        murderer.addListener(new VMKilledListener());
+        murderer.addListener(listener);
 
         RequestQueue queue = OSGIUtils.getInstance().getService(RequestQueue.class);
         queue.putRequest(murderer);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/killvm/src/main/java/com/redhat/thermostat/client/killvm/internal/SwingVMKilledListener.java	Wed Sep 19 13:11:57 2012 +0200
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat 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, or (at your
+ * option) any later version.
+ *
+ * Thermostat 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 Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.client.killvm.internal;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.swing.JOptionPane;
+import javax.swing.SwingUtilities;
+
+import com.redhat.thermostat.client.killvm.locale.LocaleResources;
+import com.redhat.thermostat.common.command.Request;
+import com.redhat.thermostat.common.command.RequestResponseListener;
+import com.redhat.thermostat.common.command.Response;
+import com.redhat.thermostat.common.locale.Translate;
+
+public class SwingVMKilledListener implements RequestResponseListener {
+
+    private final Translate t;
+    private static final Logger logger = Logger
+            .getLogger(SwingVMKilledListener.class.getName());
+
+    public SwingVMKilledListener() {
+        this.t = LocaleResources.createLocalizer();
+    }
+    
+    @Override
+    public void fireComplete(Request request, Response response) {
+        switch (response.getType()) {
+        case EXCEPTION:
+            logger.log(Level.SEVERE,
+                    "Exception response from kill VM request. Command channel failure?");
+            showErrorMessage(t.localize(LocaleResources.KILL_ACTION_EXCEPTION_RESPONSE_MSG));
+            break;
+        case ERROR:
+            String vmId = request.getParameter("vm-id");
+            logger.log(Level.SEVERE,
+                    "Kill request error for VM ID "
+                            + vmId);
+            showErrorMessage(t.localize(LocaleResources.KILL_ACTION_ERROR_RESPONSE_MSG, vmId));
+            break;
+        case PONG: // fall-through, also OK :)
+        case OK:
+            logger.log(Level.INFO,
+                    "VM with id " + request.getParameter("vm-id")
+                            + " killed on host "
+                            + request.getTarget().toString());
+            break;
+        default:
+            logger.log(Level.WARNING, "Unknown result from KILL VM command.");
+            break;
+        }
+    }
+    
+    /* 
+     * protected for testing
+     * 
+     */
+    protected void showErrorMessage(final String message) {
+        SwingUtilities.invokeLater(new Runnable() {
+            @Override
+            public void run() {
+                JOptionPane.showMessageDialog(null, message, "",
+                        JOptionPane.ERROR_MESSAGE);
+            }
+        });
+    }
+}
--- a/client/killvm/src/main/java/com/redhat/thermostat/client/killvm/internal/VMKilledListener.java	Wed Sep 19 10:08:17 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-/*
- * Copyright 2012 Red Hat, Inc.
- *
- * This file is part of Thermostat.
- *
- * Thermostat 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, or (at your
- * option) any later version.
- *
- * Thermostat 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 Thermostat; see the file COPYING.  If not see
- * <http://www.gnu.org/licenses/>.
- *
- * Linking this code with other modules is making a combined work
- * based on this code.  Thus, the terms and conditions of the GNU
- * General Public License cover the whole combination.
- *
- * As a special exception, the copyright holders of this code give
- * you permission to link this code with independent modules to
- * produce an executable, regardless of the license terms of these
- * independent modules, and to copy and distribute the resulting
- * executable under terms of your choice, provided that you also
- * meet, for each linked independent module, the terms and conditions
- * of the license of that module.  An independent module is a module
- * which is not derived from or based on this code.  If you modify
- * this code, you may extend this exception to your version of the
- * library, but you are not obligated to do so.  If you do not wish
- * to do so, delete this exception statement from your version.
- */
-
-package com.redhat.thermostat.client.killvm.internal;
-
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import com.redhat.thermostat.common.command.Request;
-import com.redhat.thermostat.common.command.RequestResponseListener;
-import com.redhat.thermostat.common.command.Response;
-
-public class VMKilledListener implements RequestResponseListener {
-
-    private static final Logger logger = Logger
-            .getLogger(VMKilledListener.class.getName());
-
-    @Override
-    public void fireComplete(Request request, Response response) {
-        switch (response.getType()) {
-        case EXCEPTION:
-            logger.log(Level.SEVERE,
-                    "Exception response from kill VM request. Command channel failure?");
-            break;
-        case ERROR:
-            logger.log(Level.SEVERE,
-                    "Kill request error for VM ID "
-                            + request.getParameter("vm-id"));
-            break;
-        case PONG: // fall-through, also OK :)
-        case OK:
-            // TODO: Report this to user somehow (notification?)
-            logger.log(Level.INFO,
-                    "VM with id " + request.getParameter("vm-id")
-                            + " killed on host "
-                            + request.getTarget().toString());
-            break;
-        default:
-            logger.log(Level.WARNING, "Unknown result from KILL VM command.");
-            break;
-        }
-    }
-}
--- a/client/killvm/src/main/java/com/redhat/thermostat/client/killvm/locale/LocaleResources.java	Wed Sep 19 10:08:17 2012 +0200
+++ b/client/killvm/src/main/java/com/redhat/thermostat/client/killvm/locale/LocaleResources.java	Wed Sep 19 13:11:57 2012 +0200
@@ -42,6 +42,8 @@
 
     ACTION_NAME,
     ACTION_DESCRIPTION,
+    KILL_ACTION_EXCEPTION_RESPONSE_MSG,
+    KILL_ACTION_ERROR_RESPONSE_MSG,
     MISSING_INFO;
 
     public static final String RESOURCE_BUNDLE =
--- a/client/killvm/src/main/resources/com/redhat/thermostat/client/killvm/locale/strings.properties	Wed Sep 19 10:08:17 2012 +0200
+++ b/client/killvm/src/main/resources/com/redhat/thermostat/client/killvm/locale/strings.properties	Wed Sep 19 13:11:57 2012 +0200
@@ -1,3 +1,5 @@
 ACTION_NAME = Kill Application
 ACTION_DESCRIPTION = Kill the selected VM Process
-MISSING_INFO = Missing Information
\ No newline at end of file
+MISSING_INFO = Missing Information
+KILL_ACTION_EXCEPTION_RESPONSE_MSG = Channel Transport Error. Please check your configuration!
+KILL_ACTION_ERROR_RESPONSE_MSG = Failed to kill VM with pid {0}
\ No newline at end of file
--- a/client/killvm/src/test/java/com/redhat/thermostat/client/killvm/internal/KillVMActionTest.java	Wed Sep 19 10:08:17 2012 +0200
+++ b/client/killvm/src/test/java/com/redhat/thermostat/client/killvm/internal/KillVMActionTest.java	Wed Sep 19 13:11:57 2012 +0200
@@ -83,7 +83,7 @@
         ApplicationContextUtil.resetApplicationContext();
         factory = mock(DAOFactory.class);
         ApplicationContext.getInstance().setDAOFactory(factory);
-        action = new KillVMAction();
+        action = new KillVMAction(new SwingVMKilledListener());
     }
 
     @After
@@ -124,7 +124,7 @@
         when(factory.getAgentInfoDAO()).thenReturn(agentDao);
 
         final Request req = mock(Request.class);
-        KillVMAction action = new KillVMAction() {
+        KillVMAction action = new KillVMAction(new SwingVMKilledListener()) {
             @Override
             Request getKillRequest(InetSocketAddress target) {
                 return req;
@@ -140,7 +140,7 @@
                 .forClass(String.class);
         verify(req).setParameter(vmIdParamCaptor.capture(), any(String.class));
         assertEquals("vm-id", vmIdParamCaptor.getValue());
-        verify(req).addListener(isA(VMKilledListener.class));
+        verify(req).addListener(isA(SwingVMKilledListener.class));
         ArgumentCaptor<String> receiverCaptor = ArgumentCaptor
                 .forClass(String.class);
         verify(req).setReceiver(receiverCaptor.capture());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/killvm/src/test/java/com/redhat/thermostat/client/killvm/internal/SwingVMKilledListenerTest.java	Wed Sep 19 13:11:57 2012 +0200
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat 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, or (at your
+ * option) any later version.
+ *
+ * Thermostat 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 Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.client.killvm.internal;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.net.SocketAddress;
+
+import org.junit.Test;
+
+import com.redhat.thermostat.common.command.Request;
+import com.redhat.thermostat.common.command.Response;
+import com.redhat.thermostat.common.command.Response.ResponseType;
+
+public class SwingVMKilledListenerTest {
+    
+    @Test
+    public void exceptionShowsError() {
+        ResponseActionListener listener = new ResponseActionListener();
+        Request request = mock(Request.class); 
+        Response resp = new Response(ResponseType.EXCEPTION);
+        listener.fireComplete(request, resp);
+        assertTrue(listener.isActionPerformed());
+    }
+    
+    @Test
+    public void errorShowsError() {
+        ResponseActionListener listener = new ResponseActionListener();
+        Request request = mock(Request.class); 
+        Response resp = new Response(ResponseType.ERROR);
+        listener.fireComplete(request, resp);
+        assertTrue(listener.isActionPerformed());
+    }
+    
+    @Test
+    public void okShowsNoMessage() {
+        ResponseActionListener listener = new ResponseActionListener();
+        Request request = mock(Request.class);
+        SocketAddress addr = mock(SocketAddress.class);
+        when(request.getTarget()).thenReturn(addr);
+        Response resp = new Response(ResponseType.OK);
+        listener.fireComplete(request, resp);
+        assertFalse(listener.isActionPerformed());
+    }
+    
+    private class ResponseActionListener extends SwingVMKilledListener {
+
+        private boolean actionPerformed = false;
+        
+        public boolean isActionPerformed() {
+            return actionPerformed;
+        }
+
+        @Override
+        protected void showErrorMessage(final String message) {
+            actionPerformed = true;
+        }
+        
+    }
+}