jdk/src/share/classes/sun/tools/jconsole/JConsole.java
author michaelm
Thu, 24 Oct 2013 20:39:21 +0100
changeset 22339 e91bfaf4360d
parent 19581 f860aff6ed05
child 23010 6dadb192ad81
permissions -rw-r--r--
8011786: Better applet networking Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12851
diff changeset
     2
 * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jconsole;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.security.auth.login.FailedLoginException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.net.ssl.SSLHandshakeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.jconsole.JConsolePlugin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.net.util.IPAddressUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import static sun.tools.jconsole.Utilities.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
@SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class JConsole extends JFrame
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    implements ActionListener, InternalFrameListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static /*final*/ boolean IS_GTK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static /*final*/ boolean IS_WIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // Apply the system L&F if it is GTK or Windows, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // the L&F is not specified using a system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (System.getProperty("swing.defaultlaf") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            String systemLaF = UIManager.getSystemLookAndFeelClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            if (systemLaF.equals("com.sun.java.swing.plaf.gtk.GTKLookAndFeel") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                systemLaF.equals("com.sun.java.swing.plaf.windows.WindowsLookAndFeel")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    UIManager.setLookAndFeel(systemLaF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                } catch (Exception e) {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    67
                    System.err.println(Resources.format(Messages.JCONSOLE_COLON_, e.getMessage()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        updateLafValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static void updateLafValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        String lafName = UIManager.getLookAndFeel().getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        IS_GTK = lafName.equals("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        IS_WIN = lafName.equals("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        //BorderedComponent.updateLafValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private final static String title =
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    86
        Messages.JAVA_MONITORING___MANAGEMENT_CONSOLE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public final static String ROOT_URL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        "service:jmx:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static int updateInterval = 4000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static String pluginPath = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private JMenuBar menuBar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private JMenuItem hotspotMI, connectMI, exitMI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private WindowMenu windowMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private JMenuItem tileMI, cascadeMI, minimizeAllMI, restoreAllMI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private JMenuItem userGuideMI, aboutMI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private JButton connectButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private JDesktopPane desktop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private ConnectDialog connectDialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private CreateMBeanDialog createDialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private ArrayList<VMInternalFrame> windows =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        new ArrayList<VMInternalFrame>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private int frameLoc = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static boolean debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public JConsole(boolean hotspot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        super(title);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        setRootPane(new FixedJRootPane());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        setAccessibleDescription(this,
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   115
                                 Messages.JCONSOLE_ACCESSIBLE_DESCRIPTION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        menuBar = new JMenuBar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        setJMenuBar(menuBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // TODO: Use Actions !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   123
        JMenu connectionMenu = new JMenu(Messages.CONNECTION);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   124
        connectionMenu.setMnemonic(Resources.getMnemonicInt(Messages.CONNECTION));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        menuBar.add(connectionMenu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if(hotspot) {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   127
            hotspotMI = new JMenuItem(Messages.HOTSPOT_MBEANS_ELLIPSIS);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   128
            hotspotMI.setMnemonic(Resources.getMnemonicInt(Messages.HOTSPOT_MBEANS_ELLIPSIS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            hotspotMI.setAccelerator(KeyStroke.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                     getKeyStroke(KeyEvent.VK_H,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                                  InputEvent.CTRL_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            hotspotMI.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            connectionMenu.add(hotspotMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            connectionMenu.addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   138
        connectMI = new JMenuItem(Messages.NEW_CONNECTION_ELLIPSIS);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   139
        connectMI.setMnemonic(Resources.getMnemonicInt(Messages.NEW_CONNECTION_ELLIPSIS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        connectMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                                        InputEvent.CTRL_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        connectMI.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        connectionMenu.add(connectMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        connectionMenu.addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   147
        exitMI = new JMenuItem(Messages.EXIT);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   148
        exitMI.setMnemonic(Resources.getMnemonicInt(Messages.EXIT));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        exitMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                                     InputEvent.ALT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        exitMI.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        connectionMenu.add(exitMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   155
        JMenu helpMenu = new JMenu(Messages.HELP_MENU_TITLE);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   156
        helpMenu.setMnemonic(Resources.getMnemonicInt(Messages.HELP_MENU_TITLE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        menuBar.add(helpMenu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (AboutDialog.isBrowseSupported()) {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   160
            userGuideMI = new JMenuItem(Messages.HELP_MENU_USER_GUIDE_TITLE);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   161
            userGuideMI.setMnemonic(Resources.getMnemonicInt(Messages.HELP_MENU_USER_GUIDE_TITLE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            userGuideMI.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            helpMenu.add(userGuideMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            helpMenu.addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   166
        aboutMI = new JMenuItem(Messages.HELP_MENU_ABOUT_TITLE);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   167
        aboutMI.setMnemonic(Resources.getMnemonicInt(Messages.HELP_MENU_ABOUT_TITLE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        aboutMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        aboutMI.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        helpMenu.add(aboutMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public JDesktopPane getDesktopPane() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return desktop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public List<VMInternalFrame> getInternalFrames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return windows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private void createMDI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        // Restore title - we now show connection name on internal frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        setTitle(title);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        Container cp = getContentPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        Component oldCenter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            ((BorderLayout)cp.getLayout()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            getLayoutComponent(BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   190
        windowMenu = new WindowMenu(Messages.WINDOW);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   191
        windowMenu.setMnemonic(Resources.getMnemonicInt(Messages.WINDOW));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // Add Window menu before Help menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        menuBar.add(windowMenu, menuBar.getComponentCount() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        desktop = new JDesktopPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        desktop.setBackground(new Color(235, 245, 255));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        cp.add(desktop, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (oldCenter instanceof VMPanel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            addFrame((VMPanel)oldCenter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private class WindowMenu extends JMenu {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        VMInternalFrame[] windowMenuWindows = new VMInternalFrame[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        int separatorPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // The width value of viewR is used to truncate long menu items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // The rest are placeholders and are ignored for this purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        Rectangle viewR = new Rectangle(0, 0, 400, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        Rectangle textR = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        Rectangle iconR = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        WindowMenu(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            super(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   218
            cascadeMI = new JMenuItem(Messages.CASCADE);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   219
            cascadeMI.setMnemonic(Resources.getMnemonicInt(Messages.CASCADE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            cascadeMI.addActionListener(JConsole.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            add(cascadeMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   223
            tileMI = new JMenuItem(Messages.TILE);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   224
            tileMI.setMnemonic(Resources.getMnemonicInt(Messages.TILE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            tileMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                                         InputEvent.CTRL_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            tileMI.addActionListener(JConsole.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            add(tileMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   230
            minimizeAllMI = new JMenuItem(Messages.MINIMIZE_ALL);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   231
            minimizeAllMI.setMnemonic(Resources.getMnemonicInt(Messages.MINIMIZE_ALL));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            minimizeAllMI.addActionListener(JConsole.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            add(minimizeAllMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   235
            restoreAllMI = new JMenuItem(Messages.RESTORE_ALL);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   236
            restoreAllMI.setMnemonic(Resources.getMnemonicInt(Messages.RESTORE_ALL));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            restoreAllMI.addActionListener(JConsole.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            add(restoreAllMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            separatorPosition = getMenuComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        private void add(VMInternalFrame vmIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (separatorPosition == getMenuComponentCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            int index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            int position = separatorPosition + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            int n = windowMenuWindows.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                if (windowMenuWindows[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    // Slot is in use, try next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    position++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    // Found a free slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                // Create a slot at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                VMInternalFrame[] newArray = new VMInternalFrame[n + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                System.arraycopy(windowMenuWindows, 0, newArray, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                windowMenuWindows = newArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                index = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            windowMenuWindows[index] = vmIF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            String indexString = "" + (index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            String vmName = vmIF.getVMPanel().getDisplayName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            // Maybe truncate menu item string and end with "..."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            String text =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                SwingUtilities.layoutCompoundLabel(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                        getGraphics().getFontMetrics(getFont()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                        indexString +  " " + vmName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                        null, 0, 0, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                        viewR, iconR, textR, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            JMenuItem mi = new JMenuItem(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (text.endsWith("...")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                mi.setToolTipText(vmName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // Set mnemonic using last digit of number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            int nDigits = indexString.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            mi.setMnemonic(indexString.charAt(nDigits-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            mi.setDisplayedMnemonicIndex(nDigits-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            mi.putClientProperty("JConsole.vmIF", vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            mi.addActionListener(JConsole.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            vmIF.putClientProperty("JConsole.menuItem", mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            add(mi, position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        private void remove(VMInternalFrame vmIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            for (int i = 0; i < windowMenuWindows.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                if (windowMenuWindows[i] == vmIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    windowMenuWindows[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            JMenuItem mi = (JMenuItem)vmIF.getClientProperty("JConsole.menuItem");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            remove(mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            mi.putClientProperty("JConsole.vmIF", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            vmIF.putClientProperty("JConsole.menuItem", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (separatorPosition == getMenuComponentCount() - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                remove(getMenuComponent(getMenuComponentCount() - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public void actionPerformed(ActionEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        Object src = ev.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (src == hotspotMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            showCreateMBeanDialog();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (src == connectButton || src == connectMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            VMPanel vmPanel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            JInternalFrame vmIF = desktop.getSelectedFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (vmIF instanceof VMInternalFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                vmPanel = ((VMInternalFrame)vmIF).getVMPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                String hostName = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                String url = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                if (vmPanel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    hostName = vmPanel.getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    if(vmPanel.getUrl() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        url = vmPanel.getUrl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                showConnectDialog(url, hostName, 0, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        } else if (src == tileMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            tileWindows();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        } else if (src == cascadeMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            cascadeWindows();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } else if (src == minimizeAllMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            for (VMInternalFrame vmIF : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    vmIF.setIcon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } else if (src == restoreAllMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            for (VMInternalFrame vmIF : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    vmIF.setIcon(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } else if (src == exitMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        } else if (src == userGuideMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            AboutDialog.browseUserGuide(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } else if (src == aboutMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            AboutDialog.showAboutDialog(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        } else if (src instanceof JMenuItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            JMenuItem mi = (JMenuItem)src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            VMInternalFrame vmIF = (VMInternalFrame)mi.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                getClientProperty("JConsole.vmIF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            if (vmIF != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    vmIF.setIcon(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    vmIF.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                vmIF.moveToFront();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public void tileWindows() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        int w = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        int h = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        for (VMInternalFrame vmIF : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            if (!vmIF.isIcon()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                if (w == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        vmIF.setMaximum(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        w = vmIF.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        h = vmIF.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (n > 0 && w > 0 && h > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            int rows = (int)Math.ceil(Math.sqrt(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            int cols = n / rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (rows * cols < n) cols++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            int y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            w /= cols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            h /= rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            int col = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            for (VMInternalFrame vmIF : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                if (!vmIF.isIcon()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                        vmIF.setMaximum(n==1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                        // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    if (n > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        vmIF.setBounds(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    if (col < cols-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        col++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        x += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        col = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        y += h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public void cascadeWindows() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        int w = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        int h = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        for (VMInternalFrame vmIF : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (!vmIF.isIcon()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    vmIF.setMaximum(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                vmIF.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                if (w == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        w = vmIF.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        h = vmIF.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        vmIF.setMaximum(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        w = vmIF.getWidth() - w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        h = vmIF.getHeight() - h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        vmIF.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        int y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        int dX = (n > 1) ? (w / (n - 1)) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        int dY = (n > 1) ? (h / (n - 1)) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        for (VMInternalFrame vmIF : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            if (!vmIF.isIcon()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                vmIF.setLocation(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                vmIF.moveToFront();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                x += dX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                y += dY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    // Call on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    void addHost(String hostName, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                 String userName, String password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        addHost(hostName, port, userName, password, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    // Call on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    void addVmid(LocalVirtualMachine lvm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        addVmid(lvm, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    // Call on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    void addVmid(final LocalVirtualMachine lvm, final boolean tile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        new Thread("JConsole.addVmid") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    addProxyClient(ProxyClient.getProxyClient(lvm), tile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                } catch (final SecurityException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    failed(ex, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                } catch (final IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    failed(ex, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    // Call on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    void addUrl(final String url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                final String userName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                final String password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                final boolean tile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        new Thread("JConsole.addUrl") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    addProxyClient(ProxyClient.getProxyClient(url, userName, password),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                                   tile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                } catch (final MalformedURLException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    failed(ex, url, userName, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                } catch (final SecurityException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    failed(ex, url, userName, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                } catch (final IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    failed(ex, url, userName, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    // Call on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    void addHost(final String hostName, final int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                 final String userName, final String password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                 final boolean tile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        new Thread("JConsole.addHost") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    addProxyClient(ProxyClient.getProxyClient(hostName, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                                              userName, password),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                   tile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                } catch (final IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    dbgStackTrace(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                            showConnectDialog(null, hostName, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                                              userName, password, errorMessage(ex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    // Call on worker thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    void addProxyClient(final ProxyClient proxyClient, final boolean tile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                VMPanel vmPanel = new VMPanel(proxyClient, updateInterval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                addFrame(vmPanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                if (tile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                            tileWindows();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                vmPanel.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    // Call on worker thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    private void failed(final Exception ex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        final String url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        final String userName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        final String password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                dbgStackTrace(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                showConnectDialog(url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                                  null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                                  -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                  userName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                                  password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                  errorMessage(ex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    private VMInternalFrame addFrame(VMPanel vmPanel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        final VMInternalFrame vmIF = new VMInternalFrame(vmPanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        for (VMInternalFrame f : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                f.setMaximum(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        desktop.add(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        vmIF.setLocation(frameLoc, frameLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        frameLoc += 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        vmIF.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        windows.add(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        if (windows.size() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                vmIF.setMaximum(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            } catch (PropertyVetoException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                // Ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        vmIF.addInternalFrameListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        windowMenu.add(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return vmIF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    private void showConnectDialog(String url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                                   String hostName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                                   int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                                   String userName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                                   String password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                   String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if (connectDialog == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            connectDialog = new ConnectDialog(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        connectDialog.setConnectionParameters(url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                              hostName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                                              port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                              userName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                              password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                              msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        connectDialog.refresh();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        connectDialog.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            // Bring to front of other dialogs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            connectDialog.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        } catch (PropertyVetoException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    private void showCreateMBeanDialog() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        if (createDialog == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            createDialog = new CreateMBeanDialog(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        createDialog.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            // Bring to front of other dialogs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            createDialog.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        } catch (PropertyVetoException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    private void removeVMInternalFrame(VMInternalFrame vmIF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        windowMenu.remove(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        desktop.remove(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        desktop.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        vmIF.getVMPanel().cleanUp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        vmIF.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    private boolean isProxyClientUsed(ProxyClient client) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        for(VMInternalFrame frame : windows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            ProxyClient cli = frame.getVMPanel().getProxyClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            if(client == cli)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    static boolean isValidRemoteString(String txt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        boolean valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if (txt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            txt = txt.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            if (txt.startsWith(ROOT_URL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                if (txt.length() > ROOT_URL.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                //---------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                // Supported host and port combinations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                //     hostname:port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                //     IPv4Address:port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                //     [IPv6Address]:port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                //---------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                // Is literal IPv6 address?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                if (txt.startsWith("[")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    int index = txt.indexOf("]:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                        // Extract literal IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        String address = txt.substring(1, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                        if (IPAddressUtil.isIPv6LiteralAddress(address)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                            // Extract port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                                String portStr = txt.substring(index + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                                int port = Integer.parseInt(portStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                                if (port >= 0 && port <= 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                                    valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                            } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                                valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    String[] s = txt.split(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    if (s.length == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                            int port = Integer.parseInt(s[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                            if (port >= 0 && port <= 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                                valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                        } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                            valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        return valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    private String errorMessage(Exception ex) {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   720
       String msg = Messages.CONNECTION_FAILED;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
       if (ex instanceof IOException || ex instanceof SecurityException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
           Throwable cause = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
           Throwable c = ex.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
           while (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
               cause = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
               c = c.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
           if (cause instanceof ConnectException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
               return msg + ": " + cause.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
           } else if (cause instanceof UnknownHostException) {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   731
               return Resources.format(Messages.UNKNOWN_HOST, cause.getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
           } else if (cause instanceof NoRouteToHostException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
               return msg + ": " + cause.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
           } else if (cause instanceof FailedLoginException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
               return msg + ": " + cause.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
           } else if (cause instanceof SSLHandshakeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
               return msg + ": "+ cause.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        } else if (ex instanceof MalformedURLException) {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   740
           return Resources.format(Messages.INVALID_URL, ex.getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        return msg + ": " + ex.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    // InternalFrameListener interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    public void internalFrameClosing(InternalFrameEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        VMInternalFrame vmIF = (VMInternalFrame)e.getInternalFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        removeVMInternalFrame(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        windows.remove(vmIF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        ProxyClient client = vmIF.getVMPanel().getProxyClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        if(!isProxyClientUsed(client))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            client.markAsDead();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        if (windows.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            showConnectDialog("", "", 0, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    public void internalFrameOpened(InternalFrameEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    public void internalFrameClosed(InternalFrameEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    public void internalFrameIconified(InternalFrameEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    public void internalFrameDeiconified(InternalFrameEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    public void internalFrameActivated(InternalFrameEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    public void internalFrameDeactivated(InternalFrameEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    private static void usage() {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   769
        System.err.println(Resources.format(Messages.ZZ_USAGE_TEXT, "jconsole"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    private static void mainInit(final List<String> urls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                                 final List<String> hostNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                                 final List<Integer> ports,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                                 final List<LocalVirtualMachine> vmids,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                                 final ProxyClient proxyClient,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                 final boolean noTile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                                 final boolean hotspot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        // Always create Swing GUI on the Event Dispatching Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                    JConsole jConsole = new JConsole(hotspot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    // Center the window on screen, taking into account screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    // size and insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    Toolkit toolkit = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    GraphicsConfiguration gc = jConsole.getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    Dimension scrSize = toolkit.getScreenSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    Insets scrInsets  = toolkit.getScreenInsets(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    Rectangle scrBounds =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                        new Rectangle(scrInsets.left, scrInsets.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                                      scrSize.width  - scrInsets.left - scrInsets.right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                                      scrSize.height - scrInsets.top  - scrInsets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    int w = Math.min(900, scrBounds.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    int h = Math.min(750, scrBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    jConsole.setBounds(scrBounds.x + (scrBounds.width  - w) / 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                                       scrBounds.y + (scrBounds.height - h) / 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                                       w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    jConsole.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    jConsole.createMDI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                    for (int i = 0; i < hostNames.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                        jConsole.addHost(hostNames.get(i), ports.get(i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                                         null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                                         (i == hostNames.size() - 1) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                                         !noTile : false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    for (int i = 0; i < urls.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                        jConsole.addUrl(urls.get(i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                                        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                                        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                                        (i == urls.size() - 1) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                                        !noTile : false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                    for (int i = 0; i < vmids.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                        jConsole.addVmid(vmids.get(i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                                        (i == vmids.size() - 1) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                                        !noTile : false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                    if (vmids.size() == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                        hostNames.size() == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                        urls.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                        jConsole.showConnectDialog(null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                                                   null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                                                   0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                                                   null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                                                   null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                                                   null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        boolean noTile = false, hotspot = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        int argIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        ProxyClient proxyClient = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        if (System.getProperty("jconsole.showOutputViewer") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            OutputViewer.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        while (args.length - argIndex > 0 && args[argIndex].startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            String arg = args[argIndex++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            if (arg.equals("-h") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                arg.equals("-help") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                arg.equals("-?")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            } else if (arg.startsWith("-interval=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    updateInterval = Integer.parseInt(arg.substring(10)) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                        1000;
19581
f860aff6ed05 6417649: -interval=0 is accepted and jconsole doesn't update window content at all
egahlin
parents: 14342
diff changeset
   861
                    if (updateInterval <= 0) {
f860aff6ed05 6417649: -interval=0 is accepted and jconsole doesn't update window content at all
egahlin
parents: 14342
diff changeset
   862
                        usage();
f860aff6ed05 6417649: -interval=0 is accepted and jconsole doesn't update window content at all
egahlin
parents: 14342
diff changeset
   863
                        return;
f860aff6ed05 6417649: -interval=0 is accepted and jconsole doesn't update window content at all
egahlin
parents: 14342
diff changeset
   864
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            } else if (arg.equals("-pluginpath")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                if (argIndex < args.length && !args[argIndex].startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                    pluginPath = args[argIndex++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                    // Invalid argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            } else if (arg.equals("-notile")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                noTile = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            } else if (arg.equals("-version")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                Version.print(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            } else if (arg.equals("-debug")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            } else if (arg.equals("-fullversion")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                Version.printFullVersion(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                // Unknown switch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        if (System.getProperty("jconsole.showUnsupported") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            hotspot = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        List<String> urls = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        List<String> hostNames = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        List<Integer> ports = new ArrayList<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        List<LocalVirtualMachine> vms = new ArrayList<LocalVirtualMachine>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        for (int i = argIndex; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            String arg = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            if (isValidRemoteString(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                if (arg.startsWith(ROOT_URL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    urls.add(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                } else if (arg.matches(".*:[0-9]*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                    int p = arg.lastIndexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                    hostNames.add(arg.substring(0, p));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                        ports.add(Integer.parseInt(arg.substring(p+1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                    } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                        usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                if (!isLocalAttachAvailable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                    System.err.println("Local process monitoring is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    int vmid = Integer.parseInt(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                    LocalVirtualMachine lvm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                        LocalVirtualMachine.getLocalVirtualMachine(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    if (lvm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                        System.err.println("Invalid process id:" + vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                    vms.add(lvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        mainInit(urls, hostNames, ports, vms, proxyClient, noTile, hotspot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    public static boolean isDebug() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        return debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    private static void dbgStackTrace(Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    private static final boolean localAttachmentSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        boolean supported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            Class.forName("com.sun.tools.attach.VirtualMachine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            Class.forName("sun.management.ConnectorAddressLink");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            supported = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        } catch (NoClassDefFoundError x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            supported = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            supported = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        localAttachmentSupported = supported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    public static boolean isLocalAttachAvailable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        return localAttachmentSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    private static ServiceLoader<JConsolePlugin> pluginService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    // Return a list of newly instantiated JConsolePlugin objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    static synchronized List<JConsolePlugin> getPlugins() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        if (pluginService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            // First time loading and initializing the plugins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            initPluginService(pluginPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            // reload the plugin so that new instances will be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            pluginService.reload();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        List<JConsolePlugin> plugins = new ArrayList<JConsolePlugin>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        for (JConsolePlugin p : pluginService) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            plugins.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        return plugins;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    private static void initPluginService(String pluginPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        if (pluginPath.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                ClassLoader pluginCL = new URLClassLoader(pathToURLs(pluginPath));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                ServiceLoader<JConsolePlugin> plugins =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    ServiceLoader.load(JConsolePlugin.class, pluginCL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                // validate all plugins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            for (JConsolePlugin p : plugins) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    if (isDebug()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                        System.out.println("Plugin " + p.getClass() + " loaded.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                pluginService = plugins;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            } catch (ServiceConfigurationError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                // Error occurs during initialization of plugin
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
  1006
                System.out.println(Resources.format(Messages.FAIL_TO_LOAD_PLUGIN,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                                   e.getMessage()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                if (JConsole.isDebug()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                }
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
  1012
                System.out.println(Resources.format(Messages.INVALID_PLUGIN_PATH,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                                   e.getMessage()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        if (pluginService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            initEmptyPlugin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    private static void initEmptyPlugin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        ClassLoader pluginCL = new URLClassLoader(new URL[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        pluginService = ServiceLoader.load(JConsolePlugin.class, pluginCL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * Utility method for converting a search path string to an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * of directory and JAR file URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @param path the search path string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @return the resulting array of directory and JAR file URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    private static URL[] pathToURLs(String path) throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        String[] names = path.split(File.pathSeparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        URL[] urls = new URL[names.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        for (String f : names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            URL url = fileToURL(new File(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            urls[count++] = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        return urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * Returns the directory or JAR file URL corresponding to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * local file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * @param file the File object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @return the resulting directory or JAR file URL, or null if unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    private static URL fileToURL(File file) throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            name = file.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            name = file.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        name = name.replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        if (!name.startsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            name = "/" + name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        // If the file does not exist, then assume that it's a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        if (!file.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            name = name + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        return new URL("file", "", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    private static class FixedJRootPane extends JRootPane {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            updateLafValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            super.updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
         * The revalidate method seems to be the only one that gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
         * called whenever there is a change of L&F or change of theme
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
         * in Windows L&F and GTK L&F.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        public void revalidate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            // Workaround for Swing bug where the titledborder in both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            // GTK and Windows L&F's use calculated colors instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            // the highlight/shadow colors from the theme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            // Putting null removes any previous override and causes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            // fallback to the current L&F's value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            UIManager.put("TitledBorder.border", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            Border border = UIManager.getBorder("TitledBorder.border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            if (border instanceof BorderUIResource.EtchedBorderUIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                Color highlight = UIManager.getColor("ToolBar.highlight");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                Color shadow    = UIManager.getColor("ToolBar.shadow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                border = new BorderUIResource.EtchedBorderUIResource(highlight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                                                                     shadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                UIManager.put("TitledBorder.border", border);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            if (IS_GTK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                // Workaround for Swing bug where the titledborder in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                // GTK L&F use hardcoded color and font for the title
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                // instead of getting them from the theme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                UIManager.put("TitledBorder.titleColor",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                              UIManager.getColor("Label.foreground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                UIManager.put("TitledBorder.font",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                              UIManager.getFont("Label.font"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            super.revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
}