Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/ExceptionSafePlugin.java
40948 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.tools.jconsole;2526import java.util.HashMap;27import java.util.Map;2829import javax.swing.JOptionPane;30import javax.swing.JPanel;31import javax.swing.SwingWorker;3233import com.sun.tools.jconsole.JConsolePlugin;3435/**36* Proxy that shields GUI from plug-in exceptions.37*38*/39final class ExceptionSafePlugin extends JConsolePlugin {4041private static boolean ignoreExceptions;42private final JConsolePlugin plugin;4344public ExceptionSafePlugin(JConsolePlugin plugin) {45this.plugin = plugin;46}4748@Override49public Map<String, JPanel> getTabs() {50try {51return plugin.getTabs();52} catch (RuntimeException e) {53handleException(e);54}55return new HashMap<>();56}5758@Override59public SwingWorker<?, ?> newSwingWorker() {60try {61return plugin.newSwingWorker();62} catch (RuntimeException e) {63handleException(e);64}65return null;66}6768@Override69public void dispose() {70try {71plugin.dispose();72} catch (RuntimeException e) {73handleException(e);74}75}7677public void executeSwingWorker(SwingWorker<?, ?> sw) {78try {79sw.execute();80} catch (RuntimeException e) {81handleException(e);82}83}8485private void handleException(Exception e) {86if (JConsole.isDebug()) {87System.err.println("Plug-in exception:");88e.printStackTrace();89} else {90if (!ignoreExceptions) {91showExceptionDialog(e);92}93}94}9596private void showExceptionDialog(Exception e) {97Object[] buttonTexts = {98Messages.PLUGIN_EXCEPTION_DIALOG_BUTTON_OK,99Messages.PLUGIN_EXCEPTION_DIALOG_BUTTON_EXIT,100Messages.PLUGIN_EXCEPTION_DIALOG_BUTTON_IGNORE101};102103String message = String.format(104Messages.PLUGIN_EXCEPTION_DIALOG_MESSAGE,105plugin.getClass().getSimpleName(),106String.valueOf(e.getMessage())107);108109int buttonIndex = JOptionPane.showOptionDialog(110null,111message,112Messages.PLUGIN_EXCEPTION_DIALOG_TITLE,113JOptionPane.YES_NO_CANCEL_OPTION,114JOptionPane.ERROR_MESSAGE,115null,116buttonTexts,117buttonTexts[0]118);119120if (buttonIndex == 1) {121System.exit(0);122}123ignoreExceptions = buttonIndex == 2;124}125}126127128