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

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

fix #12779 - anonymize user name

  • 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 // Ignore exception
138 if (Main.isTraceEnabled()) {
139 Main.trace(e.getMessage());
140 }
141 }
142 List<String> commandLineArgs = Main.getCommandLineArgs();
143 if (!commandLineArgs.isEmpty()) {
144 text.append("Program arguments: ").append(Arrays.toString(paramCleanup(commandLineArgs).toArray())).append('\n');
145 }
146 if (Main.main != null) {
147 DataSet dataset = Main.main.getCurrentDataSet();
148 if (dataset != null) {
149 String result = DatasetConsistencyTest.runTests(dataset);
150 if (result.isEmpty()) {
151 text.append("Dataset consistency test: No problems found\n");
152 } else {
153 text.append("\nDataset consistency test:\n").append(result).append('\n');
154 }
155 }
156 }
157 text.append('\n').append(PluginHandler.getBugReportText()).append('\n');
158
159 appendCollection(text, "Tagging presets", getCustomUrls(TaggingPresetPreference.PresetPrefHelper.INSTANCE));
160 appendCollection(text, "Map paint styles", getCustomUrls(MapPaintPreference.MapPaintPrefHelper.INSTANCE));
161 appendCollection(text, "Validator rules", getCustomUrls(ValidatorTagCheckerRulesPreference.RulePrefHelper.INSTANCE));
162 appendCollection(text, "Last errors/warnings", Main.getLastErrorAndWarnings());
163
164 return text.toString();
165 }
166
167 protected static Collection<String> getCustomUrls(SourceEditor.SourcePrefHelper helper) {
168 Set<String> set = new TreeSet<>();
169 for (SourceEntry entry : helper.get()) {
170 set.add(entry.url);
171 }
172 for (ExtendedSourceEntry def : helper.getDefault()) {
173 set.remove(def.url);
174 }
175 return set;
176 }
177
178 private static List<String> paramCleanup(Collection<String> params) {
179 List<String> result = new ArrayList<>(params.size());
180 for (String param : params) {
181 result.add(paramCleanup(param));
182 }
183 return result;
184 }
185
186 /**
187 * Shortens and removes private informations from a parameter used for status report.
188 * @param param parameter to cleanup
189 * @return shortened/anonymized parameter
190 */
191 private static String paramCleanup(String param) {
192 final String envJavaHome = System.getenv("JAVA_HOME");
193 final String envJavaHomeAlt = Main.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}";
194 final String propJavaHome = System.getProperty("java.home");
195 final String propJavaHomeAlt = "<java.home>";
196 final String prefDir = Main.pref.getPreferencesDirectory().toString();
197 final String prefDirAlt = "<josm.pref>";
198 final String userDataDir = Main.pref.getUserDataDirectory().toString();
199 final String userDataDirAlt = "<josm.userdata>";
200 final String userCacheDir = Main.pref.getCacheDirectory().toString();
201 final String userCacheDirAlt = "<josm.cache>";
202 final String userHomeDir = System.getProperty("user.home");
203 final String userHomeDirAlt = Main.isPlatformWindows() ? "%UserProfile%" : "${HOME}";
204 final String userName = System.getProperty("user.name");
205 final String userNameAlt = "<user.name>";
206
207 String val = param;
208 val = paramReplace(val, envJavaHome, envJavaHomeAlt);
209 val = paramReplace(val, envJavaHome, envJavaHomeAlt);
210 val = paramReplace(val, propJavaHome, propJavaHomeAlt);
211 val = paramReplace(val, prefDir, prefDirAlt);
212 val = paramReplace(val, userDataDir, userDataDirAlt);
213 val = paramReplace(val, userCacheDir, userCacheDirAlt);
214 val = paramReplace(val, userHomeDir, userHomeDirAlt);
215 val = paramReplace(val, userName, userNameAlt);
216 return val;
217 }
218
219 private static String paramReplace(String str, String target, String replacement) {
220 return target == null ? str : str.replace(target, replacement);
221 }
222
223 protected static <T> void appendCollection(StringBuilder text, String label, Collection<T> col) {
224 if (!col.isEmpty()) {
225 text.append(label+":\n");
226 for (T o : col) {
227 text.append("- ").append(paramCleanup(o.toString())).append('\n');
228 }
229 text.append('\n');
230 }
231 }
232
233 @Override
234 public void actionPerformed(ActionEvent e) {
235 StringBuilder text = new StringBuilder();
236 String reportHeader = getReportHeader();
237 text.append(reportHeader);
238 try {
239 Map<String, Setting<?>> settings = Main.pref.getAllSettings();
240 Set<String> keys = new HashSet<>(settings.keySet());
241 for (String key : keys) {
242 // Remove sensitive information from status report
243 if (key.startsWith("marker.show") || key.contains("username") || key.contains("password") || key.contains("access-token")) {
244 settings.remove(key);
245 }
246 }
247 for (Entry<String, Setting<?>> entry : settings.entrySet()) {
248 text.append(paramCleanup(entry.getKey()))
249 .append('=')
250 .append(paramCleanup(entry.getValue().getValue().toString())).append('\n');
251 }
252 } catch (Exception x) {
253 Main.error(x);
254 }
255
256 DebugTextDisplay ta = new DebugTextDisplay(text.toString());
257
258 ExtendedDialog ed = new ExtendedDialog(Main.parent,
259 tr("Status Report"),
260 new String[] {tr("Copy to clipboard and close"), tr("Report bug"), tr("Close") });
261 ed.setButtonIcons(new String[] {"copy", "bug", "cancel" });
262 ed.setContent(ta, false);
263 ed.setMinimumSize(new Dimension(380, 200));
264 ed.setPreferredSize(new Dimension(700, Main.parent.getHeight()-50));
265
266 switch (ed.showDialog().getValue()) {
267 case 1: ta.copyToClippboard(); break;
268 case 2: BugReportSender.reportBug(reportHeader); break;
269 }
270 }
271}
Note: See TracBrowser for help on using the repository browser.