1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.lang.management.ManagementFactory;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.HashSet;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.ListIterator;
|
---|
17 | import java.util.Locale;
|
---|
18 | import java.util.Map;
|
---|
19 | import java.util.Map.Entry;
|
---|
20 | import java.util.Set;
|
---|
21 | import java.util.stream.Collectors;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.data.Version;
|
---|
25 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
26 | import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
|
---|
27 | import org.openstreetmap.josm.data.preferences.Setting;
|
---|
28 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
29 | import org.openstreetmap.josm.gui.preferences.SourceEditor;
|
---|
30 | import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
|
---|
31 | import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
|
---|
32 | import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference;
|
---|
33 | import org.openstreetmap.josm.io.OsmApi;
|
---|
34 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
35 | import org.openstreetmap.josm.tools.PlatformHookUnixoid;
|
---|
36 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
37 | import org.openstreetmap.josm.tools.Utils;
|
---|
38 | import org.openstreetmap.josm.tools.bugreport.BugReportSender;
|
---|
39 | import 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 | */
|
---|
47 | public 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(256);
|
---|
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 | // Add Gnome Atk wrapper details if found
|
---|
111 | String atkWrapperDetails = ((PlatformHookUnixoid) Main.platform).getAtkWrapperPackageDetails();
|
---|
112 | if (atkWrapperDetails != null) {
|
---|
113 | text.append("Java ATK Wrapper package: ")
|
---|
114 | .append(atkWrapperDetails)
|
---|
115 | .append('\n');
|
---|
116 | }
|
---|
117 | }
|
---|
118 | try {
|
---|
119 | // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance)
|
---|
120 | List<String> vmArguments = new ArrayList<>(ManagementFactory.getRuntimeMXBean().getInputArguments());
|
---|
121 | for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext();) {
|
---|
122 | String value = it.next();
|
---|
123 | if (value.contains("=")) {
|
---|
124 | String[] param = value.split("=");
|
---|
125 | // Hide some parameters for privacy concerns
|
---|
126 | if (param[0].toLowerCase(Locale.ENGLISH).startsWith("-dproxy")) {
|
---|
127 | it.set(param[0]+"=xxx");
|
---|
128 | } else {
|
---|
129 | // Replace some paths for readability and privacy concerns
|
---|
130 | String val = paramCleanup(param[1]);
|
---|
131 | if (!val.equals(param[1])) {
|
---|
132 | it.set(param[0] + '=' + val);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | } else if (value.startsWith("-X")) {
|
---|
136 | // Remove arguments like -Xbootclasspath/a, -Xverify:remote, that can be very long and unhelpful
|
---|
137 | it.remove();
|
---|
138 | }
|
---|
139 | }
|
---|
140 | if (!vmArguments.isEmpty()) {
|
---|
141 | text.append("VM arguments: ").append(vmArguments.toString().replace("\\\\", "\\")).append('\n');
|
---|
142 | }
|
---|
143 | } catch (SecurityException e) {
|
---|
144 | Main.trace(e);
|
---|
145 | }
|
---|
146 | List<String> commandLineArgs = Main.getCommandLineArgs();
|
---|
147 | if (!commandLineArgs.isEmpty()) {
|
---|
148 | text.append("Program arguments: ").append(Arrays.toString(paramCleanup(commandLineArgs).toArray())).append('\n');
|
---|
149 | }
|
---|
150 | if (Main.main != null) {
|
---|
151 | DataSet dataset = Main.getLayerManager().getEditDataSet();
|
---|
152 | if (dataset != null) {
|
---|
153 | String result = DatasetConsistencyTest.runTests(dataset);
|
---|
154 | if (result.isEmpty()) {
|
---|
155 | text.append("Dataset consistency test: No problems found\n");
|
---|
156 | } else {
|
---|
157 | text.append("\nDataset consistency test:\n").append(result).append('\n');
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 | text.append("\n");
|
---|
162 | appendCollection(text, "Plugins", Utils.transform(PluginHandler.getBugReportInformation(), i -> "+ " + i));
|
---|
163 | appendCollection(text, "Tagging presets", getCustomUrls(TaggingPresetPreference.PresetPrefHelper.INSTANCE));
|
---|
164 | appendCollection(text, "Map paint styles", getCustomUrls(MapPaintPreference.MapPaintPrefHelper.INSTANCE));
|
---|
165 | appendCollection(text, "Validator rules", getCustomUrls(ValidatorTagCheckerRulesPreference.RulePrefHelper.INSTANCE));
|
---|
166 | appendCollection(text, "Last errors/warnings", Utils.transform(Main.getLastErrorAndWarnings(), i -> "- " + i));
|
---|
167 |
|
---|
168 | String osmApi = OsmApi.getOsmApi().getServerUrl();
|
---|
169 | if (!OsmApi.DEFAULT_API_URL.equals(osmApi.trim())) {
|
---|
170 | text.append("OSM API: ").append(osmApi).append("\n\n");
|
---|
171 | }
|
---|
172 |
|
---|
173 | return text.toString();
|
---|
174 | }
|
---|
175 |
|
---|
176 | private static Collection<String> getCustomUrls(SourceEditor.SourcePrefHelper helper) {
|
---|
177 | final Set<String> defaultUrls = helper.getDefault().stream()
|
---|
178 | .map(i -> i.url)
|
---|
179 | .collect(Collectors.toSet());
|
---|
180 | return helper.get().stream()
|
---|
181 | .filter(i -> !defaultUrls.contains(i.url))
|
---|
182 | .map(i -> (i.active ? "+ " : "- ") + i.url)
|
---|
183 | .collect(Collectors.toList());
|
---|
184 | }
|
---|
185 |
|
---|
186 | private static List<String> paramCleanup(Collection<String> params) {
|
---|
187 | List<String> result = new ArrayList<>(params.size());
|
---|
188 | for (String param : params) {
|
---|
189 | result.add(paramCleanup(param));
|
---|
190 | }
|
---|
191 | return result;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Shortens and removes private informations from a parameter used for status report.
|
---|
196 | * @param param parameter to cleanup
|
---|
197 | * @return shortened/anonymized parameter
|
---|
198 | */
|
---|
199 | private static String paramCleanup(String param) {
|
---|
200 | final String envJavaHome = System.getenv("JAVA_HOME");
|
---|
201 | final String envJavaHomeAlt = Main.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}";
|
---|
202 | final String propJavaHome = System.getProperty("java.home");
|
---|
203 | final String propJavaHomeAlt = "<java.home>";
|
---|
204 | final String prefDir = Main.pref.getPreferencesDirectory().toString();
|
---|
205 | final String prefDirAlt = "<josm.pref>";
|
---|
206 | final String userDataDir = Main.pref.getUserDataDirectory().toString();
|
---|
207 | final String userDataDirAlt = "<josm.userdata>";
|
---|
208 | final String userCacheDir = Main.pref.getCacheDirectory().toString();
|
---|
209 | final String userCacheDirAlt = "<josm.cache>";
|
---|
210 | final String userHomeDir = System.getProperty("user.home");
|
---|
211 | final String userHomeDirAlt = Main.isPlatformWindows() ? "%UserProfile%" : "${HOME}";
|
---|
212 | final String userName = System.getProperty("user.name");
|
---|
213 | final String userNameAlt = "<user.name>";
|
---|
214 |
|
---|
215 | String val = param;
|
---|
216 | val = paramReplace(val, envJavaHome, envJavaHomeAlt);
|
---|
217 | val = paramReplace(val, envJavaHome, envJavaHomeAlt);
|
---|
218 | val = paramReplace(val, propJavaHome, propJavaHomeAlt);
|
---|
219 | val = paramReplace(val, prefDir, prefDirAlt);
|
---|
220 | val = paramReplace(val, userDataDir, userDataDirAlt);
|
---|
221 | val = paramReplace(val, userCacheDir, userCacheDirAlt);
|
---|
222 | val = paramReplace(val, userHomeDir, userHomeDirAlt);
|
---|
223 | val = paramReplace(val, userName, userNameAlt);
|
---|
224 | return val;
|
---|
225 | }
|
---|
226 |
|
---|
227 | private static String paramReplace(String str, String target, String replacement) {
|
---|
228 | return target == null ? str : str.replace(target, replacement);
|
---|
229 | }
|
---|
230 |
|
---|
231 | private static void appendCollection(StringBuilder text, String label, Collection<String> col) {
|
---|
232 | if (!col.isEmpty()) {
|
---|
233 | text.append(label).append(":\n");
|
---|
234 | for (String o : col) {
|
---|
235 | text.append(paramCleanup(o)).append('\n');
|
---|
236 | }
|
---|
237 | text.append('\n');
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | @Override
|
---|
242 | public void actionPerformed(ActionEvent e) {
|
---|
243 | StringBuilder text = new StringBuilder();
|
---|
244 | String reportHeader = getReportHeader();
|
---|
245 | text.append(reportHeader);
|
---|
246 | try {
|
---|
247 | Map<String, Setting<?>> settings = Main.pref.getAllSettings();
|
---|
248 | Set<String> keys = new HashSet<>(settings.keySet());
|
---|
249 | for (String key : keys) {
|
---|
250 | // Remove sensitive information from status report
|
---|
251 | if (key.startsWith("marker.show") || key.contains("username") || key.contains("password") || key.contains("access-token")) {
|
---|
252 | settings.remove(key);
|
---|
253 | }
|
---|
254 | }
|
---|
255 | for (Entry<String, Setting<?>> entry : settings.entrySet()) {
|
---|
256 | text.append(paramCleanup(entry.getKey()))
|
---|
257 | .append('=')
|
---|
258 | .append(paramCleanup(entry.getValue().getValue().toString())).append('\n');
|
---|
259 | }
|
---|
260 | } catch (Exception x) {
|
---|
261 | Main.error(x);
|
---|
262 | }
|
---|
263 |
|
---|
264 | DebugTextDisplay ta = new DebugTextDisplay(text.toString());
|
---|
265 |
|
---|
266 | ExtendedDialog ed = new ExtendedDialog(Main.parent,
|
---|
267 | tr("Status Report"),
|
---|
268 | new String[] {tr("Copy to clipboard and close"), tr("Report bug"), tr("Close") });
|
---|
269 | ed.setButtonIcons(new String[] {"copy", "bug", "cancel" });
|
---|
270 | ed.setContent(ta, false);
|
---|
271 | ed.setMinimumSize(new Dimension(380, 200));
|
---|
272 | ed.setPreferredSize(new Dimension(700, Main.parent.getHeight()-50));
|
---|
273 |
|
---|
274 | switch (ed.showDialog().getValue()) {
|
---|
275 | case 1: ta.copyToClippboard(); break;
|
---|
276 | case 2: BugReportSender.reportBug(reportHeader); break;
|
---|
277 | default: // Do nothing
|
---|
278 | }
|
---|
279 | }
|
---|
280 | }
|
---|