1112 lines · 39.5 KB · Java
RawDownload
/* * Copyright (c) 2004, 2006, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package sun.tools.jconsole; import java.awt.*;import java.awt.event.*;import java.beans.*;import java.io.*;import java.lang.reflect.InvocationTargetException;import java.net.*;import java.util.*;import java.util.List; import javax.swing.*;import javax.swing.border.*;import javax.swing.event.*;import javax.swing.plaf.*;import javax.management.remote.JMXServiceURL;import javax.management.remote.JMXConnector;import javax.security.auth.login.FailedLoginException;import javax.net.ssl.SSLHandshakeException; import com.sun.tools.jconsole.JConsolePlugin; import sun.net.util.IPAddressUtil; import static sun.tools.jconsole.Resources.*;import static sun.tools.jconsole.Utilities.*; @SuppressWarnings("serial")public class JConsole extends JFrame    implements ActionListener, InternalFrameListener {     static /*final*/ boolean IS_GTK;    static /*final*/ boolean IS_WIN;     static {        // Apply the system L&F if it is GTK or Windows, and        // the L&F is not specified using a system property.        if (System.getProperty("swing.defaultlaf") == null) {            String systemLaF = UIManager.getSystemLookAndFeelClassName();            if (systemLaF.equals("com.sun.java.swing.plaf.gtk.GTKLookAndFeel") ||                systemLaF.equals("com.sun.java.swing.plaf.windows.WindowsLookAndFeel")) {                 try {                    UIManager.setLookAndFeel(systemLaF);                } catch (Exception e) {                    System.err.println(Resources.getText("JConsole: ", e.getMessage()));                }            }        }         updateLafValues();    }      static void updateLafValues() {        String lafName = UIManager.getLookAndFeel().getClass().getName();        IS_GTK = lafName.equals("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");        IS_WIN = lafName.equals("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");         //BorderedComponent.updateLafValues();    }      private final static String title =        Resources.getText("Java Monitoring & Management Console");    public final static String ROOT_URL =        "service:jmx:";     private static int updateInterval = 4000;    private static String pluginPath = "";     private JMenuBar menuBar;    private JMenuItem hotspotMI, connectMI, exitMI;    private WindowMenu windowMenu;    private JMenuItem tileMI, cascadeMI, minimizeAllMI, restoreAllMI;    private JMenuItem userGuideMI, aboutMI;     private JButton connectButton;    private JDesktopPane desktop;    private ConnectDialog connectDialog;    private CreateMBeanDialog createDialog;     private ArrayList<VMInternalFrame> windows =        new ArrayList<VMInternalFrame>();     private int frameLoc = 5;    static boolean debug;     public JConsole(boolean hotspot) {        super(title);         setRootPane(new FixedJRootPane());        setAccessibleDescription(this,                                 getText("JConsole.accessibleDescription"));        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         menuBar = new JMenuBar();        setJMenuBar(menuBar);         // TODO: Use Actions !         JMenu connectionMenu = new JMenu(getText("Connection"));        connectionMenu.setMnemonic(getMnemonicInt("Connection"));        menuBar.add(connectionMenu);        if(hotspot) {            hotspotMI = new JMenuItem(getText("Hotspot MBeans..."));            hotspotMI.setMnemonic(getMnemonicInt("Hotspot MBeans..."));            hotspotMI.setAccelerator(KeyStroke.                                     getKeyStroke(KeyEvent.VK_H,                                                  InputEvent.CTRL_MASK));            hotspotMI.addActionListener(this);            connectionMenu.add(hotspotMI);             connectionMenu.addSeparator();        }         connectMI = new JMenuItem(Resources.getText("New Connection..."));        connectMI.setMnemonic(getMnemonicInt("New Connection..."));        connectMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,                                                        InputEvent.CTRL_MASK));        connectMI.addActionListener(this);        connectionMenu.add(connectMI);         connectionMenu.addSeparator();         exitMI = new JMenuItem(Resources.getText("Exit"));        exitMI.setMnemonic(getMnemonicInt("Exit"));        exitMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,                                                     InputEvent.ALT_MASK));        exitMI.addActionListener(this);        connectionMenu.add(exitMI);          JMenu helpMenu = new JMenu(getText("HelpMenu.title"));        helpMenu.setMnemonic(getMnemonicInt("HelpMenu.title"));        menuBar.add(helpMenu);         if (AboutDialog.isBrowseSupported()) {            userGuideMI = new JMenuItem(getText("HelpMenu.UserGuide.title"));            userGuideMI.setMnemonic(getMnemonicInt("HelpMenu.UserGuide.title"));            userGuideMI.addActionListener(this);            helpMenu.add(userGuideMI);            helpMenu.addSeparator();        }        aboutMI = new JMenuItem(getText("HelpMenu.About.title"));        aboutMI.setMnemonic(getMnemonicInt("HelpMenu.About.title"));        aboutMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));        aboutMI.addActionListener(this);        helpMenu.add(aboutMI);    }     public JDesktopPane getDesktopPane() {        return desktop;    }     public List<VMInternalFrame> getInternalFrames() {        return windows;    }     private void createMDI() {        // Restore title - we now show connection name on internal frames        setTitle(title);         Container cp = getContentPane();        Component oldCenter =            ((BorderLayout)cp.getLayout()).            getLayoutComponent(BorderLayout.CENTER);         windowMenu = new WindowMenu(Resources.getText("Window"));        windowMenu.setMnemonic(getMnemonicInt("Window"));        // Add Window menu before Help menu        menuBar.add(windowMenu, menuBar.getComponentCount() - 1);         desktop = new JDesktopPane();        desktop.setBackground(new Color(235, 245, 255));         cp.add(desktop, BorderLayout.CENTER);         if (oldCenter instanceof VMPanel) {            addFrame((VMPanel)oldCenter);        }    }     private class WindowMenu extends JMenu {        VMInternalFrame[] windowMenuWindows = new VMInternalFrame[0];        int separatorPosition;         // The width value of viewR is used to truncate long menu items.        // The rest are placeholders and are ignored for this purpose.        Rectangle viewR = new Rectangle(0, 0, 400, 20);        Rectangle textR = new Rectangle(0, 0, 0, 0);        Rectangle iconR = new Rectangle(0, 0, 0, 0);         WindowMenu(String text) {            super(text);             cascadeMI = new JMenuItem(Resources.getText("Cascade"));            cascadeMI.setMnemonic(getMnemonicInt("Cascade"));            cascadeMI.addActionListener(JConsole.this);            add(cascadeMI);             tileMI = new JMenuItem(Resources.getText("Tile"));            tileMI.setMnemonic(getMnemonicInt("Tile"));            tileMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,                                                         InputEvent.CTRL_MASK));            tileMI.addActionListener(JConsole.this);            add(tileMI);             minimizeAllMI = new JMenuItem(Resources.getText("Minimize All"));            minimizeAllMI.setMnemonic(getMnemonicInt("Minimize All"));            minimizeAllMI.addActionListener(JConsole.this);            add(minimizeAllMI);             restoreAllMI = new JMenuItem(Resources.getText("Restore All"));            restoreAllMI.setMnemonic(getMnemonicInt("Restore All"));            restoreAllMI.addActionListener(JConsole.this);            add(restoreAllMI);             separatorPosition = getMenuComponentCount();        }         private void add(VMInternalFrame vmIF) {            if (separatorPosition == getMenuComponentCount()) {                addSeparator();            }             int index = -1;            int position = separatorPosition + 1;            int n = windowMenuWindows.length;             for (int i = 0; i < n; i++) {                if (windowMenuWindows[i] != null) {                    // Slot is in use, try next                    position++;                } else {                    // Found a free slot                    index = i;                    break;                }            }             if (index == -1) {                // Create a slot at the end                VMInternalFrame[] newArray = new VMInternalFrame[n + 1];                System.arraycopy(windowMenuWindows, 0, newArray, 0, n);                windowMenuWindows = newArray;                index = n;            }             windowMenuWindows[index] = vmIF;             String indexString = "" + (index+1);            String vmName = vmIF.getVMPanel().getDisplayName();            // Maybe truncate menu item string and end with "..."            String text =                SwingUtilities.layoutCompoundLabel(this,                                        getGraphics().getFontMetrics(getFont()),                                        indexString +  " " + vmName,                                        null, 0, 0, 0, 0,                                        viewR, iconR, textR, 0);            JMenuItem mi = new JMenuItem(text);            if (text.endsWith("...")) {                mi.setToolTipText(vmName);            }             // Set mnemonic using last digit of number            int nDigits = indexString.length();            mi.setMnemonic(indexString.charAt(nDigits-1));            mi.setDisplayedMnemonicIndex(nDigits-1);             mi.putClientProperty("JConsole.vmIF", vmIF);            mi.addActionListener(JConsole.this);            vmIF.putClientProperty("JConsole.menuItem", mi);            add(mi, position);        }         private void remove(VMInternalFrame vmIF) {            for (int i = 0; i < windowMenuWindows.length; i++) {                if (windowMenuWindows[i] == vmIF) {                    windowMenuWindows[i] = null;                }            }            JMenuItem mi = (JMenuItem)vmIF.getClientProperty("JConsole.menuItem");            remove(mi);            mi.putClientProperty("JConsole.vmIF", null);            vmIF.putClientProperty("JConsole.menuItem", null);             if (separatorPosition == getMenuComponentCount() - 1) {                remove(getMenuComponent(getMenuComponentCount() - 1));            }        }    }     public void actionPerformed(ActionEvent ev) {        Object src = ev.getSource();        if (src == hotspotMI) {            showCreateMBeanDialog();        }         if (src == connectButton || src == connectMI) {            VMPanel vmPanel = null;            JInternalFrame vmIF = desktop.getSelectedFrame();            if (vmIF instanceof VMInternalFrame) {                vmPanel = ((VMInternalFrame)vmIF).getVMPanel();            }                String hostName = "";                String url = "";                if (vmPanel != null) {                    hostName = vmPanel.getHostName();                    if(vmPanel.getUrl() != null)                        url = vmPanel.getUrl();                }                showConnectDialog(url, hostName, 0, null, null, null);        } else if (src == tileMI) {            tileWindows();        } else if (src == cascadeMI) {            cascadeWindows();        } else if (src == minimizeAllMI) {            for (VMInternalFrame vmIF : windows) {                try {                    vmIF.setIcon(true);                } catch (PropertyVetoException ex) {                    // Ignore                }            }        } else if (src == restoreAllMI) {            for (VMInternalFrame vmIF : windows) {                try {                    vmIF.setIcon(false);                } catch (PropertyVetoException ex) {                    // Ignore                }            }        } else if (src == exitMI) {            System.exit(0);        } else if (src == userGuideMI) {            AboutDialog.browseUserGuide(this);        } else if (src == aboutMI) {            AboutDialog.showAboutDialog(this);        } else if (src instanceof JMenuItem) {            JMenuItem mi = (JMenuItem)src;            VMInternalFrame vmIF = (VMInternalFrame)mi.                getClientProperty("JConsole.vmIF");            if (vmIF != null) {                try {                    vmIF.setIcon(false);                    vmIF.setSelected(true);                } catch (PropertyVetoException ex) {                    // Ignore                }                vmIF.moveToFront();            }        }    }      public void tileWindows() {        int w = -1;        int h = -1;        int n = 0;        for (VMInternalFrame vmIF : windows) {            if (!vmIF.isIcon()) {                n++;                if (w == -1) {                    try {                        vmIF.setMaximum(true);                        w = vmIF.getWidth();                        h = vmIF.getHeight();                    } catch (PropertyVetoException ex) {                        // Ignore                    }                }            }        }        if (n > 0 && w > 0 && h > 0) {            int rows = (int)Math.ceil(Math.sqrt(n));            int cols = n / rows;            if (rows * cols < n) cols++;            int x = 0;            int y = 0;            w /= cols;            h /= rows;            int col = 0;            for (VMInternalFrame vmIF : windows) {                if (!vmIF.isIcon()) {                    try {                        vmIF.setMaximum(n==1);                    } catch (PropertyVetoException ex) {                        // Ignore                    }                    if (n > 1) {                        vmIF.setBounds(x, y, w, h);                    }                    if (col < cols-1) {                        col++;                        x += w;                    } else {                        col = 0;                        x = 0;                        y += h;                    }                }            }        }    }     public void cascadeWindows() {        int n = 0;        int w = -1;        int h = -1;        for (VMInternalFrame vmIF : windows) {            if (!vmIF.isIcon()) {                try {                    vmIF.setMaximum(false);                } catch (PropertyVetoException ex) {                    // Ignore                }                n++;                vmIF.pack();                if (w == -1) {                    try {                        w = vmIF.getWidth();                        h = vmIF.getHeight();                        vmIF.setMaximum(true);                        w = vmIF.getWidth() - w;                        h = vmIF.getHeight() - h;                        vmIF.pack();                    } catch (PropertyVetoException ex) {                        // Ignore                    }                }            }        }        int x = 0;        int y = 0;        int dX = (n > 1) ? (w / (n - 1)) : 0;        int dY = (n > 1) ? (h / (n - 1)) : 0;        for (VMInternalFrame vmIF : windows) {            if (!vmIF.isIcon()) {                vmIF.setLocation(x, y);                vmIF.moveToFront();                x += dX;                y += dY;            }        }    }     // Call on EDT    void addHost(String hostName, int port,                 String userName, String password) {        addHost(hostName, port, userName, password, false);    }     // Call on EDT    void addVmid(LocalVirtualMachine lvm) {        addVmid(lvm, false);    }     // Call on EDT    void addVmid(final LocalVirtualMachine lvm, final boolean tile) {        new Thread("JConsole.addVmid") {            public void run() {                try {                    addProxyClient(ProxyClient.getProxyClient(lvm), tile);                } catch (final SecurityException ex) {                    failed(ex, null, null, null);                } catch (final IOException ex) {                    failed(ex, null, null, null);                }            }        }.start();    }     // Call on EDT    void addUrl(final String url,

Lines 1–500 of 1112.