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.TreeSet;
|
---|
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.SourceEditor.ExtendedSourceEntry;
|
---|
31 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
32 | import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
|
---|
33 | import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
|
---|
34 | import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference;
|
---|
35 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
36 | import org.openstreetmap.josm.tools.PlatformHookUnixoid;
|
---|
37 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
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();
|
---|
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 | final String envJavaHome = System.getenv("JAVA_HOME");
|
---|
113 | final String envJavaHomeAlt = Main.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}";
|
---|
114 | final String propJavaHome = System.getProperty("java.home");
|
---|
115 | final String propJavaHomeAlt = "<java.home>";
|
---|
116 | final String prefDir = Main.pref.getPreferencesDirectory().toString();
|
---|
117 | final String prefDirAlt = "<josm.pref>";
|
---|
118 | final String userDataDir = Main.pref.getUserDataDirectory().toString();
|
---|
119 | final String userDataDirAlt = "<josm.userdata>";
|
---|
120 | final String userCacheDir = Main.pref.getCacheDirectory().toString();
|
---|
121 | final String userCacheDirAlt = "<josm.cache>";
|
---|
122 | final String userHomeDir = System.getProperty("user.home");
|
---|
123 | final String userHomeDirAlt = Main.isPlatformWindows() ? "%UserProfile%" : "${HOME}";
|
---|
124 |
|
---|
125 | // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance)
|
---|
126 | List<String> vmArguments = new ArrayList<>(ManagementFactory.getRuntimeMXBean().getInputArguments());
|
---|
127 | for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext();) {
|
---|
128 | String value = it.next();
|
---|
129 | if (value.contains("=")) {
|
---|
130 | String[] param = value.split("=");
|
---|
131 | // Hide some parameters for privacy concerns
|
---|
132 | if (param[0].toLowerCase(Locale.ENGLISH).startsWith("-dproxy")) {
|
---|
133 | it.set(param[0]+"=xxx");
|
---|
134 | } else {
|
---|
135 | // Replace some paths for readability and privacy concerns
|
---|
136 | String val = param[1];
|
---|
137 | val = paramReplace(val, envJavaHome, envJavaHomeAlt);
|
---|
138 | val = paramReplace(val, envJavaHome, envJavaHomeAlt);
|
---|
139 | val = paramReplace(val, propJavaHome, propJavaHomeAlt);
|
---|
140 | val = paramReplace(val, prefDir, prefDirAlt);
|
---|
141 | val = paramReplace(val, userDataDir, userDataDirAlt);
|
---|
142 | val = paramReplace(val, userCacheDir, userCacheDirAlt);
|
---|
143 | val = paramReplace(val, userHomeDir, userHomeDirAlt);
|
---|
144 | if (!val.equals(param[1])) {
|
---|
145 | it.set(param[0] + '=' + val);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | } else if (value.startsWith("-X")) {
|
---|
149 | // Remove arguments like -Xbootclasspath/a, -Xverify:remote, that can be very long and unhelpful
|
---|
150 | it.remove();
|
---|
151 | }
|
---|
152 | }
|
---|
153 | if (!vmArguments.isEmpty()) {
|
---|
154 | text.append("VM arguments: ").append(vmArguments.toString().replace("\\\\", "\\")).append('\n');
|
---|
155 | }
|
---|
156 | } catch (SecurityException e) {
|
---|
157 | // Ignore exception
|
---|
158 | if (Main.isTraceEnabled()) {
|
---|
159 | Main.trace(e.getMessage());
|
---|
160 | }
|
---|
161 | }
|
---|
162 | List<String> commandLineArgs = Main.getCommandLineArgs();
|
---|
163 | if (!commandLineArgs.isEmpty()) {
|
---|
164 | text.append("Program arguments: ").append(Arrays.toString(commandLineArgs.toArray())).append('\n');
|
---|
165 | }
|
---|
166 | if (Main.main != null) {
|
---|
167 | DataSet dataset = Main.main.getCurrentDataSet();
|
---|
168 | if (dataset != null) {
|
---|
169 | String result = DatasetConsistencyTest.runTests(dataset);
|
---|
170 | if (result.isEmpty()) {
|
---|
171 | text.append("Dataset consistency test: No problems found\n");
|
---|
172 | } else {
|
---|
173 | text.append("\nDataset consistency test:\n").append(result).append('\n');
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 | text.append('\n').append(PluginHandler.getBugReportText()).append('\n');
|
---|
178 |
|
---|
179 | appendCollection(text, "Tagging presets", getCustomUrls(TaggingPresetPreference.PresetPrefHelper.INSTANCE));
|
---|
180 | appendCollection(text, "Map paint styles", getCustomUrls(MapPaintPreference.MapPaintPrefHelper.INSTANCE));
|
---|
181 | appendCollection(text, "Validator rules", getCustomUrls(ValidatorTagCheckerRulesPreference.RulePrefHelper.INSTANCE));
|
---|
182 | appendCollection(text, "Last errors/warnings", Main.getLastErrorAndWarnings());
|
---|
183 |
|
---|
184 | return text.toString();
|
---|
185 | }
|
---|
186 |
|
---|
187 | protected static Collection<String> getCustomUrls(SourceEditor.SourcePrefHelper helper) {
|
---|
188 | Set<String> set = new TreeSet<>();
|
---|
189 | for (SourceEntry entry : helper.get()) {
|
---|
190 | set.add(entry.url);
|
---|
191 | }
|
---|
192 | for (ExtendedSourceEntry def : helper.getDefault()) {
|
---|
193 | set.remove(def.url);
|
---|
194 | }
|
---|
195 | return set;
|
---|
196 | }
|
---|
197 |
|
---|
198 | private static String paramReplace(String str, String target, String replacement) {
|
---|
199 | if (target == null) return str;
|
---|
200 | return str.replace(target, replacement);
|
---|
201 | }
|
---|
202 |
|
---|
203 | protected static <T> void appendCollection(StringBuilder text, String label, Collection<T> col) {
|
---|
204 | if (!col.isEmpty()) {
|
---|
205 | text.append(label+":\n");
|
---|
206 | for (T o : col) {
|
---|
207 | text.append("- ").append(o.toString()).append('\n');
|
---|
208 | }
|
---|
209 | text.append('\n');
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | @Override
|
---|
214 | public void actionPerformed(ActionEvent e) {
|
---|
215 | StringBuilder text = new StringBuilder();
|
---|
216 | String reportHeader = getReportHeader();
|
---|
217 | text.append(reportHeader);
|
---|
218 | try {
|
---|
219 | Map<String, Setting<?>> settings = Main.pref.getAllSettings();
|
---|
220 | Set<String> keys = new HashSet<>(settings.keySet());
|
---|
221 | for (String key : keys) {
|
---|
222 | // Remove sensitive information from status report
|
---|
223 | if (key.startsWith("marker.show") || key.contains("username") || key.contains("password") || key.contains("access-token")) {
|
---|
224 | settings.remove(key);
|
---|
225 | }
|
---|
226 | }
|
---|
227 | for (Entry<String, Setting<?>> entry : settings.entrySet()) {
|
---|
228 | text.append(entry.getKey()).append('=').append(entry.getValue().getValue()).append('\n');
|
---|
229 | }
|
---|
230 | } catch (Exception x) {
|
---|
231 | Main.error(x);
|
---|
232 | }
|
---|
233 |
|
---|
234 | DebugTextDisplay ta = new DebugTextDisplay(text.toString());
|
---|
235 |
|
---|
236 | ExtendedDialog ed = new ExtendedDialog(Main.parent,
|
---|
237 | tr("Status Report"),
|
---|
238 | new String[] {tr("Copy to clipboard and close"), tr("Report bug"), tr("Close") });
|
---|
239 | ed.setButtonIcons(new String[] {"copy", "bug", "cancel" });
|
---|
240 | ed.setContent(ta, false);
|
---|
241 | ed.setMinimumSize(new Dimension(380, 200));
|
---|
242 | ed.setPreferredSize(new Dimension(700, Main.parent.getHeight()-50));
|
---|
243 |
|
---|
244 | switch (ed.showDialog().getValue()) {
|
---|
245 | case 1: ta.copyToClippboard(); break;
|
---|
246 | case 2: BugReportSender.reportBug(reportHeader); break;
|
---|
247 | }
|
---|
248 | }
|
---|
249 | }
|
---|