final String userName, final String password, final boolean tile) { new Thread("JConsole.addUrl") { public void run() { try { addProxyClient(ProxyClient.getProxyClient(url, userName, password), tile); } catch (final MalformedURLException ex) { failed(ex, url, userName, password); } catch (final SecurityException ex) { failed(ex, url, userName, password); } catch (final IOException ex) { failed(ex, url, userName, password); } } }.start(); } // Call on EDT void addHost(final String hostName, final int port, final String userName, final String password, final boolean tile) { new Thread("JConsole.addHost") { public void run() { try { addProxyClient(ProxyClient.getProxyClient(hostName, port, userName, password), tile); } catch (final IOException ex) { dbgStackTrace(ex); SwingUtilities.invokeLater(new Runnable() { public void run() { showConnectDialog(null, hostName, port, userName, password, errorMessage(ex)); } }); } } }.start(); } // Call on worker thread void addProxyClient(final ProxyClient proxyClient, final boolean tile) { SwingUtilities.invokeLater(new Runnable() { public void run() { VMPanel vmPanel = new VMPanel(proxyClient, updateInterval); addFrame(vmPanel); if (tile) { SwingUtilities.invokeLater(new Runnable() { public void run() { tileWindows(); } }); } vmPanel.connect(); } }); } // Call on worker thread private void failed(final Exception ex, final String url, final String userName, final String password) { SwingUtilities.invokeLater(new Runnable() { public void run() { dbgStackTrace(ex); showConnectDialog(url, null, -1, userName, password, errorMessage(ex)); } }); } private VMInternalFrame addFrame(VMPanel vmPanel) { final VMInternalFrame vmIF = new VMInternalFrame(vmPanel); for (VMInternalFrame f : windows) { try { f.setMaximum(false); } catch (PropertyVetoException ex) { // Ignore } } desktop.add(vmIF); vmIF.setLocation(frameLoc, frameLoc); frameLoc += 30; vmIF.setVisible(true); windows.add(vmIF); if (windows.size() == 1) { try { vmIF.setMaximum(true); } catch (PropertyVetoException ex) { // Ignore } } vmIF.addInternalFrameListener(this); windowMenu.add(vmIF); return vmIF; } private void showConnectDialog(String url, String hostName, int port, String userName, String password, String msg) { if (connectDialog == null) { connectDialog = new ConnectDialog(this); } connectDialog.setConnectionParameters(url, hostName, port, userName, password, msg); connectDialog.refresh(); connectDialog.setVisible(true); try { // Bring to front of other dialogs connectDialog.setSelected(true); } catch (PropertyVetoException e) { } } private void showCreateMBeanDialog() { if (createDialog == null) { createDialog = new CreateMBeanDialog(this); } createDialog.setVisible(true); try { // Bring to front of other dialogs createDialog.setSelected(true); } catch (PropertyVetoException e) { } } private void removeVMInternalFrame(VMInternalFrame vmIF) { windowMenu.remove(vmIF); desktop.remove(vmIF); desktop.repaint(); vmIF.getVMPanel().cleanUp(); vmIF.dispose(); } private boolean isProxyClientUsed(ProxyClient client) { for(VMInternalFrame frame : windows) { ProxyClient cli = frame.getVMPanel().getProxyClient(false); if(client == cli) return true; } return false; } static boolean isValidRemoteString(String txt) { boolean valid = false; if (txt != null) { txt = txt.trim(); if (txt.startsWith(ROOT_URL)) { if (txt.length() > ROOT_URL.length()) { valid = true; } } else { //--------------------------------------- // Supported host and port combinations: // hostname:port // IPv4Address:port // [IPv6Address]:port //--------------------------------------- // Is literal IPv6 address? // if (txt.startsWith("[")) { int index = txt.indexOf("]:"); if (index != -1) { // Extract literal IPv6 address // String address = txt.substring(1, index); if (IPAddressUtil.isIPv6LiteralAddress(address)) { // Extract port // try { String portStr = txt.substring(index + 2); int port = Integer.parseInt(portStr); if (port >= 0 && port <= 0xFFFF) { valid = true; } } catch (NumberFormatException ex) { valid = false; } } } } else { String[] s = txt.split(":"); if (s.length == 2) { try { int port = Integer.parseInt(s[1]); if (port >= 0 && port <= 0xFFFF) { valid = true; } } catch (NumberFormatException ex) { valid = false; } } } } } return valid; } private String errorMessage(Exception ex) { String msg = Resources.getText("Connection failed"); if (ex instanceof IOException || ex instanceof SecurityException) { Throwable cause = null; Throwable c = ex.getCause(); while (c != null) { cause = c; c = c.getCause(); } if (cause instanceof ConnectException) { return msg + ": " + cause.getMessage(); } else if (cause instanceof UnknownHostException) { return Resources.getText("Unknown Host", cause.getMessage()); } else if (cause instanceof NoRouteToHostException) { return msg + ": " + cause.getMessage(); } else if (cause instanceof FailedLoginException) { return msg + ": " + cause.getMessage(); } else if (cause instanceof SSLHandshakeException) { return msg + ": "+ cause.getMessage(); } } else if (ex instanceof MalformedURLException) { return Resources.getText("Invalid URL", ex.getMessage()); } return msg + ": " + ex.getMessage(); } // InternalFrameListener interface public void internalFrameClosing(InternalFrameEvent e) { VMInternalFrame vmIF = (VMInternalFrame)e.getInternalFrame(); removeVMInternalFrame(vmIF); windows.remove(vmIF); ProxyClient client = vmIF.getVMPanel().getProxyClient(false); if(!isProxyClientUsed(client)) client.markAsDead(); if (windows.size() == 0) { showConnectDialog("", "", 0, null, null, null); } } public void internalFrameOpened(InternalFrameEvent e) {} public void internalFrameClosed(InternalFrameEvent e) {} public void internalFrameIconified(InternalFrameEvent e) {} public void internalFrameDeiconified(InternalFrameEvent e) {} public void internalFrameActivated(InternalFrameEvent e) {} public void internalFrameDeactivated(InternalFrameEvent e) {} private static void usage() { System.err.println(Resources.getText("zz usage text", "jconsole")); } private static void mainInit(final List<String> urls, final List<String> hostNames, final List<Integer> ports, final List<LocalVirtualMachine> vmids, final ProxyClient proxyClient, final boolean noTile, final boolean hotspot) { // Always create Swing GUI on the Event Dispatching Thread SwingUtilities.invokeLater(new Runnable() { public void run() { JConsole jConsole = new JConsole(hotspot); // Center the window on screen, taking into account screen // size and insets. Toolkit toolkit = Toolkit.getDefaultToolkit(); GraphicsConfiguration gc = jConsole.getGraphicsConfiguration(); Dimension scrSize = toolkit.getScreenSize(); Insets scrInsets = toolkit.getScreenInsets(gc); Rectangle scrBounds = new Rectangle(scrInsets.left, scrInsets.top, scrSize.width - scrInsets.left - scrInsets.right, scrSize.height - scrInsets.top - scrInsets.bottom); int w = Math.min(900, scrBounds.width); int h = Math.min(750, scrBounds.height); jConsole.setBounds(scrBounds.x + (scrBounds.width - w) / 2, scrBounds.y + (scrBounds.height - h) / 2, w, h); jConsole.setVisible(true); jConsole.createMDI(); for (int i = 0; i < hostNames.size(); i++) { jConsole.addHost(hostNames.get(i), ports.get(i), null, null, (i == hostNames.size() - 1) ? !noTile : false); } for (int i = 0; i < urls.size(); i++) { jConsole.addUrl(urls.get(i), null, null, (i == urls.size() - 1) ? !noTile : false); } for (int i = 0; i < vmids.size(); i++) { jConsole.addVmid(vmids.get(i), (i == vmids.size() - 1) ? !noTile : false); } if (vmids.size() == 0 && hostNames.size() == 0 && urls.size() == 0) { jConsole.showConnectDialog(null, null, 0, null, null, null); } } }); } public static void main(String[] args) { boolean noTile = false, hotspot = false; int argIndex = 0; ProxyClient proxyClient = null; if (System.getProperty("jconsole.showOutputViewer") != null) { OutputViewer.init(); } while (args.length - argIndex > 0 && args[argIndex].startsWith("-")) { String arg = args[argIndex++]; if (arg.equals("-h") || arg.equals("-help") || arg.equals("-?")) { usage(); return; } else if (arg.startsWith("-interval=")) { try { updateInterval = Integer.parseInt(arg.substring(10)) * 1000; } catch (NumberFormatException ex) { usage(); return; } } else if (arg.equals("-pluginpath")) { if (argIndex < args.length && !args[argIndex].startsWith("-")) { pluginPath = args[argIndex++]; } else { // Invalid argument usage(); return; } } else if (arg.equals("-notile")) { noTile = true; } else if (arg.equals("-version")) { Version.print(System.err); return; } else if (arg.equals("-debug")) { debug = true; } else if (arg.equals("-fullversion")) { Version.printFullVersion(System.err); return; } else { // Unknown switch usage(); return; } } if (System.getProperty("jconsole.showUnsupported") != null) { hotspot = true; } List<String> urls = new ArrayList<String>(); List<String> hostNames = new ArrayList<String>(); List<Integer> ports = new ArrayList<Integer>(); List<LocalVirtualMachine> vms = new ArrayList<LocalVirtualMachine>(); for (int i = argIndex; i < args.length; i++) { String arg = args[i]; if (isValidRemoteString(arg)) { if (arg.startsWith(ROOT_URL)) { urls.add(arg); } else if (arg.matches(".*:[0-9]*")) { int p = arg.lastIndexOf(':'); hostNames.add(arg.substring(0, p)); try { ports.add(Integer.parseInt(arg.substring(p+1))); } catch (NumberFormatException ex) { usage(); return; } } } else { if (!isLocalAttachAvailable()) { System.err.println("Local process monitoring is not supported"); return; } try { int vmid = Integer.parseInt(arg); LocalVirtualMachine lvm = LocalVirtualMachine.getLocalVirtualMachine(vmid); if (lvm == null) { System.err.println("Invalid process id:" + vmid); return; } vms.add(lvm); } catch (NumberFormatException ex) { usage(); return; } } } mainInit(urls, hostNames, ports, vms, proxyClient, noTile, hotspot); } public static boolean isDebug() { return debug; } private static void dbgStackTrace(Exception ex) { if (debug) { ex.printStackTrace(); } } private static final boolean localAttachmentSupported; static { boolean supported; try { Class.forName("com.sun.tools.attach.VirtualMachine"); Class.forName("sun.management.ConnectorAddressLink"); supported = true; } catch (NoClassDefFoundError x) { supported = false; } catch (ClassNotFoundException x) { supported = false; } localAttachmentSupported = supported; } public static boolean isLocalAttachAvailable() { return localAttachmentSupported; } private static ServiceLoader<JConsolePlugin> pluginService = null; // Return a list of newly instantiated JConsolePlugin objects static synchronized List<JConsolePlugin> getPlugins() { if (pluginService == null) { // First time loading and initializing the plugins initPluginService(pluginPath); } else { // reload the plugin so that new instances will be created pluginService.reload(); } List<JConsolePlugin> plugins = new ArrayList<JConsolePlugin>(); for (JConsolePlugin p : pluginService) { plugins.add(p); } return plugins; } private static void initPluginService(String pluginPath) { if (pluginPath.length() > 0) { try { ClassLoader pluginCL = new URLClassLoader(pathToURLs(pluginPath)); ServiceLoader<JConsolePlugin> plugins = ServiceLoader.load(JConsolePlugin.class, pluginCL); // validate all plugins for (JConsolePlugin p : plugins) { if (isDebug()) { System.out.println("Plugin " + p.getClass() + " loaded.");Lines 501–1000 of 1112. Previous lines Load more
final String userName, final String password, final boolean tile) { new Thread("JConsole.addUrl") { public void run() { try { addProxyClient(ProxyClient.getProxyClient(url, userName, password), tile); } catch (final MalformedURLException ex) { failed(ex, url, userName, password); } catch (final SecurityException ex) { failed(ex, url, userName, password); } catch (final IOException ex) { failed(ex, url, userName, password); } } }.start(); } // Call on EDT void addHost(final String hostName, final int port, final String userName, final String password, final boolean tile) { new Thread("JConsole.addHost") { public void run() { try { addProxyClient(ProxyClient.getProxyClient(hostName, port, userName, password), tile); } catch (final IOException ex) { dbgStackTrace(ex); SwingUtilities.invokeLater(new Runnable() { public void run() { showConnectDialog(null, hostName, port, userName, password, errorMessage(ex)); } }); } } }.start(); } // Call on worker thread void addProxyClient(final ProxyClient proxyClient, final boolean tile) { SwingUtilities.invokeLater(new Runnable() { public void run() { VMPanel vmPanel = new VMPanel(proxyClient, updateInterval); addFrame(vmPanel); if (tile) { SwingUtilities.invokeLater(new Runnable() { public void run() { tileWindows(); } }); } vmPanel.connect(); } }); } // Call on worker thread private void failed(final Exception ex, final String url, final String userName, final String password) { SwingUtilities.invokeLater(new Runnable() { public void run() { dbgStackTrace(ex); showConnectDialog(url, null, -1, userName, password, errorMessage(ex)); } }); } private VMInternalFrame addFrame(VMPanel vmPanel) { final VMInternalFrame vmIF = new VMInternalFrame(vmPanel); for (VMInternalFrame f : windows) { try { f.setMaximum(false); } catch (PropertyVetoException ex) { // Ignore } } desktop.add(vmIF); vmIF.setLocation(frameLoc, frameLoc); frameLoc += 30; vmIF.setVisible(true); windows.add(vmIF); if (windows.size() == 1) { try { vmIF.setMaximum(true); } catch (PropertyVetoException ex) { // Ignore } } vmIF.addInternalFrameListener(this); windowMenu.add(vmIF); return vmIF; } private void showConnectDialog(String url, String hostName, int port, String userName, String password, String msg) { if (connectDialog == null) { connectDialog = new ConnectDialog(this); } connectDialog.setConnectionParameters(url, hostName, port, userName, password, msg); connectDialog.refresh(); connectDialog.setVisible(true); try { // Bring to front of other dialogs connectDialog.setSelected(true); } catch (PropertyVetoException e) { } } private void showCreateMBeanDialog() { if (createDialog == null) { createDialog = new CreateMBeanDialog(this); } createDialog.setVisible(true); try { // Bring to front of other dialogs createDialog.setSelected(true); } catch (PropertyVetoException e) { } } private void removeVMInternalFrame(VMInternalFrame vmIF) { windowMenu.remove(vmIF); desktop.remove(vmIF); desktop.repaint(); vmIF.getVMPanel().cleanUp(); vmIF.dispose(); } private boolean isProxyClientUsed(ProxyClient client) { for(VMInternalFrame frame : windows) { ProxyClient cli = frame.getVMPanel().getProxyClient(false); if(client == cli) return true; } return false; } static boolean isValidRemoteString(String txt) { boolean valid = false; if (txt != null) { txt = txt.trim(); if (txt.startsWith(ROOT_URL)) { if (txt.length() > ROOT_URL.length()) { valid = true; } } else { //--------------------------------------- // Supported host and port combinations: // hostname:port // IPv4Address:port // [IPv6Address]:port //--------------------------------------- // Is literal IPv6 address? // if (txt.startsWith("[")) { int index = txt.indexOf("]:"); if (index != -1) { // Extract literal IPv6 address // String address = txt.substring(1, index); if (IPAddressUtil.isIPv6LiteralAddress(address)) { // Extract port // try { String portStr = txt.substring(index + 2); int port = Integer.parseInt(portStr); if (port >= 0 && port <= 0xFFFF) { valid = true; } } catch (NumberFormatException ex) { valid = false; } } } } else { String[] s = txt.split(":"); if (s.length == 2) { try { int port = Integer.parseInt(s[1]); if (port >= 0 && port <= 0xFFFF) { valid = true; } } catch (NumberFormatException ex) { valid = false; } } } } } return valid; } private String errorMessage(Exception ex) { String msg = Resources.getText("Connection failed"); if (ex instanceof IOException || ex instanceof SecurityException) { Throwable cause = null; Throwable c = ex.getCause(); while (c != null) { cause = c; c = c.getCause(); } if (cause instanceof ConnectException) { return msg + ": " + cause.getMessage(); } else if (cause instanceof UnknownHostException) { return Resources.getText("Unknown Host", cause.getMessage()); } else if (cause instanceof NoRouteToHostException) { return msg + ": " + cause.getMessage(); } else if (cause instanceof FailedLoginException) { return msg + ": " + cause.getMessage(); } else if (cause instanceof SSLHandshakeException) { return msg + ": "+ cause.getMessage(); } } else if (ex instanceof MalformedURLException) { return Resources.getText("Invalid URL", ex.getMessage()); } return msg + ": " + ex.getMessage(); } // InternalFrameListener interface public void internalFrameClosing(InternalFrameEvent e) { VMInternalFrame vmIF = (VMInternalFrame)e.getInternalFrame(); removeVMInternalFrame(vmIF); windows.remove(vmIF); ProxyClient client = vmIF.getVMPanel().getProxyClient(false); if(!isProxyClientUsed(client)) client.markAsDead(); if (windows.size() == 0) { showConnectDialog("", "", 0, null, null, null); } } public void internalFrameOpened(InternalFrameEvent e) {} public void internalFrameClosed(InternalFrameEvent e) {} public void internalFrameIconified(InternalFrameEvent e) {} public void internalFrameDeiconified(InternalFrameEvent e) {} public void internalFrameActivated(InternalFrameEvent e) {} public void internalFrameDeactivated(InternalFrameEvent e) {} private static void usage() { System.err.println(Resources.getText("zz usage text", "jconsole")); } private static void mainInit(final List<String> urls, final List<String> hostNames, final List<Integer> ports, final List<LocalVirtualMachine> vmids, final ProxyClient proxyClient, final boolean noTile, final boolean hotspot) { // Always create Swing GUI on the Event Dispatching Thread SwingUtilities.invokeLater(new Runnable() { public void run() { JConsole jConsole = new JConsole(hotspot); // Center the window on screen, taking into account screen // size and insets. Toolkit toolkit = Toolkit.getDefaultToolkit(); GraphicsConfiguration gc = jConsole.getGraphicsConfiguration(); Dimension scrSize = toolkit.getScreenSize(); Insets scrInsets = toolkit.getScreenInsets(gc); Rectangle scrBounds = new Rectangle(scrInsets.left, scrInsets.top, scrSize.width - scrInsets.left - scrInsets.right, scrSize.height - scrInsets.top - scrInsets.bottom); int w = Math.min(900, scrBounds.width); int h = Math.min(750, scrBounds.height); jConsole.setBounds(scrBounds.x + (scrBounds.width - w) / 2, scrBounds.y + (scrBounds.height - h) / 2, w, h); jConsole.setVisible(true); jConsole.createMDI(); for (int i = 0; i < hostNames.size(); i++) { jConsole.addHost(hostNames.get(i), ports.get(i), null, null, (i == hostNames.size() - 1) ? !noTile : false); } for (int i = 0; i < urls.size(); i++) { jConsole.addUrl(urls.get(i), null, null, (i == urls.size() - 1) ? !noTile : false); } for (int i = 0; i < vmids.size(); i++) { jConsole.addVmid(vmids.get(i), (i == vmids.size() - 1) ? !noTile : false); } if (vmids.size() == 0 && hostNames.size() == 0 && urls.size() == 0) { jConsole.showConnectDialog(null, null, 0, null, null, null); } } }); } public static void main(String[] args) { boolean noTile = false, hotspot = false; int argIndex = 0; ProxyClient proxyClient = null; if (System.getProperty("jconsole.showOutputViewer") != null) { OutputViewer.init(); } while (args.length - argIndex > 0 && args[argIndex].startsWith("-")) { String arg = args[argIndex++]; if (arg.equals("-h") || arg.equals("-help") || arg.equals("-?")) { usage(); return; } else if (arg.startsWith("-interval=")) { try { updateInterval = Integer.parseInt(arg.substring(10)) * 1000; } catch (NumberFormatException ex) { usage(); return; } } else if (arg.equals("-pluginpath")) { if (argIndex < args.length && !args[argIndex].startsWith("-")) { pluginPath = args[argIndex++]; } else { // Invalid argument usage(); return; } } else if (arg.equals("-notile")) { noTile = true; } else if (arg.equals("-version")) { Version.print(System.err); return; } else if (arg.equals("-debug")) { debug = true; } else if (arg.equals("-fullversion")) { Version.printFullVersion(System.err); return; } else { // Unknown switch usage(); return; } } if (System.getProperty("jconsole.showUnsupported") != null) { hotspot = true; } List<String> urls = new ArrayList<String>(); List<String> hostNames = new ArrayList<String>(); List<Integer> ports = new ArrayList<Integer>(); List<LocalVirtualMachine> vms = new ArrayList<LocalVirtualMachine>(); for (int i = argIndex; i < args.length; i++) { String arg = args[i]; if (isValidRemoteString(arg)) { if (arg.startsWith(ROOT_URL)) { urls.add(arg); } else if (arg.matches(".*:[0-9]*")) { int p = arg.lastIndexOf(':'); hostNames.add(arg.substring(0, p)); try { ports.add(Integer.parseInt(arg.substring(p+1))); } catch (NumberFormatException ex) { usage(); return; } } } else { if (!isLocalAttachAvailable()) { System.err.println("Local process monitoring is not supported"); return; } try { int vmid = Integer.parseInt(arg); LocalVirtualMachine lvm = LocalVirtualMachine.getLocalVirtualMachine(vmid); if (lvm == null) { System.err.println("Invalid process id:" + vmid); return; } vms.add(lvm); } catch (NumberFormatException ex) { usage(); return; } } } mainInit(urls, hostNames, ports, vms, proxyClient, noTile, hotspot); } public static boolean isDebug() { return debug; } private static void dbgStackTrace(Exception ex) { if (debug) { ex.printStackTrace(); } } private static final boolean localAttachmentSupported; static { boolean supported; try { Class.forName("com.sun.tools.attach.VirtualMachine"); Class.forName("sun.management.ConnectorAddressLink"); supported = true; } catch (NoClassDefFoundError x) { supported = false; } catch (ClassNotFoundException x) { supported = false; } localAttachmentSupported = supported; } public static boolean isLocalAttachAvailable() { return localAttachmentSupported; } private static ServiceLoader<JConsolePlugin> pluginService = null; // Return a list of newly instantiated JConsolePlugin objects static synchronized List<JConsolePlugin> getPlugins() { if (pluginService == null) { // First time loading and initializing the plugins initPluginService(pluginPath); } else { // reload the plugin so that new instances will be created pluginService.reload(); } List<JConsolePlugin> plugins = new ArrayList<JConsolePlugin>(); for (JConsolePlugin p : pluginService) { plugins.add(p); } return plugins; } private static void initPluginService(String pluginPath) { if (pluginPath.length() > 0) { try { ClassLoader pluginCL = new URLClassLoader(pathToURLs(pluginPath)); ServiceLoader<JConsolePlugin> plugins = ServiceLoader.load(JConsolePlugin.class, pluginCL); // validate all plugins for (JConsolePlugin p : plugins) { if (isDebug()) { System.out.println("Plugin " + p.getClass() + " loaded.");