jdk/src/jdk.jconsole/share/classes/sun/tools/jconsole/ExceptionSafePlugin.java
author sundar
Mon, 31 Aug 2015 17:51:02 +0530
changeset 32434 769b3d81ae69
parent 25859 3317bb8137f4
permissions -rw-r--r--
8134731: Function.prototype.apply interacts incorrectly with arguments Reviewed-by: attila, hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19432
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
     1
/*
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
     4
 *
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    10
 *
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    15
 * accompanied this code).
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    16
 *
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    20
 *
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    23
 * questions.
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    24
 */
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    25
package sun.tools.jconsole;
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    26
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    27
import java.util.HashMap;
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    28
import java.util.Map;
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    29
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    30
import javax.swing.JOptionPane;
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    31
import javax.swing.JPanel;
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    32
import javax.swing.SwingWorker;
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    33
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    34
import com.sun.tools.jconsole.JConsolePlugin;
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    35
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    36
/**
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    37
 * Proxy that shields GUI from plug-in exceptions.
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    38
 *
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    39
 */
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    40
final class ExceptionSafePlugin extends JConsolePlugin {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    41
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    42
    private static boolean ignoreExceptions;
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    43
    private final JConsolePlugin plugin;
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    44
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    45
    public ExceptionSafePlugin(JConsolePlugin plugin) {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    46
        this.plugin = plugin;
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    47
    }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    48
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    49
    @Override
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    50
    public Map<String, JPanel> getTabs() {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    51
        try {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    52
            return plugin.getTabs();
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    53
        } catch (RuntimeException e) {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    54
            handleException(e);
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    55
        }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    56
        return new HashMap<>();
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    57
    }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    58
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    59
    @Override
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    60
    public SwingWorker<?, ?> newSwingWorker() {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    61
        try {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    62
            return plugin.newSwingWorker();
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    63
        } catch (RuntimeException e) {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    64
            handleException(e);
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    65
        }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    66
        return null;
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    67
    }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    68
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    69
    @Override
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    70
    public void dispose() {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    71
        try {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    72
            plugin.dispose();
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    73
        } catch (RuntimeException e) {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    74
            handleException(e);
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    75
        }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    76
    }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    77
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    78
    public void executeSwingWorker(SwingWorker<?, ?> sw) {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    79
        try {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    80
            sw.execute();
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    81
        } catch (RuntimeException e) {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    82
            handleException(e);
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    83
        }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    84
    }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    85
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    86
    private void handleException(Exception e) {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    87
        if (JConsole.isDebug()) {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    88
            System.err.println("Plug-in exception:");
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    89
            e.printStackTrace();
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    90
        } else {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    91
            if (!ignoreExceptions) {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    92
                showExceptionDialog(e);
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    93
            }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    94
        }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    95
    }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    96
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    97
    private void showExceptionDialog(Exception e) {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    98
        Object[] buttonTexts = {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
    99
            Messages.PLUGIN_EXCEPTION_DIALOG_BUTTON_OK,
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   100
            Messages.PLUGIN_EXCEPTION_DIALOG_BUTTON_EXIT,
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   101
            Messages.PLUGIN_EXCEPTION_DIALOG_BUTTON_IGNORE
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   102
        };
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   103
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   104
        String message = String.format(
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   105
            Messages.PLUGIN_EXCEPTION_DIALOG_MESSAGE,
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   106
            plugin.getClass().getSimpleName(),
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   107
            String.valueOf(e.getMessage())
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   108
        );
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   109
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   110
        int buttonIndex = JOptionPane.showOptionDialog(
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   111
            null,
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   112
            message,
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   113
            Messages.PLUGIN_EXCEPTION_DIALOG_TITLE,
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   114
            JOptionPane.YES_NO_CANCEL_OPTION,
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   115
            JOptionPane.ERROR_MESSAGE,
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   116
            null,
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   117
            buttonTexts,
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   118
            buttonTexts[0]
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   119
        );
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   120
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   121
        if (buttonIndex == 1) {
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   122
            System.exit(0);
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   123
        }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   124
        ignoreExceptions = buttonIndex == 2;
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   125
    }
ce3b6c1ab8a6 6696970: Jconsole becomes unusable if a plugin throws an exception
egahlin
parents:
diff changeset
   126
}