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

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

findbugs - SF_SWITCH_NO_DEFAULT + various sonar fixes

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