changeset 19432:ce3b6c1ab8a6

6696970: Jconsole becomes unusable if a plugin throws an exception Reviewed-by: mchung, jbachorik
author egahlin
date Fri, 16 Aug 2013 18:58:36 +0200
parents 254c78d54bd9
children 2dade0de0206
files jdk/src/share/classes/sun/tools/jconsole/ExceptionSafePlugin.java jdk/src/share/classes/sun/tools/jconsole/Messages.java jdk/src/share/classes/sun/tools/jconsole/VMPanel.java jdk/src/share/classes/sun/tools/jconsole/resources/messages.properties
diffstat 4 files changed, 142 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/share/classes/sun/tools/jconsole/ExceptionSafePlugin.java	Fri Aug 16 18:58:36 2013 +0200
@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code 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
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package sun.tools.jconsole;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.SwingWorker;
+
+import com.sun.tools.jconsole.JConsolePlugin;
+
+/**
+ * Proxy that shields GUI from plug-in exceptions.
+ *
+ */
+final class ExceptionSafePlugin extends JConsolePlugin {
+
+    private static boolean ignoreExceptions;
+    private final JConsolePlugin plugin;
+
+    public ExceptionSafePlugin(JConsolePlugin plugin) {
+        this.plugin = plugin;
+    }
+
+    @Override
+    public Map<String, JPanel> getTabs() {
+        try {
+            return plugin.getTabs();
+        } catch (RuntimeException e) {
+            handleException(e);
+        }
+        return new HashMap<>();
+    }
+
+    @Override
+    public SwingWorker<?, ?> newSwingWorker() {
+        try {
+            return plugin.newSwingWorker();
+        } catch (RuntimeException e) {
+            handleException(e);
+        }
+        return null;
+    }
+
+    @Override
+    public void dispose() {
+        try {
+            plugin.dispose();
+        } catch (RuntimeException e) {
+            handleException(e);
+        }
+    }
+
+    public void executeSwingWorker(SwingWorker<?, ?> sw) {
+        try {
+            sw.execute();
+        } catch (RuntimeException e) {
+            handleException(e);
+        }
+    }
+
+    private void handleException(Exception e) {
+        if (JConsole.isDebug()) {
+            System.err.println("Plug-in exception:");
+            e.printStackTrace();
+        } else {
+            if (!ignoreExceptions) {
+                showExceptionDialog(e);
+            }
+        }
+    }
+
+    private void showExceptionDialog(Exception e) {
+        Object[] buttonTexts = {
+            Messages.PLUGIN_EXCEPTION_DIALOG_BUTTON_OK,
+            Messages.PLUGIN_EXCEPTION_DIALOG_BUTTON_EXIT,
+            Messages.PLUGIN_EXCEPTION_DIALOG_BUTTON_IGNORE
+        };
+
+        String message = String.format(
+            Messages.PLUGIN_EXCEPTION_DIALOG_MESSAGE,
+            plugin.getClass().getSimpleName(),
+            String.valueOf(e.getMessage())
+        );
+
+        int buttonIndex = JOptionPane.showOptionDialog(
+            null,
+            message,
+            Messages.PLUGIN_EXCEPTION_DIALOG_TITLE,
+            JOptionPane.YES_NO_CANCEL_OPTION,
+            JOptionPane.ERROR_MESSAGE,
+            null,
+            buttonTexts,
+            buttonTexts[0]
+        );
+
+        if (buttonIndex == 1) {
+            System.exit(0);
+        }
+        ignoreExceptions = buttonIndex == 2;
+    }
+}
--- a/jdk/src/share/classes/sun/tools/jconsole/Messages.java	Fri Aug 16 16:53:46 2013 +0200
+++ b/jdk/src/share/classes/sun/tools/jconsole/Messages.java	Fri Aug 16 18:58:36 2013 +0200
@@ -240,6 +240,11 @@
     public static String PLOTTER_ACCESSIBLE_NAME_NO_DATA;
     public static String PLOTTER_SAVE_AS_MENU_ITEM;
     public static String PLOTTER_TIME_RANGE_MENU;
+    public static String PLUGIN_EXCEPTION_DIALOG_BUTTON_EXIT;
+    public static String PLUGIN_EXCEPTION_DIALOG_BUTTON_IGNORE;
+    public static String PLUGIN_EXCEPTION_DIALOG_BUTTON_OK;
+    public static String PLUGIN_EXCEPTION_DIALOG_MESSAGE;
+    public static String PLUGIN_EXCEPTION_DIALOG_TITLE;
     public static String PROBLEM_ADDING_LISTENER;
     public static String PROBLEM_DISPLAYING_MBEAN;
     public static String PROBLEM_INVOKING;
--- a/jdk/src/share/classes/sun/tools/jconsole/VMPanel.java	Fri Aug 16 16:53:46 2013 +0200
+++ b/jdk/src/share/classes/sun/tools/jconsole/VMPanel.java	Fri Aug 16 18:58:36 2013 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -71,7 +71,7 @@
 
     // Each VMPanel has its own instance of the JConsolePlugin
     // A map of JConsolePlugin to the previous SwingWorker
-    private Map<JConsolePlugin, SwingWorker<?, ?>> plugins = null;
+    private Map<ExceptionSafePlugin, SwingWorker<?, ?>> plugins = null;
     private boolean pluginTabsAdded = false;
 
     // Update these only on the EDT
@@ -107,10 +107,10 @@
             }
         }
 
-        plugins = new LinkedHashMap<JConsolePlugin, SwingWorker<?, ?>>();
+        plugins = new LinkedHashMap<ExceptionSafePlugin, SwingWorker<?, ?>>();
         for (JConsolePlugin p : JConsole.getPlugins()) {
             p.setContext(proxyClient);
-            plugins.put(p, null);
+            plugins.put(new ExceptionSafePlugin(p), null);
         }
 
         Utilities.updateTransparency(this);
@@ -566,7 +566,7 @@
             }
 
             // plugin GUI update
-            for (JConsolePlugin p : plugins.keySet()) {
+            for (ExceptionSafePlugin p : plugins.keySet()) {
                 SwingWorker<?, ?> sw = p.newSwingWorker();
                 SwingWorker<?, ?> prevSW = plugins.get(p);
                 // schedule SwingWorker to run only if the previous
@@ -575,7 +575,7 @@
                     if (sw == null || sw.getState() == SwingWorker.StateValue.PENDING) {
                         plugins.put(p, sw);
                         if (sw != null) {
-                            sw.execute();
+                            p.executeSwingWorker(sw);
                         }
                     }
                 }
--- a/jdk/src/share/classes/sun/tools/jconsole/resources/messages.properties	Fri Aug 16 16:53:46 2013 +0200
+++ b/jdk/src/share/classes/sun/tools/jconsole/resources/messages.properties	Fri Aug 16 18:58:36 2013 +0200
@@ -198,6 +198,11 @@
 PLOTTER_ACCESSIBLE_NAME_NO_DATA=No data plotted.
 PLOTTER_SAVE_AS_MENU_ITEM=Save data &as...
 PLOTTER_TIME_RANGE_MENU=&Time Range
+PLUGIN_EXCEPTION_DIALOG_BUTTON_EXIT=Exit
+PLUGIN_EXCEPTION_DIALOG_BUTTON_IGNORE=Ignore
+PLUGIN_EXCEPTION_DIALOG_BUTTON_OK=OK
+PLUGIN_EXCEPTION_DIALOG_MESSAGE=An unexpected exception has occurred in %s:\n\n%s\n\nStart with -debug for details. Ignore will suppress further exceptions.
+PLUGIN_EXCEPTION_DIALOG_TITLE=Plug-in exception
 PROBLEM_ADDING_LISTENER=Problem adding listener
 PROBLEM_DISPLAYING_MBEAN=Problem displaying MBean
 PROBLEM_INVOKING=Problem invoking