author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 14342 | jdk/src/share/classes/sun/tools/jcmd/Arguments.java@8435a30053c1 |
child 35967 | 31cdfa4bf870 |
permissions | -rw-r--r-- |
11365 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
11365
diff
changeset
|
2 |
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. |
11365 | 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.tools.jcmd; |
|
27 |
||
28 |
import java.io.BufferedReader; |
|
29 |
import java.io.FileReader; |
|
30 |
import java.io.IOException; |
|
31 |
||
32 |
class Arguments { |
|
33 |
private boolean listProcesses = false; |
|
34 |
private boolean listCounters = false; |
|
35 |
private boolean showUsage = false; |
|
36 |
private int pid = -1; |
|
37 |
private String command = null; |
|
38 |
private String processSubstring; |
|
39 |
||
40 |
public boolean isListProcesses() { return listProcesses; } |
|
41 |
public boolean isListCounters() { return listCounters; } |
|
42 |
public boolean isShowUsage() { return showUsage; } |
|
43 |
public int getPid() { return pid; } |
|
44 |
public String getCommand() { return command; } |
|
45 |
public String getProcessSubstring() { return processSubstring; } |
|
46 |
||
47 |
public Arguments(String[] args) { |
|
48 |
if (args.length == 0 || args[0].equals("-l")) { |
|
49 |
listProcesses = true; |
|
50 |
return; |
|
51 |
} |
|
52 |
||
53 |
if (args[0].equals("-h") || args[0].equals("-help") ) { |
|
54 |
showUsage = true; |
|
55 |
return; |
|
56 |
} |
|
57 |
||
58 |
try { |
|
59 |
pid = Integer.parseInt(args[0]); |
|
60 |
} catch (NumberFormatException ex) { |
|
61 |
// use as a partial class-name instead |
|
62 |
if (args[0].charAt(0) != '-') { |
|
63 |
// unless it starts with a '-' |
|
64 |
processSubstring = args[0]; |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
StringBuilder sb = new StringBuilder(); |
|
69 |
for (int i = 1; i < args.length; i++) { |
|
70 |
if (args[i].equals("-f")) { |
|
71 |
if (args.length == i + 1) { |
|
72 |
throw new IllegalArgumentException( |
|
73 |
"No file specified for parameter -f"); |
|
74 |
} else if (args.length == i + 2) { |
|
75 |
try { |
|
76 |
readCommandFile(args[i + 1]); |
|
77 |
} catch(IOException e) { |
|
78 |
throw new IllegalArgumentException( |
|
79 |
"Could not read from file specified with -f option: " |
|
80 |
+ args[i + 1]); |
|
81 |
} |
|
82 |
return; |
|
83 |
} else { |
|
84 |
throw new IllegalArgumentException( |
|
85 |
"Options after -f are not allowed"); |
|
86 |
} |
|
87 |
} else if (args[i].equals("PerfCounter.print")) { |
|
88 |
listCounters = true; |
|
89 |
} else { |
|
90 |
sb.append(args[i]).append(" "); |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
if (listCounters != true && sb.length() == 0) { |
|
95 |
throw new IllegalArgumentException("No command specified"); |
|
96 |
} |
|
97 |
||
98 |
command = sb.toString().trim(); |
|
99 |
} |
|
100 |
||
101 |
private void readCommandFile(String path) throws IOException { |
|
102 |
try (BufferedReader bf = new BufferedReader(new FileReader(path));) { |
|
103 |
StringBuilder sb = new StringBuilder(); |
|
104 |
String s; |
|
105 |
while ((s = bf.readLine()) != null) { |
|
106 |
sb.append(s).append("\n"); |
|
107 |
} |
|
108 |
command = sb.toString(); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
public static void usage() { |
|
113 |
System.out.println("Usage: jcmd <pid | main class> <command ...|PerfCounter.print|-f file>"); |
|
114 |
System.out.println(" or: jcmd -l "); |
|
115 |
System.out.println(" or: jcmd -h "); |
|
116 |
System.out.println(" "); |
|
117 |
System.out.println(" command must be a valid jcmd command for the selected jvm. "); |
|
118 |
System.out.println(" Use the command \"help\" to see which commands are available. "); |
|
119 |
System.out.println(" If the pid is 0, commands will be sent to all Java processes. "); |
|
120 |
System.out.println(" The main class argument will be used to match (either partially "); |
|
121 |
System.out.println(" or fully) the class used to start Java. "); |
|
122 |
System.out.println(" If no options are given, lists Java processes (same as -p). "); |
|
123 |
System.out.println(" "); |
|
124 |
System.out.println(" PerfCounter.print display the counters exposed by this process "); |
|
125 |
System.out.println(" -f read and execute commands from the file "); |
|
126 |
System.out.println(" -l list JVM processes on the local machine "); |
|
127 |
System.out.println(" -h this help "); |
|
128 |
} |
|
129 |
} |