source: josm/trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java@ 7313

Last change on this file since 7313 was 7206, checked in by Don-vip, 10 years ago

see #10033 - allow remote control to work from osm.org in https on Windows systems by adding updated JOSM localhost certificate to Windows Root Certificates keystore

  • Property svn:eol-style set to native
File size: 15.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Desktop;
7import java.awt.Dimension;
8import java.awt.GraphicsEnvironment;
9import java.awt.event.KeyEvent;
10import java.io.BufferedReader;
11import java.io.File;
12import java.io.FileInputStream;
13import java.io.IOException;
14import java.io.InputStreamReader;
15import java.net.URI;
16import java.net.URISyntaxException;
17import java.nio.charset.StandardCharsets;
18import java.security.KeyStore;
19import java.security.KeyStoreException;
20import java.security.NoSuchAlgorithmException;
21import java.security.cert.CertificateException;
22import java.util.Arrays;
23
24import javax.swing.JOptionPane;
25
26import org.openstreetmap.josm.Main;
27import org.openstreetmap.josm.gui.ExtendedDialog;
28import org.openstreetmap.josm.gui.util.GuiHelper;
29
30/**
31 * {@code PlatformHook} base implementation.
32 *
33 * Don't write (Main.platform instanceof PlatformHookUnixoid) because other platform
34 * hooks are subclasses of this class.
35 */
36public class PlatformHookUnixoid implements PlatformHook {
37
38 private String osDescription;
39
40 @Override
41 public void preStartupHook() {
42 }
43
44 @Override
45 public void startupHook() {
46 }
47
48 @Override
49 public void openUrl(String url) throws IOException {
50 for (String program : Main.pref.getCollection("browser.unix",
51 Arrays.asList("xdg-open", "#DESKTOP#", "$BROWSER", "gnome-open", "kfmclient openURL", "firefox"))) {
52 try {
53 if ("#DESKTOP#".equals(program)) {
54 Desktop.getDesktop().browse(new URI(url));
55 } else if (program.startsWith("$")) {
56 program = System.getenv().get(program.substring(1));
57 Runtime.getRuntime().exec(new String[]{program, url});
58 } else {
59 Runtime.getRuntime().exec(new String[]{program, url});
60 }
61 return;
62 } catch (IOException | URISyntaxException e) {
63 Main.warn(e);
64 }
65 }
66 }
67
68 @Override
69 public void initSystemShortcuts() {
70 // TODO: Insert system shortcuts here. See Windows and especially OSX to see how to.
71 for(int i = KeyEvent.VK_F1; i <= KeyEvent.VK_F12; ++i)
72 Shortcut.registerSystemShortcut("screen:toogle"+i, tr("reserved"), i, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK).setAutomatic();
73 Shortcut.registerSystemShortcut("system:reset", tr("reserved"), KeyEvent.VK_DELETE, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK).setAutomatic();
74 Shortcut.registerSystemShortcut("system:resetX", tr("reserved"), KeyEvent.VK_BACK_SPACE, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK).setAutomatic();
75 }
76
77 /**
78 * This should work for all platforms. Yeah, should.
79 * See PlatformHook.java for a list of reasons why
80 * this is implemented here...
81 */
82 @Override
83 public String makeTooltip(String name, Shortcut sc) {
84 String result = "";
85 result += "<html>";
86 result += name;
87 if (sc != null && sc.getKeyText().length() != 0) {
88 result += " ";
89 result += "<font size='-2'>";
90 result += "("+sc.getKeyText()+")";
91 result += "</font>";
92 }
93 result += "&nbsp;</html>";
94 return result;
95 }
96
97 @Override
98 public String getDefaultStyle() {
99 return "javax.swing.plaf.metal.MetalLookAndFeel";
100 }
101
102 @Override
103 public boolean canFullscreen() {
104 return !GraphicsEnvironment.isHeadless() &&
105 GraphicsEnvironment.getLocalGraphicsEnvironment()
106 .getDefaultScreenDevice().isFullScreenSupported();
107 }
108
109 @Override
110 public boolean rename(File from, File to) {
111 return from.renameTo(to);
112 }
113
114 /**
115 * Determines if the distribution is Debian or Ubuntu, or a derivative.
116 * @return {@code true} if the distribution is Debian, Ubuntu or Mint, {@code false} otherwise
117 */
118 public static boolean isDebianOrUbuntu() {
119 try {
120 String dist = Utils.execOutput(Arrays.asList("lsb_release", "-i", "-s"));
121 return "Debian".equalsIgnoreCase(dist) || "Ubuntu".equalsIgnoreCase(dist) || "Mint".equalsIgnoreCase(dist);
122 } catch (IOException e) {
123 Main.warn(e);
124 return false;
125 }
126 }
127
128 /**
129 * Determines if the JVM is OpenJDK-based.
130 * @return {@code true} if {@code java.home} contains "openjdk", {@code false} otherwise
131 * @since 6951
132 */
133 public static boolean isOpenJDK() {
134 String javaHome = System.getProperty("java.home");
135 return javaHome != null && javaHome.contains("openjdk");
136 }
137
138 /**
139 * Get the package name including detailed version.
140 * @param packageName The package name
141 * @return The package name and package version if it can be identified, null otherwise
142 */
143 public static String getPackageDetails(String packageName) {
144 try {
145 String version = Utils.execOutput(Arrays.asList(
146 "dpkg-query", "--show", "--showformat", "${Architecture}-${Version}", packageName));
147 if (version != null) {
148 return packageName + ":" + version;
149 }
150 } catch (IOException e) {
151 Main.warn(e);
152 }
153 return null;
154 }
155
156 /**
157 * Get the Java package name including detailed version.
158 *
159 * Some Java bugs are specific to a certain security update, so in addition
160 * to the Java version, we also need the exact package version.
161 *
162 * This was originally written for #8921, so only Debian based distributions
163 * are covered at the moment. This can be extended to other distributions
164 * if needed.
165 *
166 * @return The package name and package version if it can be identified, null
167 * otherwise
168 */
169 public String getJavaPackageDetails() {
170 if (isDebianOrUbuntu()) {
171 switch(System.getProperty("java.home")) {
172 case "/usr/lib/jvm/java-7-openjdk-amd64/jre":
173 case "/usr/lib/jvm/java-7-openjdk-i386/jre":
174 return getPackageDetails("openjdk-7-jre");
175 }
176 }
177 return null;
178 }
179
180 /**
181 * Get the Web Start package name including detailed version.
182 *
183 * Debian and Ubuntu OpenJDK packages are shipped with icedtea-web package,
184 * but its version does not match main java package version.
185 *
186 * Only Debian based distributions are covered at the moment.
187 * This can be extended to other distributions if needed.
188 *
189 * Simply return {@code null} if there's no separate package for Java WebStart.
190 *
191 * @return The package name and package version if it can be identified, null otherwise
192 */
193 public String getWebStartPackageDetails() {
194 if (isDebianOrUbuntu() && isOpenJDK()) {
195 return getPackageDetails("icedtea-netx");
196 }
197 return null;
198 }
199
200 protected String buildOSDescription() {
201 String osName = System.getProperty("os.name");
202 if ("Linux".equalsIgnoreCase(osName)) {
203 try {
204 // Try lsb_release (only available on LSB-compliant Linux systems, see https://www.linuxbase.org/lsb-cert/productdir.php?by_prod )
205 Process p = Runtime.getRuntime().exec("lsb_release -ds");
206 try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8))) {
207 String line = Utils.strip(input.readLine());
208 if (line != null && !line.isEmpty()) {
209 line = line.replaceAll("\"+","");
210 line = line.replaceAll("NAME=",""); // strange code for some Gentoo's
211 if(line.startsWith("Linux ")) // e.g. Linux Mint
212 return line;
213 else if(!line.isEmpty())
214 return "Linux " + line;
215 }
216 }
217 } catch (IOException e) {
218 // Non LSB-compliant Linux system. List of common fallback release files: http://linuxmafia.com/faq/Admin/release-files.html
219 for (LinuxReleaseInfo info : new LinuxReleaseInfo[]{
220 new LinuxReleaseInfo("/etc/lsb-release", "DISTRIB_DESCRIPTION", "DISTRIB_ID", "DISTRIB_RELEASE"),
221 new LinuxReleaseInfo("/etc/os-release", "PRETTY_NAME", "NAME", "VERSION"),
222 new LinuxReleaseInfo("/etc/arch-release"),
223 new LinuxReleaseInfo("/etc/debian_version", "Debian GNU/Linux "),
224 new LinuxReleaseInfo("/etc/fedora-release"),
225 new LinuxReleaseInfo("/etc/gentoo-release"),
226 new LinuxReleaseInfo("/etc/redhat-release")
227 }) {
228 String description = info.extractDescription();
229 if (description != null && !description.isEmpty()) {
230 return "Linux " + description;
231 }
232 }
233 }
234 }
235 return osName;
236 }
237
238 @Override
239 public String getOSDescription() {
240 if (osDescription == null) {
241 osDescription = buildOSDescription();
242 }
243 return osDescription;
244 }
245
246 protected static class LinuxReleaseInfo {
247 private final String path;
248 private final String descriptionField;
249 private final String idField;
250 private final String releaseField;
251 private final boolean plainText;
252 private final String prefix;
253
254 public LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField) {
255 this(path, descriptionField, idField, releaseField, false, null);
256 }
257
258 public LinuxReleaseInfo(String path) {
259 this(path, null, null, null, true, null);
260 }
261
262 public LinuxReleaseInfo(String path, String prefix) {
263 this(path, null, null, null, true, prefix);
264 }
265
266 private LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField, boolean plainText, String prefix) {
267 this.path = path;
268 this.descriptionField = descriptionField;
269 this.idField = idField;
270 this.releaseField = releaseField;
271 this.plainText = plainText;
272 this.prefix = prefix;
273 }
274
275 @Override public String toString() {
276 return "ReleaseInfo [path=" + path + ", descriptionField=" + descriptionField +
277 ", idField=" + idField + ", releaseField=" + releaseField + "]";
278 }
279
280 /**
281 * Extracts OS detailed information from a Linux release file (/etc/xxx-release)
282 * @return The OS detailed information, or {@code null}
283 */
284 public String extractDescription() {
285 String result = null;
286 if (path != null) {
287 File file = new File(path);
288 if (file.exists()) {
289 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
290 String id = null;
291 String release = null;
292 String line;
293 while (result == null && (line = reader.readLine()) != null) {
294 if (line.contains("=")) {
295 String[] tokens = line.split("=");
296 if (tokens.length >= 2) {
297 // Description, if available, contains exactly what we need
298 if (descriptionField != null && descriptionField.equalsIgnoreCase(tokens[0])) {
299 result = Utils.strip(tokens[1]);
300 } else if (idField != null && idField.equalsIgnoreCase(tokens[0])) {
301 id = Utils.strip(tokens[1]);
302 } else if (releaseField != null && releaseField.equalsIgnoreCase(tokens[0])) {
303 release = Utils.strip(tokens[1]);
304 }
305 }
306 } else if (plainText && !line.isEmpty()) {
307 // Files composed of a single line
308 result = Utils.strip(line);
309 }
310 }
311 // If no description has been found, try to rebuild it with "id" + "release" (i.e. "name" + "version")
312 if (result == null && id != null && release != null) {
313 result = id + " " + release;
314 }
315 } catch (IOException e) {
316 // Ignore
317 }
318 }
319 }
320 // Append prefix if any
321 if (result != null && !result.isEmpty() && prefix != null && !prefix.isEmpty()) {
322 result = prefix + result;
323 }
324 if(result != null)
325 result = result.replaceAll("\"+","");
326 return result;
327 }
328 }
329
330 protected void askUpdateJava(String version) {
331 askUpdateJava(version, "https://www.java.com/download");
332 }
333
334 // Method kept because strings have already been translated. To enable for Java 8 migration somewhere in 2016
335 protected void askUpdateJava(final String version, final String url) {
336 GuiHelper.runInEDTAndWait(new Runnable() {
337 @Override
338 public void run() {
339 ExtendedDialog ed = new ExtendedDialog(
340 Main.parent,
341 tr("Outdated Java version"),
342 new String[]{tr("Update Java"), tr("Cancel")});
343 // Check if the dialog has not already been permanently hidden by user
344 if (!ed.toggleEnable("askUpdateJava8").toggleCheckState()) {
345 ed.setButtonIcons(new String[]{"java.png", "cancel.png"}).setCancelButton(2);
346 ed.setMinimumSize(new Dimension(480, 300));
347 ed.setIcon(JOptionPane.WARNING_MESSAGE);
348 String content = tr("You are running version {0} of Java.", "<b>"+version+"</b>")+"<br><br>";
349 if ("Sun Microsystems Inc.".equals(System.getProperty("java.vendor")) && !isOpenJDK()) {
350 content += "<b>"+tr("This version is no longer supported by {0} since {1} and is not recommended for use.",
351 "Oracle", tr("April 2015"))+"</b><br><br>";
352 }
353 content += "<b>"+tr("JOSM will soon stop working with this version; we highly recommend you to update to Java {0}.", "8")+"</b><br><br>"+
354 tr("Would you like to update now ?");
355 ed.setContent(content);
356
357 if (ed.showDialog().getValue() == 1) {
358 try {
359 openUrl(url);
360 } catch (IOException e) {
361 Main.warn(e);
362 }
363 }
364 }
365 }
366 });
367 }
368
369 @Override
370 public void setupHttpsCertificate(KeyStore.PrivateKeyEntry privateKeyEntry)
371 throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
372 // TODO setup HTTPS certificate on Unix systems
373 }
374}
Note: See TracBrowser for help on using the repository browser.