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

Last change on this file since 10601 was 10463, checked in by Don-vip, 8 years ago

sonar - squid:S1166 - Exception handlers should preserve the original exceptions

  • Property svn:eol-style set to native
File size: 12.1 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.Locale;
18import java.util.Map;
19import java.util.Map.Entry;
20import java.util.Set;
21import java.util.TreeSet;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.data.Version;
25import org.openstreetmap.josm.data.osm.DataSet;
26import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
27import org.openstreetmap.josm.data.preferences.Setting;
28import org.openstreetmap.josm.gui.ExtendedDialog;
29import org.openstreetmap.josm.gui.preferences.SourceEditor;
30import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
31import org.openstreetmap.josm.gui.preferences.SourceEntry;
32import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
33import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
34import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference;
35import org.openstreetmap.josm.io.OsmApi;
36import org.openstreetmap.josm.plugins.PluginHandler;
37import org.openstreetmap.josm.tools.PlatformHookUnixoid;
38import org.openstreetmap.josm.tools.Shortcut;
39import org.openstreetmap.josm.tools.bugreport.BugReportSender;
40import org.openstreetmap.josm.tools.bugreport.DebugTextDisplay;
41
42/**
43 * @author xeen
44 *
45 * Opens a dialog with useful status information like version numbers for Java, JOSM and plugins
46 * Also includes preferences with stripped username and password
47 */
48public final class ShowStatusReportAction extends JosmAction {
49
50 /**
51 * Constructs a new {@code ShowStatusReportAction}
52 */
53 public ShowStatusReportAction() {
54 super(
55 tr("Show Status Report"),
56 "clock",
57 tr("Show status report with useful information that can be attached to bugs"),
58 Shortcut.registerShortcut("help:showstatusreport", tr("Help: {0}",
59 tr("Show Status Report")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false);
60
61 putValue("help", ht("/Action/ShowStatusReport"));
62 putValue("toolbar", "help/showstatusreport");
63 Main.toolbar.register(this);
64 }
65
66 private static boolean isRunningJavaWebStart() {
67 try {
68 // See http://stackoverflow.com/a/16200769/2257172
69 return Class.forName("javax.jnlp.ServiceManager") != null;
70 } catch (ClassNotFoundException e) {
71 return false;
72 }
73 }
74
75 /**
76 * Replies the report header (software and system info)
77 * @return The report header (software and system info)
78 */
79 public static String getReportHeader() {
80 StringBuilder text = new StringBuilder(256);
81 String runtimeVersion = System.getProperty("java.runtime.version");
82 text.append(Version.getInstance().getReleaseAttributes())
83 .append("\nIdentification: ").append(Version.getInstance().getAgentString())
84 .append("\nMemory Usage: ")
85 .append(Runtime.getRuntime().totalMemory()/1024/1024)
86 .append(" MB / ")
87 .append(Runtime.getRuntime().maxMemory()/1024/1024)
88 .append(" MB (")
89 .append(Runtime.getRuntime().freeMemory()/1024/1024)
90 .append(" MB allocated, but free)\nJava version: ")
91 .append(runtimeVersion != null ? runtimeVersion : System.getProperty("java.version")).append(", ")
92 .append(System.getProperty("java.vendor")).append(", ")
93 .append(System.getProperty("java.vm.name")).append('\n');
94 if (Main.platform.getClass() == PlatformHookUnixoid.class) {
95 // Add Java package details
96 String packageDetails = ((PlatformHookUnixoid) Main.platform).getJavaPackageDetails();
97 if (packageDetails != null) {
98 text.append("Java package: ")
99 .append(packageDetails)
100 .append('\n');
101 }
102 // Add WebStart package details if run from JNLP
103 if (isRunningJavaWebStart()) {
104 String webStartDetails = ((PlatformHookUnixoid) Main.platform).getWebStartPackageDetails();
105 if (webStartDetails != null) {
106 text.append("WebStart package: ")
107 .append(webStartDetails)
108 .append('\n');
109 }
110 }
111 }
112 try {
113 // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance)
114 List<String> vmArguments = new ArrayList<>(ManagementFactory.getRuntimeMXBean().getInputArguments());
115 for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext();) {
116 String value = it.next();
117 if (value.contains("=")) {
118 String[] param = value.split("=");
119 // Hide some parameters for privacy concerns
120 if (param[0].toLowerCase(Locale.ENGLISH).startsWith("-dproxy")) {
121 it.set(param[0]+"=xxx");
122 } else {
123 // Replace some paths for readability and privacy concerns
124 String val = paramCleanup(param[1]);
125 if (!val.equals(param[1])) {
126 it.set(param[0] + '=' + val);
127 }
128 }
129 } else if (value.startsWith("-X")) {
130 // Remove arguments like -Xbootclasspath/a, -Xverify:remote, that can be very long and unhelpful
131 it.remove();
132 }
133 }
134 if (!vmArguments.isEmpty()) {
135 text.append("VM arguments: ").append(vmArguments.toString().replace("\\\\", "\\")).append('\n');
136 }
137 } catch (SecurityException e) {
138 Main.trace(e);
139 }
140 List<String> commandLineArgs = Main.getCommandLineArgs();
141 if (!commandLineArgs.isEmpty()) {
142 text.append("Program arguments: ").append(Arrays.toString(paramCleanup(commandLineArgs).toArray())).append('\n');
143 }
144 if (Main.main != null) {
145 DataSet dataset = Main.getLayerManager().getEditDataSet();
146 if (dataset != null) {
147 String result = DatasetConsistencyTest.runTests(dataset);
148 if (result.isEmpty()) {
149 text.append("Dataset consistency test: No problems found\n");
150 } else {
151 text.append("\nDataset consistency test:\n").append(result).append('\n');
152 }
153 }
154 }
155 text.append('\n').append(PluginHandler.getBugReportText()).append('\n');
156
157 appendCollection(text, "Tagging presets", getCustomUrls(TaggingPresetPreference.PresetPrefHelper.INSTANCE));
158 appendCollection(text, "Map paint styles", getCustomUrls(MapPaintPreference.MapPaintPrefHelper.INSTANCE));
159 appendCollection(text, "Validator rules", getCustomUrls(ValidatorTagCheckerRulesPreference.RulePrefHelper.INSTANCE));
160 appendCollection(text, "Last errors/warnings", Main.getLastErrorAndWarnings());
161
162 String osmApi = OsmApi.getOsmApi().getServerUrl();
163 if (!OsmApi.DEFAULT_API_URL.equals(osmApi.trim())) {
164 text.append("OSM API: ").append(osmApi).append("\n\n");
165 }
166
167 return text.toString();
168 }
169
170 private static Collection<String> getCustomUrls(SourceEditor.SourcePrefHelper helper) {
171 Set<String> set = new TreeSet<>();
172 for (SourceEntry entry : helper.get()) {
173 set.add(entry.url);
174 }
175 for (ExtendedSourceEntry def : helper.getDefault()) {
176 set.remove(def.url);
177 }
178 return set;
179 }
180
181 private static List<String> paramCleanup(Collection<String> params) {
182 List<String> result = new ArrayList<>(params.size());
183 for (String param : params) {
184 result.add(paramCleanup(param));
185 }
186 return result;
187 }
188
189 /**
190 * Shortens and removes private informations from a parameter used for status report.
191 * @param param parameter to cleanup
192 * @return shortened/anonymized parameter
193 */
194 private static String paramCleanup(String param) {
195 final String envJavaHome = System.getenv("JAVA_HOME");
196 final String envJavaHomeAlt = Main.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}";
197 final String propJavaHome = System.getProperty("java.home");
198 final String propJavaHomeAlt = "<java.home>";
199 final String prefDir = Main.pref.getPreferencesDirectory().toString();
200 final String prefDirAlt = "<josm.pref>";
201 final String userDataDir = Main.pref.getUserDataDirectory().toString();
202 final String userDataDirAlt = "<josm.userdata>";
203 final String userCacheDir = Main.pref.getCacheDirectory().toString();
204 final String userCacheDirAlt = "<josm.cache>";
205 final String userHomeDir = System.getProperty("user.home");
206 final String userHomeDirAlt = Main.isPlatformWindows() ? "%UserProfile%" : "${HOME}";
207 final String userName = System.getProperty("user.name");
208 final String userNameAlt = "<user.name>";
209
210 String val = param;
211 val = paramReplace(val, envJavaHome, envJavaHomeAlt);
212 val = paramReplace(val, envJavaHome, envJavaHomeAlt);
213 val = paramReplace(val, propJavaHome, propJavaHomeAlt);
214 val = paramReplace(val, prefDir, prefDirAlt);
215 val = paramReplace(val, userDataDir, userDataDirAlt);
216 val = paramReplace(val, userCacheDir, userCacheDirAlt);
217 val = paramReplace(val, userHomeDir, userHomeDirAlt);
218 val = paramReplace(val, userName, userNameAlt);
219 return val;
220 }
221
222 private static String paramReplace(String str, String target, String replacement) {
223 return target == null ? str : str.replace(target, replacement);
224 }
225
226 private static <T> void appendCollection(StringBuilder text, String label, Collection<T> col) {
227 if (!col.isEmpty()) {
228 text.append(label+":\n");
229 for (T o : col) {
230 text.append("- ").append(paramCleanup(o.toString())).append('\n');
231 }
232 text.append('\n');
233 }
234 }
235
236 @Override
237 public void actionPerformed(ActionEvent e) {
238 StringBuilder text = new StringBuilder();
239 String reportHeader = getReportHeader();
240 text.append(reportHeader);
241 try {
242 Map<String, Setting<?>> settings = Main.pref.getAllSettings();
243 Set<String> keys = new HashSet<>(settings.keySet());
244 for (String key : keys) {
245 // Remove sensitive information from status report
246 if (key.startsWith("marker.show") || key.contains("username") || key.contains("password") || key.contains("access-token")) {
247 settings.remove(key);
248 }
249 }
250 for (Entry<String, Setting<?>> entry : settings.entrySet()) {
251 text.append(paramCleanup(entry.getKey()))
252 .append('=')
253 .append(paramCleanup(entry.getValue().getValue().toString())).append('\n');
254 }
255 } catch (Exception x) {
256 Main.error(x);
257 }
258
259 DebugTextDisplay ta = new DebugTextDisplay(text.toString());
260
261 ExtendedDialog ed = new ExtendedDialog(Main.parent,
262 tr("Status Report"),
263 new String[] {tr("Copy to clipboard and close"), tr("Report bug"), tr("Close") });
264 ed.setButtonIcons(new String[] {"copy", "bug", "cancel" });
265 ed.setContent(ta, false);
266 ed.setMinimumSize(new Dimension(380, 200));
267 ed.setPreferredSize(new Dimension(700, Main.parent.getHeight()-50));
268
269 switch (ed.showDialog().getValue()) {
270 case 1: ta.copyToClippboard(); break;
271 case 2: BugReportSender.reportBug(reportHeader); break;
272 default: // Do nothing
273 }
274 }
275}
Note: See TracBrowser for help on using the repository browser.