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

Last change on this file since 13432 was 12881, checked in by bastiK, 7 years ago

see #15229 - move remaining classes to spi.preferences package, to make it self-contained

  • extract event listener classes from Preferences (duplicated, for smooth transition)
  • move *Setting classes
  • Property svn:eol-style set to native
File size: 13.9 KB
RevLine 
[1417]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
[2500]4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
[1417]5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Dimension;
[10929]8import java.awt.DisplayMode;
9import java.awt.GraphicsEnvironment;
[1417]10import java.awt.event.ActionEvent;
11import java.awt.event.KeyEvent;
[5831]12import java.lang.management.ManagementFactory;
[5840]13import java.util.ArrayList;
[5831]14import java.util.Arrays;
[7420]15import java.util.Collection;
[4635]16import java.util.HashSet;
[5831]17import java.util.List;
[5840]18import java.util.ListIterator;
[8404]19import java.util.Locale;
[4635]20import java.util.Map;
21import java.util.Map.Entry;
22import java.util.Set;
[10696]23import java.util.stream.Collectors;
[1417]24
25import org.openstreetmap.josm.Main;
[2358]26import org.openstreetmap.josm.data.Version;
[2500]27import org.openstreetmap.josm.data.osm.DataSet;
28import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
[12881]29import org.openstreetmap.josm.spi.preferences.Setting;
[12649]30import org.openstreetmap.josm.data.preferences.sources.MapPaintPrefHelper;
31import org.openstreetmap.josm.data.preferences.sources.PresetPrefHelper;
[12846]32import org.openstreetmap.josm.data.preferences.sources.SourcePrefHelper;
[12649]33import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
[1417]34import org.openstreetmap.josm.gui.ExtendedDialog;
[11650]35import org.openstreetmap.josm.gui.MainApplication;
[12649]36import org.openstreetmap.josm.gui.bugreport.DebugTextDisplay;
[10929]37import org.openstreetmap.josm.gui.util.GuiHelper;
[10226]38import org.openstreetmap.josm.io.OsmApi;
[1417]39import org.openstreetmap.josm.plugins.PluginHandler;
[12855]40import org.openstreetmap.josm.spi.preferences.Config;
[12620]41import org.openstreetmap.josm.tools.Logging;
[6103]42import org.openstreetmap.josm.tools.PlatformHookUnixoid;
[1417]43import org.openstreetmap.josm.tools.Shortcut;
[10696]44import org.openstreetmap.josm.tools.Utils;
[10055]45import org.openstreetmap.josm.tools.bugreport.BugReportSender;
[1417]46
47/**
[12581]48 * Opens a dialog with useful status information like version numbers for Java, JOSM and plugins
49 * Also includes preferences with stripped username and password.
50 *
[1417]51 * @author xeen
52 */
53public final class ShowStatusReportAction extends JosmAction {
[6069]54
[5840]55 /**
56 * Constructs a new {@code ShowStatusReportAction}
57 */
[1417]58 public ShowStatusReportAction() {
59 super(
60 tr("Show Status Report"),
61 "clock",
62 tr("Show status report with useful information that can be attached to bugs"),
63 Shortcut.registerShortcut("help:showstatusreport", tr("Help: {0}",
[4973]64 tr("Show Status Report")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false);
[1417]65
[2323]66 putValue("help", ht("/Action/ShowStatusReport"));
[4139]67 putValue("toolbar", "help/showstatusreport");
[12637]68 MainApplication.getToolbar().register(this);
[1417]69 }
70
[10058]71 private static boolean isRunningJavaWebStart() {
72 try {
73 // See http://stackoverflow.com/a/16200769/2257172
74 return Class.forName("javax.jnlp.ServiceManager") != null;
75 } catch (ClassNotFoundException e) {
76 return false;
77 }
78 }
79
[5873]80 /**
81 * Replies the report header (software and system info)
82 * @return The report header (software and system info)
83 */
[6850]84 public static String getReportHeader() {
[10242]85 StringBuilder text = new StringBuilder(256);
[9332]86 String runtimeVersion = System.getProperty("java.runtime.version");
[8379]87 text.append(Version.getInstance().getReleaseAttributes())
[12217]88 .append("\nIdentification: ").append(Version.getInstance().getAgentString());
89 String buildNumber = Main.platform.getOSBuildNumber();
90 if (!buildNumber.isEmpty()) {
91 text.append("\nOS Build number: ").append(buildNumber);
92 }
93 text.append("\nMemory Usage: ")
[8379]94 .append(Runtime.getRuntime().totalMemory()/1024/1024)
95 .append(" MB / ")
96 .append(Runtime.getRuntime().maxMemory()/1024/1024)
97 .append(" MB (")
98 .append(Runtime.getRuntime().freeMemory()/1024/1024)
[8390]99 .append(" MB allocated, but free)\nJava version: ")
[9332]100 .append(runtimeVersion != null ? runtimeVersion : System.getProperty("java.version")).append(", ")
[8390]101 .append(System.getProperty("java.vendor")).append(", ")
[10936]102 .append(System.getProperty("java.vm.name"))
103 .append("\nScreen: ");
[10932]104 if (!GraphicsEnvironment.isHeadless()) {
[10933]105 text.append(Arrays.stream(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()).map(gd -> {
106 StringBuilder b = new StringBuilder(gd.getIDstring());
107 DisplayMode dm = gd.getDisplayMode();
108 if (dm != null) {
109 b.append(' ').append(dm.getWidth()).append('x').append(dm.getHeight());
110 }
111 return b.toString();
112 }).collect(Collectors.joining(", ")));
[10929]113 }
114 Dimension maxScreenSize = GuiHelper.getMaximumScreenSize();
[10932]115 text.append("\nMaximum Screen Size: ")
116 .append((int) maxScreenSize.getWidth()).append('x')
117 .append((int) maxScreenSize.getHeight()).append('\n');
[10929]118
[11156]119 if (Main.platform instanceof PlatformHookUnixoid) {
[7318]120 // Add Java package details
[6103]121 String packageDetails = ((PlatformHookUnixoid) Main.platform).getJavaPackageDetails();
122 if (packageDetails != null) {
[8379]123 text.append("Java package: ")
124 .append(packageDetails)
[8390]125 .append('\n');
[6103]126 }
[7318]127 // Add WebStart package details if run from JNLP
[10058]128 if (isRunningJavaWebStart()) {
[6850]129 String webStartDetails = ((PlatformHookUnixoid) Main.platform).getWebStartPackageDetails();
130 if (webStartDetails != null) {
[8379]131 text.append("WebStart package: ")
132 .append(webStartDetails)
[8390]133 .append('\n');
[6850]134 }
135 }
[10734]136 // Add Gnome Atk wrapper details if found
137 String atkWrapperDetails = ((PlatformHookUnixoid) Main.platform).getAtkWrapperPackageDetails();
138 if (atkWrapperDetails != null) {
139 text.append("Java ATK Wrapper package: ")
140 .append(atkWrapperDetails)
141 .append('\n');
142 }
[6103]143 }
[5831]144 try {
[5840]145 // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance)
[7005]146 List<String> vmArguments = new ArrayList<>(ManagementFactory.getRuntimeMXBean().getInputArguments());
[8465]147 for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext();) {
[5840]148 String value = it.next();
[5992]149 if (value.contains("=")) {
150 String[] param = value.split("=");
151 // Hide some parameters for privacy concerns
[8404]152 if (param[0].toLowerCase(Locale.ENGLISH).startsWith("-dproxy")) {
[5992]153 it.set(param[0]+"=xxx");
[11498]154 } else if ("-Djnlpx.vmargs".equals(param[0])) {
155 // Remove jnlpx.vmargs (base64 encoded copy of VM arguments already included in clear)
156 it.remove();
[5992]157 } else {
[10138]158 // Replace some paths for readability and privacy concerns
[10158]159 String val = paramCleanup(param[1]);
[10138]160 if (!val.equals(param[1])) {
161 it.set(param[0] + '=' + val);
162 }
[5992]163 }
[7420]164 } else if (value.startsWith("-X")) {
165 // Remove arguments like -Xbootclasspath/a, -Xverify:remote, that can be very long and unhelpful
166 it.remove();
[5840]167 }
168 }
[5831]169 if (!vmArguments.isEmpty()) {
[8390]170 text.append("VM arguments: ").append(vmArguments.toString().replace("\\\\", "\\")).append('\n');
[5831]171 }
172 } catch (SecurityException e) {
[12620]173 Logging.trace(e);
[5831]174 }
[11650]175 List<String> commandLineArgs = MainApplication.getCommandLineArgs();
[8356]176 if (!commandLineArgs.isEmpty()) {
[10158]177 text.append("Program arguments: ").append(Arrays.toString(paramCleanup(commandLineArgs).toArray())).append('\n');
[5831]178 }
[5955]179 if (Main.main != null) {
[12636]180 DataSet dataset = MainApplication.getLayerManager().getEditDataSet();
[5955]181 if (dataset != null) {
182 String result = DatasetConsistencyTest.runTests(dataset);
[8394]183 if (result.isEmpty()) {
[5955]184 text.append("Dataset consistency test: No problems found\n");
185 } else {
[8390]186 text.append("\nDataset consistency test:\n").append(result).append('\n');
[5955]187 }
[2500]188 }
189 }
[11113]190 text.append('\n');
[10696]191 appendCollection(text, "Plugins", Utils.transform(PluginHandler.getBugReportInformation(), i -> "+ " + i));
[12649]192 appendCollection(text, "Tagging presets", getCustomUrls(PresetPrefHelper.INSTANCE));
193 appendCollection(text, "Map paint styles", getCustomUrls(MapPaintPrefHelper.INSTANCE));
194 appendCollection(text, "Validator rules", getCustomUrls(ValidatorPrefHelper.INSTANCE));
[12620]195 appendCollection(text, "Last errors/warnings", Utils.transform(Logging.getLastErrorAndWarnings(), i -> "- " + i));
[10063]196
[10226]197 String osmApi = OsmApi.getOsmApi().getServerUrl();
198 if (!OsmApi.DEFAULT_API_URL.equals(osmApi.trim())) {
199 text.append("OSM API: ").append(osmApi).append("\n\n");
200 }
201
[10063]202 return text.toString();
203 }
204
[12649]205 private static Collection<String> getCustomUrls(SourcePrefHelper helper) {
[10696]206 final Set<String> defaultUrls = helper.getDefault().stream()
207 .map(i -> i.url)
208 .collect(Collectors.toSet());
209 return helper.get().stream()
210 .filter(i -> !defaultUrls.contains(i.url))
211 .map(i -> (i.active ? "+ " : "- ") + i.url)
212 .collect(Collectors.toList());
[10063]213 }
214
[10158]215 private static List<String> paramCleanup(Collection<String> params) {
216 List<String> result = new ArrayList<>(params.size());
217 for (String param : params) {
218 result.add(paramCleanup(param));
219 }
220 return result;
221 }
222
223 /**
224 * Shortens and removes private informations from a parameter used for status report.
225 * @param param parameter to cleanup
226 * @return shortened/anonymized parameter
227 */
228 private static String paramCleanup(String param) {
229 final String envJavaHome = System.getenv("JAVA_HOME");
230 final String envJavaHomeAlt = Main.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}";
231 final String propJavaHome = System.getProperty("java.home");
232 final String propJavaHomeAlt = "<java.home>";
[12856]233 final String prefDir = Config.getDirs().getPreferencesDirectory(false).toString();
[10158]234 final String prefDirAlt = "<josm.pref>";
[12856]235 final String userDataDir = Config.getDirs().getUserDataDirectory(false).toString();
[10158]236 final String userDataDirAlt = "<josm.userdata>";
[12856]237 final String userCacheDir = Config.getDirs().getCacheDirectory(false).toString();
[10158]238 final String userCacheDirAlt = "<josm.cache>";
239 final String userHomeDir = System.getProperty("user.home");
240 final String userHomeDirAlt = Main.isPlatformWindows() ? "%UserProfile%" : "${HOME}";
[10163]241 final String userName = System.getProperty("user.name");
242 final String userNameAlt = "<user.name>";
[10158]243
244 String val = param;
245 val = paramReplace(val, envJavaHome, envJavaHomeAlt);
246 val = paramReplace(val, envJavaHome, envJavaHomeAlt);
247 val = paramReplace(val, propJavaHome, propJavaHomeAlt);
248 val = paramReplace(val, prefDir, prefDirAlt);
249 val = paramReplace(val, userDataDir, userDataDirAlt);
250 val = paramReplace(val, userCacheDir, userCacheDirAlt);
251 val = paramReplace(val, userHomeDir, userHomeDirAlt);
[10796]252 if (userName.length() >= 3) {
253 val = paramReplace(val, userName, userNameAlt);
254 }
[10158]255 return val;
256 }
257
[10138]258 private static String paramReplace(String str, String target, String replacement) {
[10158]259 return target == null ? str : str.replace(target, replacement);
[10138]260 }
261
[10696]262 private static void appendCollection(StringBuilder text, String label, Collection<String> col) {
[10063]263 if (!col.isEmpty()) {
[10696]264 text.append(label).append(":\n");
265 for (String o : col) {
266 text.append(paramCleanup(o)).append('\n');
[7420]267 }
[8390]268 text.append('\n');
[7420]269 }
[1674]270 }
271
[6084]272 @Override
[1674]273 public void actionPerformed(ActionEvent e) {
274 StringBuilder text = new StringBuilder();
[5849]275 String reportHeader = getReportHeader();
276 text.append(reportHeader);
[11746]277 Map<String, Setting<?>> settings = Main.pref.getAllSettings();
278 Set<String> keys = new HashSet<>(settings.keySet());
279 for (String key : keys) {
280 // Remove sensitive information from status report
281 if (key.startsWith("marker.show") || key.contains("username") || key.contains("password") || key.contains("access-token")) {
282 settings.remove(key);
[1417]283 }
284 }
[11746]285 for (Entry<String, Setting<?>> entry : settings.entrySet()) {
286 text.append(paramCleanup(entry.getKey()))
287 .append('=')
288 .append(paramCleanup(entry.getValue().getValue().toString())).append('\n');
289 }
[1484]290
[10055]291 DebugTextDisplay ta = new DebugTextDisplay(text.toString());
[1417]292
[2039]293 ExtendedDialog ed = new ExtendedDialog(Main.parent,
294 tr("Status Report"),
[12279]295 tr("Copy to clipboard and close"), tr("Report bug"), tr("Close"));
296 ed.setButtonIcons("copy", "bug", "cancel");
[10055]297 ed.setContent(ta, false);
[5833]298 ed.setMinimumSize(new Dimension(380, 200));
299 ed.setPreferredSize(new Dimension(700, Main.parent.getHeight()-50));
[1484]300
[5849]301 switch (ed.showDialog().getValue()) {
[11102]302 case 1: ta.copyToClipboard(); break;
[10055]303 case 2: BugReportSender.reportBug(reportHeader); break;
[10216]304 default: // Do nothing
[5849]305 }
[1417]306 }
307}
Note: See TracBrowser for help on using the repository browser.