|
1 /* |
|
2 * Copyright (c) 2016, 2018, 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 jdk.jfr.internal.cmd; |
|
27 |
|
28 import java.io.IOException; |
|
29 import java.io.PrintWriter; |
|
30 import java.nio.file.Path; |
|
31 import java.nio.file.Paths; |
|
32 import java.util.Deque; |
|
33 |
|
34 final class PrintCommand extends Command { |
|
35 @Override |
|
36 public String getName() { |
|
37 return "print"; |
|
38 } |
|
39 |
|
40 @Override |
|
41 public String getOptionSyntax() { |
|
42 return "[--xml|--json] <file>"; |
|
43 } |
|
44 |
|
45 @Override |
|
46 public String getDescription() { |
|
47 return "Print contents of a recording file (.jfr)"; |
|
48 } |
|
49 |
|
50 @Override |
|
51 public void displayOptionUsage() { |
|
52 println(" --xml Print a recording in XML format"); |
|
53 println(); |
|
54 println(" --json Print a recording in JSON format"); |
|
55 println(); |
|
56 println(" <file> Location of the recording file (.jfr) to print"); |
|
57 } |
|
58 |
|
59 @Override |
|
60 public void execute(Deque<String> options) { |
|
61 if (options.isEmpty()) { |
|
62 userFailed("Missing file"); |
|
63 } |
|
64 ensureMaxArgumentCount(options, 2); |
|
65 |
|
66 Path file = Paths.get(options.removeLast()); |
|
67 |
|
68 ensureFileExist(file); |
|
69 ensureJFRFile(file); |
|
70 ensureMaxArgumentCount(options, 1); |
|
71 |
|
72 String format = "--pretty"; |
|
73 if (!options.isEmpty()) { |
|
74 format = options.remove(); |
|
75 } |
|
76 try (PrintWriter pw = new PrintWriter(System.out)) { |
|
77 try { |
|
78 switch (format) { |
|
79 case "--pretty": |
|
80 PrettyWriter prettyWriter = new PrettyWriter(pw); |
|
81 prettyWriter.print(file); |
|
82 break; |
|
83 case "--xml": |
|
84 XMLWriter xmlPrinter = new XMLWriter(pw); |
|
85 xmlPrinter.print(file); |
|
86 break; |
|
87 case "--json": |
|
88 JSONWriter jsonWriter = new JSONWriter(pw); |
|
89 jsonWriter.print(file); |
|
90 break; |
|
91 default: |
|
92 userFailed("Unknown option " + format); |
|
93 break; |
|
94 } |
|
95 } catch (IOException ioe) { |
|
96 userFailed("Could not read recording at " + file.toAbsolutePath() + ". " + ioe.getMessage()); |
|
97 } |
|
98 } |
|
99 } |
|
100 } |