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
26package 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 */
35public 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 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 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 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 cmdLine = commandLine(vm);
106        int firstSpace = cmdLine.indexOf(' ');
107        if (firstSpace > 0) {
108            cmdLine = cmdLine.substring(0, firstSpace);
109        }
110        if (fullPath) {
111            return cmdLine;
112        }
113        /*
114         * Can't use File.separator() here because the separator for the target
115         * jvm may be different than the separator for the monitoring jvm.
116         * And we also strip embedded module e.g. "module/MainClass"
117         */
118        int lastSlash = cmdLine.lastIndexOf("/");
119        int lastBackslash = cmdLine.lastIndexOf("\\");
120        int lastSeparator = lastSlash > lastBackslash ? lastSlash : lastBackslash;
121        if (lastSeparator > 0) {
122            cmdLine = cmdLine.substring(lastSeparator + 1);
123        }
124
125        int lastPackageSeparator = cmdLine.lastIndexOf('.');
126        if (lastPackageSeparator > 0) {
127            String lastPart = cmdLine.substring(lastPackageSeparator + 1);
128            /*
129             * We could have a relative path "my.module" or
130             * a module called "my.module" and a jar file called "my.jar" or
131             * class named "jar" in package "my", e.g. "my.jar".
132             * We can never be sure here, but we assume *.jar is a jar file
133             */
134            if (lastPart.equals("jar")) {
135                return cmdLine; /* presumably a file name without path */
136            }
137            return lastPart; /* presumably a class name without package */
138        }
139
140        return cmdLine;
141    }
142
143    /**
144     * Return the JVM arguments for the target Java application.
145     *
146     * @param vm the target MonitoredVm
147     * @return String - contains the arguments passed to the JVM for the
148     *                  target Java application or the string "Unknown"
149     *                  if the command line cannot be determined.
150     */
151    public static String jvmArgs(MonitoredVm vm) throws MonitorException {
152        StringMonitor jvmArgs = (StringMonitor)vm.findByName("java.rt.vmArgs");
153        return (jvmArgs == null) ? "Unknown" : jvmArgs.stringValue();
154    }
155
156    /**
157     * Return the JVM flags for the target Java application.
158     *
159     * @param vm the target MonitoredVm
160     * @return String - contains the flags passed to the JVM for the
161     *                  target Java application or the string "Unknown"
162     *                  if the command line cannot be determined.
163     */
164    public static String jvmFlags(MonitoredVm vm) throws MonitorException {
165        StringMonitor jvmFlags =
166               (StringMonitor)vm.findByName("java.rt.vmFlags");
167        return (jvmFlags == null) ? "Unknown" : jvmFlags.stringValue();
168    }
169
170    // Index of the sun.rt.jvmCapabilities counter
171    private static int IS_ATTACHABLE = 0;
172    private static int IS_KERNEL_VM  = 1;
173
174    /**
175     * Returns true if the VM supports attach-on-demand.
176     *
177     * @param vm the target MonitoredVm
178     */
179    public static boolean isAttachable(MonitoredVm vm) throws MonitorException {
180        StringMonitor jvmCapabilities =
181               (StringMonitor)vm.findByName("sun.rt.jvmCapabilities");
182        if (jvmCapabilities == null) {
183             return false;
184        } else {
185             return jvmCapabilities.stringValue().charAt(IS_ATTACHABLE) == '1';
186        }
187    }
188
189}
190