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.Version; 026import org.openstreetmap.josm.data.osm.DataSet; 027import org.openstreetmap.josm.data.osm.DatasetConsistencyTest; 028import org.openstreetmap.josm.data.preferences.Setting; 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 String runtimeVersion = System.getProperty("java.runtime.version"); 073 text.append(Version.getInstance().getReleaseAttributes()) 074 .append("\nIdentification: ").append(Version.getInstance().getAgentString()) 075 .append("\nMemory Usage: ") 076 .append(Runtime.getRuntime().totalMemory()/1024/1024) 077 .append(" MB / ") 078 .append(Runtime.getRuntime().maxMemory()/1024/1024) 079 .append(" MB (") 080 .append(Runtime.getRuntime().freeMemory()/1024/1024) 081 .append(" MB allocated, but free)\nJava version: ") 082 .append(runtimeVersion != null ? runtimeVersion : System.getProperty("java.version")).append(", ") 083 .append(System.getProperty("java.vendor")).append(", ") 084 .append(System.getProperty("java.vm.name")).append('\n'); 085 if (Main.platform.getClass() == PlatformHookUnixoid.class) { 086 // Add Java package details 087 String packageDetails = ((PlatformHookUnixoid) Main.platform).getJavaPackageDetails(); 088 if (packageDetails != null) { 089 text.append("Java package: ") 090 .append(packageDetails) 091 .append('\n'); 092 } 093 // Add WebStart package details if run from JNLP 094 if (Package.getPackage("javax.jnlp") != null) { 095 String webStartDetails = ((PlatformHookUnixoid) Main.platform).getWebStartPackageDetails(); 096 if (webStartDetails != null) { 097 text.append("WebStart package: ") 098 .append(webStartDetails) 099 .append('\n'); 100 } 101 } 102 } 103 try { 104 final String envJavaHome = System.getenv("JAVA_HOME"); 105 final String envJavaHomeAlt = Main.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}"; 106 final String propJavaHome = System.getProperty("java.home"); 107 final String propJavaHomeAlt = "<java.home>"; 108 // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance) 109 List<String> vmArguments = new ArrayList<>(ManagementFactory.getRuntimeMXBean().getInputArguments()); 110 for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext();) { 111 String value = it.next(); 112 if (value.contains("=")) { 113 String[] param = value.split("="); 114 // Hide some parameters for privacy concerns 115 if (param[0].toLowerCase(Locale.ENGLISH).startsWith("-dproxy")) { 116 it.set(param[0]+"=xxx"); 117 } else { 118 // Shorten some parameters for readability concerns 119 shortenParam(it, param, envJavaHome, envJavaHomeAlt); 120 shortenParam(it, param, propJavaHome, propJavaHomeAlt); 121 } 122 } else if (value.startsWith("-X")) { 123 // Remove arguments like -Xbootclasspath/a, -Xverify:remote, that can be very long and unhelpful 124 it.remove(); 125 } 126 } 127 if (!vmArguments.isEmpty()) { 128 text.append("VM arguments: ").append(vmArguments.toString().replace("\\\\", "\\")).append('\n'); 129 } 130 } catch (SecurityException e) { 131 // Ignore exception 132 if (Main.isTraceEnabled()) { 133 Main.trace(e.getMessage()); 134 } 135 } 136 List<String> commandLineArgs = Main.getCommandLineArgs(); 137 if (!commandLineArgs.isEmpty()) { 138 text.append("Program arguments: ").append(Arrays.toString(commandLineArgs.toArray())).append('\n'); 139 } 140 if (Main.main != null) { 141 DataSet dataset = Main.main.getCurrentDataSet(); 142 if (dataset != null) { 143 String result = DatasetConsistencyTest.runTests(dataset); 144 if (result.isEmpty()) { 145 text.append("Dataset consistency test: No problems found\n"); 146 } else { 147 text.append("\nDataset consistency test:\n").append(result).append('\n'); 148 } 149 } 150 } 151 text.append('\n').append(PluginHandler.getBugReportText()).append('\n'); 152 153 Collection<String> errorsWarnings = Main.getLastErrorAndWarnings(); 154 if (!errorsWarnings.isEmpty()) { 155 text.append("Last errors/warnings:\n"); 156 for (String s : errorsWarnings) { 157 text.append("- ").append(s).append('\n'); 158 } 159 text.append('\n'); 160 } 161 162 return text.toString(); 163 } 164 165 @Override 166 public void actionPerformed(ActionEvent e) { 167 StringBuilder text = new StringBuilder(); 168 String reportHeader = getReportHeader(); 169 text.append(reportHeader); 170 try { 171 Map<String, Setting<?>> settings = Main.pref.getAllSettings(); 172 Set<String> keys = new HashSet<>(settings.keySet()); 173 for (String key : keys) { 174 // Remove sensitive information from status report 175 if (key.startsWith("marker.show") || key.contains("username") || key.contains("password") || key.contains("access-token")) { 176 settings.remove(key); 177 } 178 } 179 for (Entry<String, Setting<?>> entry : settings.entrySet()) { 180 text.append(entry.getKey()).append('=').append(entry.getValue().getValue()).append('\n'); 181 } 182 } catch (Exception x) { 183 Main.error(x); 184 } 185 186 JosmTextArea ta = new JosmTextArea(text.toString()); 187 ta.setWrapStyleWord(true); 188 ta.setLineWrap(true); 189 ta.setEditable(false); 190 JScrollPane sp = new JScrollPane(ta); 191 192 ExtendedDialog ed = new ExtendedDialog(Main.parent, 193 tr("Status Report"), 194 new String[] {tr("Copy to clipboard and close"), tr("Report bug"), tr("Close") }); 195 ed.setButtonIcons(new String[] {"copy", "bug", "cancel" }); 196 ed.setContent(sp, false); 197 ed.setMinimumSize(new Dimension(380, 200)); 198 ed.setPreferredSize(new Dimension(700, Main.parent.getHeight()-50)); 199 200 switch (ed.showDialog().getValue()) { 201 case 1: Utils.copyToClipboard(text.toString()); break; 202 case 2: ReportBugAction.reportBug(reportHeader); break; 203 } 204 } 205}