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; 021import java.util.TreeSet; 022 023import org.openstreetmap.josm.Main; 024import org.openstreetmap.josm.data.Version; 025import org.openstreetmap.josm.data.osm.DataSet; 026import org.openstreetmap.josm.data.osm.DatasetConsistencyTest; 027import org.openstreetmap.josm.data.preferences.Setting; 028import org.openstreetmap.josm.gui.ExtendedDialog; 029import org.openstreetmap.josm.gui.preferences.SourceEditor; 030import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry; 031import org.openstreetmap.josm.gui.preferences.SourceEntry; 032import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference; 033import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference; 034import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference; 035import org.openstreetmap.josm.io.OsmApi; 036import org.openstreetmap.josm.plugins.PluginHandler; 037import org.openstreetmap.josm.tools.PlatformHookUnixoid; 038import org.openstreetmap.josm.tools.Shortcut; 039import org.openstreetmap.josm.tools.bugreport.BugReportSender; 040import org.openstreetmap.josm.tools.bugreport.DebugTextDisplay; 041 042/** 043 * @author xeen 044 * 045 * Opens a dialog with useful status information like version numbers for Java, JOSM and plugins 046 * Also includes preferences with stripped username and password 047 */ 048public final class ShowStatusReportAction extends JosmAction { 049 050 /** 051 * Constructs a new {@code ShowStatusReportAction} 052 */ 053 public ShowStatusReportAction() { 054 super( 055 tr("Show Status Report"), 056 "clock", 057 tr("Show status report with useful information that can be attached to bugs"), 058 Shortcut.registerShortcut("help:showstatusreport", tr("Help: {0}", 059 tr("Show Status Report")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false); 060 061 putValue("help", ht("/Action/ShowStatusReport")); 062 putValue("toolbar", "help/showstatusreport"); 063 Main.toolbar.register(this); 064 } 065 066 private static boolean isRunningJavaWebStart() { 067 try { 068 // See http://stackoverflow.com/a/16200769/2257172 069 return Class.forName("javax.jnlp.ServiceManager") != null; 070 } catch (ClassNotFoundException e) { 071 return false; 072 } 073 } 074 075 /** 076 * Replies the report header (software and system info) 077 * @return The report header (software and system info) 078 */ 079 public static String getReportHeader() { 080 StringBuilder text = new StringBuilder(256); 081 String runtimeVersion = System.getProperty("java.runtime.version"); 082 text.append(Version.getInstance().getReleaseAttributes()) 083 .append("\nIdentification: ").append(Version.getInstance().getAgentString()) 084 .append("\nMemory Usage: ") 085 .append(Runtime.getRuntime().totalMemory()/1024/1024) 086 .append(" MB / ") 087 .append(Runtime.getRuntime().maxMemory()/1024/1024) 088 .append(" MB (") 089 .append(Runtime.getRuntime().freeMemory()/1024/1024) 090 .append(" MB allocated, but free)\nJava version: ") 091 .append(runtimeVersion != null ? runtimeVersion : System.getProperty("java.version")).append(", ") 092 .append(System.getProperty("java.vendor")).append(", ") 093 .append(System.getProperty("java.vm.name")).append('\n'); 094 if (Main.platform.getClass() == PlatformHookUnixoid.class) { 095 // Add Java package details 096 String packageDetails = ((PlatformHookUnixoid) Main.platform).getJavaPackageDetails(); 097 if (packageDetails != null) { 098 text.append("Java package: ") 099 .append(packageDetails) 100 .append('\n'); 101 } 102 // Add WebStart package details if run from JNLP 103 if (isRunningJavaWebStart()) { 104 String webStartDetails = ((PlatformHookUnixoid) Main.platform).getWebStartPackageDetails(); 105 if (webStartDetails != null) { 106 text.append("WebStart package: ") 107 .append(webStartDetails) 108 .append('\n'); 109 } 110 } 111 } 112 try { 113 // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance) 114 List<String> vmArguments = new ArrayList<>(ManagementFactory.getRuntimeMXBean().getInputArguments()); 115 for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext();) { 116 String value = it.next(); 117 if (value.contains("=")) { 118 String[] param = value.split("="); 119 // Hide some parameters for privacy concerns 120 if (param[0].toLowerCase(Locale.ENGLISH).startsWith("-dproxy")) { 121 it.set(param[0]+"=xxx"); 122 } else { 123 // Replace some paths for readability and privacy concerns 124 String val = paramCleanup(param[1]); 125 if (!val.equals(param[1])) { 126 it.set(param[0] + '=' + val); 127 } 128 } 129 } else if (value.startsWith("-X")) { 130 // Remove arguments like -Xbootclasspath/a, -Xverify:remote, that can be very long and unhelpful 131 it.remove(); 132 } 133 } 134 if (!vmArguments.isEmpty()) { 135 text.append("VM arguments: ").append(vmArguments.toString().replace("\\\\", "\\")).append('\n'); 136 } 137 } catch (SecurityException e) { 138 Main.trace(e); 139 } 140 List<String> commandLineArgs = Main.getCommandLineArgs(); 141 if (!commandLineArgs.isEmpty()) { 142 text.append("Program arguments: ").append(Arrays.toString(paramCleanup(commandLineArgs).toArray())).append('\n'); 143 } 144 if (Main.main != null) { 145 DataSet dataset = Main.getLayerManager().getEditDataSet(); 146 if (dataset != null) { 147 String result = DatasetConsistencyTest.runTests(dataset); 148 if (result.isEmpty()) { 149 text.append("Dataset consistency test: No problems found\n"); 150 } else { 151 text.append("\nDataset consistency test:\n").append(result).append('\n'); 152 } 153 } 154 } 155 text.append('\n').append(PluginHandler.getBugReportText()).append('\n'); 156 157 appendCollection(text, "Tagging presets", getCustomUrls(TaggingPresetPreference.PresetPrefHelper.INSTANCE)); 158 appendCollection(text, "Map paint styles", getCustomUrls(MapPaintPreference.MapPaintPrefHelper.INSTANCE)); 159 appendCollection(text, "Validator rules", getCustomUrls(ValidatorTagCheckerRulesPreference.RulePrefHelper.INSTANCE)); 160 appendCollection(text, "Last errors/warnings", Main.getLastErrorAndWarnings()); 161 162 String osmApi = OsmApi.getOsmApi().getServerUrl(); 163 if (!OsmApi.DEFAULT_API_URL.equals(osmApi.trim())) { 164 text.append("OSM API: ").append(osmApi).append("\n\n"); 165 } 166 167 return text.toString(); 168 } 169 170 private static Collection<String> getCustomUrls(SourceEditor.SourcePrefHelper helper) { 171 Set<String> set = new TreeSet<>(); 172 for (SourceEntry entry : helper.get()) { 173 set.add(entry.url); 174 } 175 for (ExtendedSourceEntry def : helper.getDefault()) { 176 set.remove(def.url); 177 } 178 return set; 179 } 180 181 private static List<String> paramCleanup(Collection<String> params) { 182 List<String> result = new ArrayList<>(params.size()); 183 for (String param : params) { 184 result.add(paramCleanup(param)); 185 } 186 return result; 187 } 188 189 /** 190 * Shortens and removes private informations from a parameter used for status report. 191 * @param param parameter to cleanup 192 * @return shortened/anonymized parameter 193 */ 194 private static String paramCleanup(String param) { 195 final String envJavaHome = System.getenv("JAVA_HOME"); 196 final String envJavaHomeAlt = Main.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}"; 197 final String propJavaHome = System.getProperty("java.home"); 198 final String propJavaHomeAlt = "<java.home>"; 199 final String prefDir = Main.pref.getPreferencesDirectory().toString(); 200 final String prefDirAlt = "<josm.pref>"; 201 final String userDataDir = Main.pref.getUserDataDirectory().toString(); 202 final String userDataDirAlt = "<josm.userdata>"; 203 final String userCacheDir = Main.pref.getCacheDirectory().toString(); 204 final String userCacheDirAlt = "<josm.cache>"; 205 final String userHomeDir = System.getProperty("user.home"); 206 final String userHomeDirAlt = Main.isPlatformWindows() ? "%UserProfile%" : "${HOME}"; 207 final String userName = System.getProperty("user.name"); 208 final String userNameAlt = "<user.name>"; 209 210 String val = param; 211 val = paramReplace(val, envJavaHome, envJavaHomeAlt); 212 val = paramReplace(val, envJavaHome, envJavaHomeAlt); 213 val = paramReplace(val, propJavaHome, propJavaHomeAlt); 214 val = paramReplace(val, prefDir, prefDirAlt); 215 val = paramReplace(val, userDataDir, userDataDirAlt); 216 val = paramReplace(val, userCacheDir, userCacheDirAlt); 217 val = paramReplace(val, userHomeDir, userHomeDirAlt); 218 val = paramReplace(val, userName, userNameAlt); 219 return val; 220 } 221 222 private static String paramReplace(String str, String target, String replacement) { 223 return target == null ? str : str.replace(target, replacement); 224 } 225 226 private static <T> void appendCollection(StringBuilder text, String label, Collection<T> col) { 227 if (!col.isEmpty()) { 228 text.append(label+":\n"); 229 for (T o : col) { 230 text.append("- ").append(paramCleanup(o.toString())).append('\n'); 231 } 232 text.append('\n'); 233 } 234 } 235 236 @Override 237 public void actionPerformed(ActionEvent e) { 238 StringBuilder text = new StringBuilder(); 239 String reportHeader = getReportHeader(); 240 text.append(reportHeader); 241 try { 242 Map<String, Setting<?>> settings = Main.pref.getAllSettings(); 243 Set<String> keys = new HashSet<>(settings.keySet()); 244 for (String key : keys) { 245 // Remove sensitive information from status report 246 if (key.startsWith("marker.show") || key.contains("username") || key.contains("password") || key.contains("access-token")) { 247 settings.remove(key); 248 } 249 } 250 for (Entry<String, Setting<?>> entry : settings.entrySet()) { 251 text.append(paramCleanup(entry.getKey())) 252 .append('=') 253 .append(paramCleanup(entry.getValue().getValue().toString())).append('\n'); 254 } 255 } catch (Exception x) { 256 Main.error(x); 257 } 258 259 DebugTextDisplay ta = new DebugTextDisplay(text.toString()); 260 261 ExtendedDialog ed = new ExtendedDialog(Main.parent, 262 tr("Status Report"), 263 new String[] {tr("Copy to clipboard and close"), tr("Report bug"), tr("Close") }); 264 ed.setButtonIcons(new String[] {"copy", "bug", "cancel" }); 265 ed.setContent(ta, false); 266 ed.setMinimumSize(new Dimension(380, 200)); 267 ed.setPreferredSize(new Dimension(700, Main.parent.getHeight()-50)); 268 269 switch (ed.showDialog().getValue()) { 270 case 1: ta.copyToClippboard(); break; 271 case 2: BugReportSender.reportBug(reportHeader); break; 272 default: // Do nothing 273 } 274 } 275}