001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 005import static org.openstreetmap.josm.tools.I18n.tr; 006 007import java.awt.Dimension; 008import java.awt.event.ActionEvent; 009import java.awt.event.KeyEvent; 010import java.lang.management.ManagementFactory; 011import java.util.ArrayList; 012import java.util.Arrays; 013import java.util.Collection; 014import java.util.HashSet; 015import java.util.List; 016import java.util.ListIterator; 017import java.util.Locale; 018import java.util.Map; 019import java.util.Map.Entry; 020import java.util.Set; 021 022import javax.swing.JScrollPane; 023 024import org.openstreetmap.josm.Main; 025import org.openstreetmap.josm.data.Preferences.Setting; 026import org.openstreetmap.josm.data.Version; 027import org.openstreetmap.josm.data.osm.DataSet; 028import org.openstreetmap.josm.data.osm.DatasetConsistencyTest; 029import org.openstreetmap.josm.gui.ExtendedDialog; 030import org.openstreetmap.josm.gui.widgets.JosmTextArea; 031import org.openstreetmap.josm.plugins.PluginHandler; 032import org.openstreetmap.josm.tools.PlatformHookUnixoid; 033import org.openstreetmap.josm.tools.Shortcut; 034import org.openstreetmap.josm.tools.Utils; 035 036/** 037 * @author xeen 038 * 039 * Opens a dialog with useful status information like version numbers for Java, JOSM and plugins 040 * Also includes preferences with stripped username and password 041 */ 042public final class ShowStatusReportAction extends JosmAction { 043 044 /** 045 * Constructs a new {@code ShowStatusReportAction} 046 */ 047 public ShowStatusReportAction() { 048 super( 049 tr("Show Status Report"), 050 "clock", 051 tr("Show status report with useful information that can be attached to bugs"), 052 Shortcut.registerShortcut("help:showstatusreport", tr("Help: {0}", 053 tr("Show Status Report")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false); 054 055 putValue("help", ht("/Action/ShowStatusReport")); 056 putValue("toolbar", "help/showstatusreport"); 057 Main.toolbar.register(this); 058 } 059 060 private static void shortenParam(ListIterator<String> it, String[] param, String source, String target) { 061 if (source != null && target.length() < source.length() && param[1].startsWith(source)) { 062 it.set(param[0] + '=' + param[1].replace(source, target)); 063 } 064 } 065 066 /** 067 * Replies the report header (software and system info) 068 * @return The report header (software and system info) 069 */ 070 public static String getReportHeader() { 071 StringBuilder text = new StringBuilder(); 072 text.append(Version.getInstance().getReleaseAttributes()) 073 .append("\nIdentification: ").append(Version.getInstance().getAgentString()) 074 .append("\nMemory Usage: ") 075 .append(Runtime.getRuntime().totalMemory()/1024/1024) 076 .append(" MB / ") 077 .append(Runtime.getRuntime().maxMemory()/1024/1024) 078 .append(" MB (") 079 .append(Runtime.getRuntime().freeMemory()/1024/1024) 080 .append(" MB allocated, but free)\nJava version: ") 081 .append(System.getProperty("java.version")).append(", ") 082 .append(System.getProperty("java.vendor")).append(", ") 083 .append(System.getProperty("java.vm.name")).append('\n'); 084 if (Main.platform.getClass() == PlatformHookUnixoid.class) { 085 // Add Java package details 086 String packageDetails = ((PlatformHookUnixoid) Main.platform).getJavaPackageDetails(); 087 if (packageDetails != null) { 088 text.append("Java package: ") 089 .append(packageDetails) 090 .append('\n'); 091 } 092 // Add WebStart package details if run from JNLP 093 if (Package.getPackage("javax.jnlp") != null) { 094 String webStartDetails = ((PlatformHookUnixoid) Main.platform).getWebStartPackageDetails(); 095 if (webStartDetails != null) { 096 text.append("WebStart package: ") 097 .append(webStartDetails) 098 .append('\n'); 099 } 100 } 101 } 102 try { 103 final String envJavaHome = System.getenv("JAVA_HOME"); 104 final String envJavaHomeAlt = Main.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}"; 105 final String propJavaHome = System.getProperty("java.home"); 106 final String propJavaHomeAlt = "<java.home>"; 107 // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance) 108 List<String> vmArguments = new ArrayList<>(ManagementFactory.getRuntimeMXBean().getInputArguments()); 109 for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext();) { 110 String value = it.next(); 111 if (value.contains("=")) { 112 String[] param = value.split("="); 113 // Hide some parameters for privacy concerns 114 if (param[0].toLowerCase(Locale.ENGLISH).startsWith("-dproxy")) { 115 it.set(param[0]+"=xxx"); 116 // Shorten some parameters for readability concerns 117 } else { 118 shortenParam(it, param, envJavaHome, envJavaHomeAlt); 119 shortenParam(it, param, propJavaHome, propJavaHomeAlt); 120 } 121 } else if (value.startsWith("-X")) { 122 // Remove arguments like -Xbootclasspath/a, -Xverify:remote, that can be very long and unhelpful 123 it.remove(); 124 } 125 } 126 if (!vmArguments.isEmpty()) { 127 text.append("VM arguments: ").append(vmArguments.toString().replace("\\\\", "\\")).append('\n'); 128 } 129 } catch (SecurityException e) { 130 // Ignore exception 131 if (Main.isTraceEnabled()) { 132 Main.trace(e.getMessage()); 133 } 134 } 135 List<String> commandLineArgs = Main.getCommandLineArgs(); 136 if (!commandLineArgs.isEmpty()) { 137 text.append("Program arguments: ").append(Arrays.toString(commandLineArgs.toArray())).append('\n'); 138 } 139 if (Main.main != null) { 140 DataSet dataset = Main.main.getCurrentDataSet(); 141 if (dataset != null) { 142 String result = DatasetConsistencyTest.runTests(dataset); 143 if (result.isEmpty()) { 144 text.append("Dataset consistency test: No problems found\n"); 145 } else { 146 text.append("\nDataset consistency test:\n").append(result).append('\n'); 147 } 148 } 149 } 150 text.append('\n').append(PluginHandler.getBugReportText()).append('\n'); 151 152 Collection<String> errorsWarnings = Main.getLastErrorAndWarnings(); 153 if (!errorsWarnings.isEmpty()) { 154 text.append("Last errors/warnings:\n"); 155 for (String s : errorsWarnings) { 156 text.append("- ").append(s).append('\n'); 157 } 158 text.append('\n'); 159 } 160 161 return text.toString(); 162 } 163 164 @Override 165 public void actionPerformed(ActionEvent e) { 166 StringBuilder text = new StringBuilder(); 167 String reportHeader = getReportHeader(); 168 text.append(reportHeader); 169 try { 170 Map<String, Setting<?>> settings = Main.pref.getAllSettings(); 171 Set<String> keys = new HashSet<>(settings.keySet()); 172 for (String key : keys) { 173 // Remove sensitive information from status report 174 if (key.startsWith("marker.show") || key.contains("username") || key.contains("password") || key.contains("access-token")) { 175 settings.remove(key); 176 } 177 } 178 for (Entry<String, Setting<?>> entry : settings.entrySet()) { 179 text.append(entry.getKey()).append('=').append(entry.getValue().getValue()).append('\n'); 180 } 181 } catch (Exception x) { 182 Main.error(x); 183 } 184 185 JosmTextArea ta = new JosmTextArea(text.toString()); 186 ta.setWrapStyleWord(true); 187 ta.setLineWrap(true); 188 ta.setEditable(false); 189 JScrollPane sp = new JScrollPane(ta); 190 191 ExtendedDialog ed = new ExtendedDialog(Main.parent, 192 tr("Status Report"), 193 new String[] {tr("Copy to clipboard and close"), tr("Report bug"), tr("Close") }); 194 ed.setButtonIcons(new String[] {"copy", "bug", "cancel" }); 195 ed.setContent(sp, false); 196 ed.setMinimumSize(new Dimension(380, 200)); 197 ed.setPreferredSize(new Dimension(700, Main.parent.getHeight()-50)); 198 199 switch (ed.showDialog().getValue()) { 200 case 1: Utils.copyToClipboard(text.toString()); break; 201 case 2: ReportBugAction.reportBug(reportHeader); break; 202 } 203 } 204}