source: josm/trunk/src/org/openstreetmap/josm/actions/ShowStatusReportAction.java@ 8379

Last change on this file since 8379 was 8379, checked in by Don-vip, 9 years ago

Consecutively calls to StringBuffer/StringBuilder .append should reuse the target object

  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Dimension;
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.lang.management.ManagementFactory;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Collection;
14import java.util.HashSet;
15import java.util.List;
16import java.util.ListIterator;
17import java.util.Map;
18import java.util.Map.Entry;
19import java.util.Set;
20
21import javax.swing.JScrollPane;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.data.Preferences.Setting;
25import org.openstreetmap.josm.data.Version;
26import org.openstreetmap.josm.data.osm.DataSet;
27import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
28import org.openstreetmap.josm.gui.ExtendedDialog;
29import org.openstreetmap.josm.gui.widgets.JosmTextArea;
30import org.openstreetmap.josm.plugins.PluginHandler;
31import org.openstreetmap.josm.tools.PlatformHookUnixoid;
32import org.openstreetmap.josm.tools.Shortcut;
33import org.openstreetmap.josm.tools.Utils;
34
35/**
36 * @author xeen
37 *
38 * Opens a dialog with useful status information like version numbers for Java, JOSM and plugins
39 * Also includes preferences with stripped username and password
40 */
41public final class ShowStatusReportAction extends JosmAction {
42
43 /**
44 * Constructs a new {@code ShowStatusReportAction}
45 */
46 public ShowStatusReportAction() {
47 super(
48 tr("Show Status Report"),
49 "clock",
50 tr("Show status report with useful information that can be attached to bugs"),
51 Shortcut.registerShortcut("help:showstatusreport", tr("Help: {0}",
52 tr("Show Status Report")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false);
53
54 putValue("help", ht("/Action/ShowStatusReport"));
55 putValue("toolbar", "help/showstatusreport");
56 Main.toolbar.register(this);
57 }
58
59 private static void shortenParam(ListIterator<String> it, String[] param, String source, String target) {
60 if (source != null && target.length() < source.length() && param[1].startsWith(source)) {
61 it.set(param[0] + "=" + param[1].replace(source, target));
62 }
63 }
64
65 /**
66 * Replies the report header (software and system info)
67 * @return The report header (software and system info)
68 */
69 public static String getReportHeader() {
70 StringBuilder text = new StringBuilder();
71 text.append(Version.getInstance().getReleaseAttributes())
72 .append("\n")
73 .append("Identification: ").append(Version.getInstance().getAgentString())
74 .append("\n")
75 .append("Memory Usage: ")
76 .append(Runtime.getRuntime().totalMemory()/1024/1024)
77 .append(" MB / ")
78 .append(Runtime.getRuntime().maxMemory()/1024/1024)
79 .append(" MB (")
80 .append(Runtime.getRuntime().freeMemory()/1024/1024)
81 .append(" MB allocated, but free)")
82 .append("\n")
83 .append("Java version: ").append(System.getProperty("java.version")).append(", ")
84 .append(System.getProperty("java.vendor")).append(", ").append(System.getProperty("java.vm.name"))
85 .append("\n");
86 if (Main.platform.getClass() == PlatformHookUnixoid.class) {
87 // Add Java package details
88 String packageDetails = ((PlatformHookUnixoid) Main.platform).getJavaPackageDetails();
89 if (packageDetails != null) {
90 text.append("Java package: ")
91 .append(packageDetails)
92 .append("\n");
93 }
94 // Add WebStart package details if run from JNLP
95 if (Package.getPackage("javax.jnlp") != null) {
96 String webStartDetails = ((PlatformHookUnixoid) Main.platform).getWebStartPackageDetails();
97 if (webStartDetails != null) {
98 text.append("WebStart package: ")
99 .append(webStartDetails)
100 .append("\n");
101 }
102 }
103 }
104 try {
105 final String envJavaHome = System.getenv("JAVA_HOME");
106 final String envJavaHomeAlt = Main.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}";
107 final String propJavaHome = System.getProperty("java.home");
108 final String propJavaHomeAlt = "<java.home>";
109 // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance)
110 List<String> vmArguments = new ArrayList<>(ManagementFactory.getRuntimeMXBean().getInputArguments());
111 for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext(); ) {
112 String value = it.next();
113 if (value.contains("=")) {
114 String[] param = value.split("=");
115 // Hide some parameters for privacy concerns
116 if (param[0].toLowerCase().startsWith("-dproxy")) {
117 it.set(param[0]+"=xxx");
118 // Shorten some parameters for readability concerns
119 } else {
120 shortenParam(it, param, envJavaHome, envJavaHomeAlt);
121 shortenParam(it, param, propJavaHome, propJavaHomeAlt);
122 }
123 } else if (value.startsWith("-X")) {
124 // Remove arguments like -Xbootclasspath/a, -Xverify:remote, that can be very long and unhelpful
125 it.remove();
126 }
127 }
128 if (!vmArguments.isEmpty()) {
129 text.append("VM arguments: "+ vmArguments.toString().replace("\\\\", "\\"));
130 text.append("\n");
131 }
132 } catch (SecurityException e) {
133 // Ignore exception
134 }
135 List<String> commandLineArgs = Main.getCommandLineArgs();
136 if (!commandLineArgs.isEmpty()) {
137 text.append("Program arguments: "+ Arrays.toString(commandLineArgs.toArray()));
138 text.append("\n");
139 }
140 if (Main.main != null) {
141 DataSet dataset = Main.main.getCurrentDataSet();
142 if (dataset != null) {
143 String result = DatasetConsistencyTest.runTests(dataset);
144 if (result.length() == 0) {
145 text.append("Dataset consistency test: No problems found\n");
146 } else {
147 text.append("\nDataset consistency test:\n"+result+"\n");
148 }
149 }
150 }
151 text.append("\n")
152 .append(PluginHandler.getBugReportText())
153 .append("\n");
154
155 Collection<String> errorsWarnings = Main.getLastErrorAndWarnings();
156 if (!errorsWarnings.isEmpty()) {
157 text.append("Last errors/warnings:\n");
158 for (String s : errorsWarnings) {
159 text.append("- ").append(s).append("\n");
160 }
161 text.append("\n");
162 }
163
164 return text.toString();
165 }
166
167 @Override
168 public void actionPerformed(ActionEvent e) {
169 StringBuilder text = new StringBuilder();
170 String reportHeader = getReportHeader();
171 text.append(reportHeader);
172 try {
173 Map<String, Setting<?>> settings = Main.pref.getAllSettings();
174 Set<String> keys = new HashSet<>(settings.keySet());
175 for (String key : keys) {
176 // Remove sensitive information from status report
177 if (key.startsWith("marker.show") || key.contains("username") || key.contains("password") || key.contains("access-token")) {
178 settings.remove(key);
179 }
180 }
181 for (Entry<String, Setting<?>> entry : settings.entrySet()) {
182 text.append(entry.getKey()).append("=").append(entry.getValue().getValue()).append("\n");
183 }
184 } catch (Exception x) {
185 Main.error(x);
186 }
187
188 JosmTextArea ta = new JosmTextArea(text.toString());
189 ta.setWrapStyleWord(true);
190 ta.setLineWrap(true);
191 ta.setEditable(false);
192 JScrollPane sp = new JScrollPane(ta);
193
194 ExtendedDialog ed = new ExtendedDialog(Main.parent,
195 tr("Status Report"),
196 new String[] {tr("Copy to clipboard and close"), tr("Report bug"), tr("Close") });
197 ed.setButtonIcons(new String[] {"copy", "bug", "cancel" });
198 ed.setContent(sp, false);
199 ed.setMinimumSize(new Dimension(380, 200));
200 ed.setPreferredSize(new Dimension(700, Main.parent.getHeight()-50));
201
202 switch (ed.showDialog().getValue()) {
203 case 1: Utils.copyToClipboard(text.toString()); break;
204 case 2: ReportBugAction.reportBug(reportHeader) ; break;
205 }
206 }
207}
Note: See TracBrowser for help on using the repository browser.