J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.tools.jps; |
| 27 | |
| 28 | import java.util.*; |
| 29 | import java.net.*; |
| 30 | import sun.jvmstat.monitor.*; |
| 31 | |
| 32 | /** |
| 33 | * Application to provide a listing of monitorable java processes. |
| 34 | * |
| 35 | * @author Brian Doherty |
| 36 | * @since 1.5 |
| 37 | */ |
| 38 | public class Jps { |
| 39 | |
| 40 | private static Arguments arguments; |
| 41 | |
| 42 | public static void main(String[] args) { |
| 43 | try { |
| 44 | arguments = new Arguments(args); |
| 45 | } catch (IllegalArgumentException e) { |
| 46 | System.err.println(e.getMessage()); |
| 47 | Arguments.printUsage(System.err); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | if (arguments.isHelp()) { |
| 52 | Arguments.printUsage(System.out); |
| 53 | System.exit(0); |
| 54 | } |
| 55 | |
| 56 | try { |
| 57 | HostIdentifier hostId = arguments.hostId(); |
| 58 | MonitoredHost monitoredHost = |
| 59 | MonitoredHost.getMonitoredHost(hostId); |
| 60 | |
| 61 | // get the set active JVMs on the specified host. |
| 62 | Set jvms = monitoredHost.activeVms(); |
| 63 | |
| 64 | for (Iterator j = jvms.iterator(); j.hasNext(); /* empty */ ) { |
| 65 | StringBuilder output = new StringBuilder(); |
| 66 | Throwable lastError = null; |
| 67 | |
| 68 | int lvmid = ((Integer)j.next()).intValue(); |
| 69 | |
| 70 | output.append(String.valueOf(lvmid)); |
| 71 | |
| 72 | if (arguments.isQuiet()) { |
| 73 | System.out.println(output); |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | MonitoredVm vm = null; |
| 78 | String vmidString = "//" + lvmid + "?mode=r"; |
| 79 | |
| 80 | try { |
| 81 | VmIdentifier id = new VmIdentifier(vmidString); |
| 82 | vm = monitoredHost.getMonitoredVm(id, 0); |
| 83 | } catch (URISyntaxException e) { |
| 84 | // unexpected as vmidString is based on a validated hostid |
| 85 | lastError = e; |
| 86 | assert false; |
| 87 | } catch (Exception e) { |
| 88 | lastError = e; |
| 89 | } finally { |
| 90 | if (vm == null) { |
| 91 | /* |
| 92 | * we ignore most exceptions, as there are race |
| 93 | * conditions where a JVM in 'jvms' may terminate |
| 94 | * before we get a chance to list its information. |
| 95 | * Other errors, such as access and I/O exceptions |
| 96 | * should stop us from iterating over the complete set. |
| 97 | */ |
| 98 | output.append(" -- process information unavailable"); |
| 99 | if (arguments.isDebug()) { |
| 100 | if ((lastError != null) |
| 101 | && (lastError.getMessage() != null)) { |
| 102 | output.append("\n\t"); |
| 103 | output.append(lastError.getMessage()); |
| 104 | } |
| 105 | } |
| 106 | System.out.println(output); |
| 107 | if (arguments.printStackTrace()) { |
| 108 | lastError.printStackTrace(); |
| 109 | } |
| 110 | continue; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | output.append(" "); |
| 115 | output.append(MonitoredVmUtil.mainClass(vm, |
| 116 | arguments.showLongPaths())); |
| 117 | |
| 118 | if (arguments.showMainArgs()) { |
| 119 | String mainArgs = MonitoredVmUtil.mainArgs(vm); |
| 120 | if (mainArgs != null && mainArgs.length() > 0) { |
| 121 | output.append(" ").append(mainArgs); |
| 122 | } |
| 123 | } |
| 124 | if (arguments.showVmArgs()) { |
| 125 | String jvmArgs = MonitoredVmUtil.jvmArgs(vm); |
| 126 | if (jvmArgs != null && jvmArgs.length() > 0) { |
| 127 | output.append(" ").append(jvmArgs); |
| 128 | } |
| 129 | } |
| 130 | if (arguments.showVmFlags()) { |
| 131 | String jvmFlags = MonitoredVmUtil.jvmFlags(vm); |
| 132 | if (jvmFlags != null && jvmFlags.length() > 0) { |
| 133 | output.append(" ").append(jvmFlags); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | System.out.println(output); |
| 138 | |
| 139 | monitoredHost.detach(vm); |
| 140 | } |
| 141 | } catch (MonitorException e) { |
| 142 | if (e.getMessage() != null) { |
| 143 | System.err.println(e.getMessage()); |
| 144 | } else { |
| 145 | Throwable cause = e.getCause(); |
| 146 | if ((cause != null) && (cause.getMessage() != null)) { |
| 147 | System.err.println(cause.getMessage()); |
| 148 | } else { |
| 149 | e.printStackTrace(); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |