Showing the changes to java/pkg/sun/jvmstat/monitor/MonitoredVmUtil.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,182 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2004, 2011, Oracle and/or its affiliates. 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. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package sun.jvmstat.monitor; | ||
| 27 | |||
| 28 | /** | ||
| 29 | * Utility class proving concenience methods for extracting various | ||
| 30 | * information from an MonitoredVm object. | ||
| 31 | * | ||
| 32 | * @author Brian Doherty | ||
| 33 | * @since 1.5 | ||
| 34 | */ | ||
| 35 | public class MonitoredVmUtil { | ||
| 36 | |||
| 37 | /** | ||
| 38 | * Private constructor - prevent instantiation. | ||
| 39 | */ | ||
| 40 | private MonitoredVmUtil() { } | ||
| 41 | |||
| 42 | /** | ||
| 43 | * Return the Java Virtual Machine Version. | ||
| 44 | * | ||
| 45 | * @param vm the target MonitoredVm | ||
| 46 | * @return String - contains the version of the target JVM or the | ||
| 47 | * the string "Unknown" if the version cannot be | ||
| 48 | * determined. | ||
| 49 | */ | ||
| 50 | public static String vmVersion(MonitoredVm vm) throws MonitorException { | ||
| 51 | StringMonitor ver = | ||
| 52 | (StringMonitor)vm.findByName("java.property.java.vm.version"); | ||
| 53 | return (ver == null) ? "Unknown" : ver.stringValue(); | ||
| 54 | } | ||
| 55 | |||
| 56 | /** | ||
| 57 | * Return the command line for the target Java application. | ||
| 58 | * | ||
| 59 | * @param vm the target MonitoredVm | ||
| 60 | * @return String - contains the command line of the target Java | ||
| 61 | * application or the the string "Unknown" if the | ||
| 62 | * command line cannot be determined. | ||
| 63 | */ | ||
| 64 | public static String commandLine(MonitoredVm vm) throws MonitorException { | ||
| 65 | StringMonitor cmd = (StringMonitor)vm.findByName("sun.rt.javaCommand"); | ||
| 66 | return (cmd == null) ? "Unknown" : cmd.stringValue(); | ||
| 67 | } | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Return the arguments to the main class for the target Java application. | ||
| 71 | * Returns the arguments to the main class. If the arguments can't be | ||
| 72 | * found, the string "Unknown" is returned. | ||
| 73 | * | ||
| 74 | * @param vm the target MonitoredVm | ||
| 75 | * @return String - contains the arguments to the main class for the | ||
| 76 | * target Java application or the the string "Unknown" | ||
| 77 | * if the command line cannot be determined. | ||
| 78 | */ | ||
| 79 | public static String mainArgs(MonitoredVm vm) throws MonitorException { | ||
| 80 | String commandLine = commandLine(vm); | ||
| 81 | |||
| 82 | int firstSpace = commandLine.indexOf(' '); | ||
| 83 | if (firstSpace > 0) { | ||
| 84 | return commandLine.substring(firstSpace + 1); | ||
| 85 | } else if (commandLine.compareTo("Unknown") == 0) { | ||
| 86 | return commandLine; | ||
| 87 | } else { | ||
| 88 | return null; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Return the main class for the target Java application. | ||
| 94 | * Returns the main class or the name of the jar file if the application | ||
| 95 | * was started with the <em>-jar</em> option. | ||
| 96 | * | ||
| 97 | * @param vm the target MonitoredVm | ||
| 98 | * @param fullPath include the full path to Jar file, where applicable | ||
| 99 | * @return String - contains the main class of the target Java | ||
| 100 | * application or the the string "Unknown" if the | ||
| 101 | * command line cannot be determined. | ||
| 102 | */ | ||
| 103 | public static String mainClass(MonitoredVm vm, boolean fullPath) | ||
| 104 | throws MonitorException { | ||
| 105 | String commandLine = commandLine(vm); | ||
| 106 | String arg0 = commandLine; | ||
| 107 | |||
| 108 | int firstSpace = commandLine.indexOf(' '); | ||
| 109 | if (firstSpace > 0) { | ||
| 110 | arg0 = commandLine.substring(0, firstSpace); | ||
| 111 | } | ||
| 112 | if (!fullPath) { | ||
| 113 | /* | ||
| 114 | * can't use File.separator() here because the separator | ||
| 115 | * for the target jvm may be different than the separator | ||
| 116 | * for the monitoring jvm. | ||
| 117 | */ | ||
| 118 | int lastFileSeparator = arg0.lastIndexOf('/'); | ||
| 119 | if (lastFileSeparator > 0) { | ||
| 120 | return arg0.substring(lastFileSeparator + 1); | ||
| 121 | } | ||
| 122 | |||
| 123 | lastFileSeparator = arg0.lastIndexOf('\\'); | ||
| 124 | if (lastFileSeparator > 0) { | ||
| 125 | return arg0.substring(lastFileSeparator + 1); | ||
| 126 | } | ||
| 127 | |||
| 128 | int lastPackageSeparator = arg0.lastIndexOf('.'); | ||
| 129 | if (lastPackageSeparator > 0) { | ||
| 130 | return arg0.substring(lastPackageSeparator + 1); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | return arg0; | ||
| 134 | } | ||
| 135 | |||
| 136 | /** | ||
| 137 | * Return the JVM arguments for the target Java application. | ||
| 138 | * | ||
| 139 | * @param vm the target MonitoredVm | ||
| 140 | * @return String - contains the arguments passed to the JVM for the | ||
| 141 | * target Java application or the the string "Unknown" | ||
| 142 | * if the command line cannot be determined. | ||
| 143 | */ | ||
| 144 | public static String jvmArgs(MonitoredVm vm) throws MonitorException { | ||
| 145 | StringMonitor jvmArgs = (StringMonitor)vm.findByName("java.rt.vmArgs"); | ||
| 146 | return (jvmArgs == null) ? "Unknown" : jvmArgs.stringValue(); | ||
| 147 | } | ||
| 148 | |||
| 149 | /** | ||
| 150 | * Return the JVM flags for the target Java application. | ||
| 151 | * | ||
| 152 | * @param vm the target MonitoredVm | ||
| 153 | * @return String - contains the flags passed to the JVM for the | ||
| 154 | * target Java application or the the string "Unknown" | ||
| 155 | * if the command line cannot be determined. | ||
| 156 | */ | ||
| 157 | public static String jvmFlags(MonitoredVm vm) throws MonitorException { | ||
| 158 | StringMonitor jvmFlags = | ||
| 159 | (StringMonitor)vm.findByName("java.rt.vmFlags"); | ||
| 160 | return (jvmFlags == null) ? "Unknown" : jvmFlags.stringValue(); | ||
| 161 | } | ||
| 162 | |||
| 163 | // Index of the sun.rt.jvmCapabilities counter | ||
| 164 | private static int IS_ATTACHABLE = 0; | ||
| 165 | private static int IS_KERNEL_VM = 1; | ||
| 166 | |||
| 167 | /** | ||
| 168 | * Returns true if the VM supports attach-on-demand. | ||
| 169 | * | ||
| 170 | * @param vm the target MonitoredVm | ||
| 171 | */ | ||
| 172 | public static boolean isAttachable(MonitoredVm vm) throws MonitorException { | ||
| 173 | StringMonitor jvmCapabilities = | ||
| 174 | (StringMonitor)vm.findByName("sun.rt.jvmCapabilities"); | ||
| 175 | if (jvmCapabilities == null) { | ||
| 176 | return false; | ||
| 177 | } else { | ||
| 178 | return jvmCapabilities.stringValue().charAt(IS_ATTACHABLE) == '1'; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | } | ||