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

Last change on this file since 12225 was 12219, checked in by Don-vip, 7 years ago

see #14652 - ask Windows/macOS users to update their version of Java when it expires (i.e when the built-in JRE expiration date is passed, about 4 months after release, 1 month after Java should have asked to update by itself). It currently proposes to update all versions of Java 8 up to update 121, released on January 17, 2017, as its expiration date is May 18, 2017.

  • Property svn:eol-style set to native
File size: 15.9 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.event.KeyEvent;
8import java.io.BufferedReader;
9import java.io.File;
10import java.io.IOException;
11import java.net.URI;
12import java.net.URISyntaxException;
13import java.nio.charset.StandardCharsets;
14import java.nio.file.Files;
15import java.nio.file.Path;
16import java.nio.file.Paths;
17import java.util.Arrays;
18import java.util.List;
19import java.util.Locale;
20
21import org.openstreetmap.josm.Main;
22
23/**
24 * {@code PlatformHook} implementation for Unix systems.
25 * @since 1023
26 */
27public class PlatformHookUnixoid implements PlatformHook {
28
29 private String osDescription;
30
31 @Override
32 public void preStartupHook() {
33 // See #12022 - Disable GNOME ATK Java wrapper as it causes a lot of serious trouble
34 if ("org.GNOME.Accessibility.AtkWrapper".equals(System.getProperty("assistive_technologies"))) {
35 System.clearProperty("assistive_technologies");
36 }
37 }
38
39 @Override
40 public void openUrl(String url) throws IOException {
41 for (String program : Main.pref.getCollection("browser.unix",
42 Arrays.asList("xdg-open", "#DESKTOP#", "$BROWSER", "gnome-open", "kfmclient openURL", "firefox"))) {
43 try {
44 if ("#DESKTOP#".equals(program)) {
45 Desktop.getDesktop().browse(new URI(url));
46 } else if (program.startsWith("$")) {
47 program = System.getenv().get(program.substring(1));
48 Runtime.getRuntime().exec(new String[]{program, url});
49 } else {
50 Runtime.getRuntime().exec(new String[]{program, url});
51 }
52 return;
53 } catch (IOException | URISyntaxException e) {
54 Main.warn(e);
55 }
56 }
57 }
58
59 @Override
60 public void initSystemShortcuts() {
61 // CHECKSTYLE.OFF: LineLength
62 // TODO: Insert system shortcuts here. See Windows and especially OSX to see how to.
63 for (int i = KeyEvent.VK_F1; i <= KeyEvent.VK_F12; ++i) {
64 Shortcut.registerSystemShortcut("screen:toogle"+i, tr("reserved"), i, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK)
65 .setAutomatic();
66 }
67 Shortcut.registerSystemShortcut("system:reset", tr("reserved"), KeyEvent.VK_DELETE, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK)
68 .setAutomatic();
69 Shortcut.registerSystemShortcut("system:resetX", tr("reserved"), KeyEvent.VK_BACK_SPACE, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK)
70 .setAutomatic();
71 // CHECKSTYLE.ON: LineLength
72 }
73
74 @Override
75 public String getDefaultStyle() {
76 return "javax.swing.plaf.metal.MetalLookAndFeel";
77 }
78
79 /**
80 * Determines if the distribution is Debian or Ubuntu, or a derivative.
81 * @return {@code true} if the distribution is Debian, Ubuntu or Mint, {@code false} otherwise
82 */
83 public static boolean isDebianOrUbuntu() {
84 try {
85 String dist = Utils.execOutput(Arrays.asList("lsb_release", "-i", "-s"));
86 return "Debian".equalsIgnoreCase(dist) || "Ubuntu".equalsIgnoreCase(dist) || "Mint".equalsIgnoreCase(dist);
87 } catch (IOException e) {
88 // lsb_release is not available on all Linux systems, so don't log at warning level
89 Main.debug(e);
90 return false;
91 }
92 }
93
94 /**
95 * Get the package name including detailed version.
96 * @param packageNames The possible package names (when a package can have different names on different distributions)
97 * @return The package name and package version if it can be identified, null otherwise
98 * @since 7314
99 */
100 public static String getPackageDetails(String... packageNames) {
101 try {
102 // CHECKSTYLE.OFF: SingleSpaceSeparator
103 boolean dpkg = Paths.get("/usr/bin/dpkg-query").toFile().exists();
104 boolean eque = Paths.get("/usr/bin/equery").toFile().exists();
105 boolean rpm = Paths.get("/bin/rpm").toFile().exists();
106 // CHECKSTYLE.ON: SingleSpaceSeparator
107 if (dpkg || rpm || eque) {
108 for (String packageName : packageNames) {
109 String[] args;
110 if (dpkg) {
111 args = new String[] {"dpkg-query", "--show", "--showformat", "${Architecture}-${Version}", packageName};
112 } else if (eque) {
113 args = new String[] {"equery", "-q", "list", "-e", "--format=$fullversion", packageName};
114 } else {
115 args = new String[] {"rpm", "-q", "--qf", "%{arch}-%{version}", packageName};
116 }
117 String version = Utils.execOutput(Arrays.asList(args));
118 if (version != null && !version.contains("not installed")) {
119 return packageName + ':' + version;
120 }
121 }
122 }
123 } catch (IOException e) {
124 Main.warn(e);
125 }
126 return null;
127 }
128
129 /**
130 * Get the Java package name including detailed version.
131 *
132 * Some Java bugs are specific to a certain security update, so in addition
133 * to the Java version, we also need the exact package version.
134 *
135 * @return The package name and package version if it can be identified, null otherwise
136 */
137 public String getJavaPackageDetails() {
138 String home = System.getProperty("java.home");
139 if (home.contains("java-8-openjdk") || home.contains("java-1.8.0-openjdk")) {
140 return getPackageDetails("openjdk-8-jre", "java-1_8_0-openjdk", "java-1.8.0-openjdk");
141 } else if (home.contains("java-9-openjdk") || home.contains("java-1.9.0-openjdk")) {
142 return getPackageDetails("openjdk-9-jre", "java-1_9_0-openjdk", "java-1.9.0-openjdk");
143 } else if (home.contains("icedtea")) {
144 return getPackageDetails("icedtea-bin");
145 } else if (home.contains("oracle")) {
146 return getPackageDetails("oracle-jdk-bin", "oracle-jre-bin");
147 }
148 return null;
149 }
150
151 /**
152 * Get the Web Start package name including detailed version.
153 *
154 * OpenJDK packages are shipped with icedtea-web package,
155 * but its version generally does not match main java package version.
156 *
157 * Simply return {@code null} if there's no separate package for Java WebStart.
158 *
159 * @return The package name and package version if it can be identified, null otherwise
160 */
161 public String getWebStartPackageDetails() {
162 if (isOpenJDK()) {
163 return getPackageDetails("icedtea-netx", "icedtea-web");
164 }
165 return null;
166 }
167
168 /**
169 * Get the Gnome ATK wrapper package name including detailed version.
170 *
171 * Debian and Ubuntu derivatives come with a pre-enabled accessibility software
172 * completely buggy that makes Swing crash in a lot of different ways.
173 *
174 * Simply return {@code null} if it's not found.
175 *
176 * @return The package name and package version if it can be identified, null otherwise
177 */
178 public String getAtkWrapperPackageDetails() {
179 if (isOpenJDK() && isDebianOrUbuntu()) {
180 return getPackageDetails("libatk-wrapper-java");
181 }
182 return null;
183 }
184
185 private String buildOSDescription() {
186 String osName = System.getProperty("os.name");
187 if ("Linux".equalsIgnoreCase(osName)) {
188 try {
189 // Try lsb_release (only available on LSB-compliant Linux systems,
190 // see https://www.linuxbase.org/lsb-cert/productdir.php?by_prod )
191 String line = exec("lsb_release -ds");
192 if (line != null && !line.isEmpty()) {
193 line = line.replaceAll("\"+", "");
194 line = line.replaceAll("NAME=", ""); // strange code for some Gentoo's
195 if (line.startsWith("Linux ")) // e.g. Linux Mint
196 return line;
197 else if (!line.isEmpty())
198 return "Linux " + line;
199 }
200 } catch (IOException e) {
201 Main.debug(e);
202 // Non LSB-compliant Linux system. List of common fallback release files: http://linuxmafia.com/faq/Admin/release-files.html
203 for (LinuxReleaseInfo info : new LinuxReleaseInfo[]{
204 new LinuxReleaseInfo("/etc/lsb-release", "DISTRIB_DESCRIPTION", "DISTRIB_ID", "DISTRIB_RELEASE"),
205 new LinuxReleaseInfo("/etc/os-release", "PRETTY_NAME", "NAME", "VERSION"),
206 new LinuxReleaseInfo("/etc/arch-release"),
207 new LinuxReleaseInfo("/etc/debian_version", "Debian GNU/Linux "),
208 new LinuxReleaseInfo("/etc/fedora-release"),
209 new LinuxReleaseInfo("/etc/gentoo-release"),
210 new LinuxReleaseInfo("/etc/redhat-release"),
211 new LinuxReleaseInfo("/etc/SuSE-release")
212 }) {
213 String description = info.extractDescription();
214 if (description != null && !description.isEmpty()) {
215 return "Linux " + description;
216 }
217 }
218 }
219 }
220 return osName;
221 }
222
223 @Override
224 public String getOSDescription() {
225 if (osDescription == null) {
226 osDescription = buildOSDescription();
227 }
228 return osDescription;
229 }
230
231 private static class LinuxReleaseInfo {
232 private final String path;
233 private final String descriptionField;
234 private final String idField;
235 private final String releaseField;
236 private final boolean plainText;
237 private final String prefix;
238
239 LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField) {
240 this(path, descriptionField, idField, releaseField, false, null);
241 }
242
243 LinuxReleaseInfo(String path) {
244 this(path, null, null, null, true, null);
245 }
246
247 LinuxReleaseInfo(String path, String prefix) {
248 this(path, null, null, null, true, prefix);
249 }
250
251 private LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField, boolean plainText, String prefix) {
252 this.path = path;
253 this.descriptionField = descriptionField;
254 this.idField = idField;
255 this.releaseField = releaseField;
256 this.plainText = plainText;
257 this.prefix = prefix;
258 }
259
260 @Override
261 public String toString() {
262 return "ReleaseInfo [path=" + path + ", descriptionField=" + descriptionField +
263 ", idField=" + idField + ", releaseField=" + releaseField + ']';
264 }
265
266 /**
267 * Extracts OS detailed information from a Linux release file (/etc/xxx-release)
268 * @return The OS detailed information, or {@code null}
269 */
270 public String extractDescription() {
271 String result = null;
272 if (path != null) {
273 Path p = Paths.get(path);
274 if (p.toFile().exists()) {
275 try (BufferedReader reader = Files.newBufferedReader(p, StandardCharsets.UTF_8)) {
276 String id = null;
277 String release = null;
278 String line;
279 while (result == null && (line = reader.readLine()) != null) {
280 if (line.contains("=")) {
281 String[] tokens = line.split("=");
282 if (tokens.length >= 2) {
283 // Description, if available, contains exactly what we need
284 if (descriptionField != null && descriptionField.equalsIgnoreCase(tokens[0])) {
285 result = Utils.strip(tokens[1]);
286 } else if (idField != null && idField.equalsIgnoreCase(tokens[0])) {
287 id = Utils.strip(tokens[1]);
288 } else if (releaseField != null && releaseField.equalsIgnoreCase(tokens[0])) {
289 release = Utils.strip(tokens[1]);
290 }
291 }
292 } else if (plainText && !line.isEmpty()) {
293 // Files composed of a single line
294 result = Utils.strip(line);
295 }
296 }
297 // If no description has been found, try to rebuild it with "id" + "release" (i.e. "name" + "version")
298 if (result == null && id != null && release != null) {
299 result = id + ' ' + release;
300 }
301 } catch (IOException e) {
302 // Ignore
303 Main.trace(e);
304 }
305 }
306 }
307 // Append prefix if any
308 if (result != null && !result.isEmpty() && prefix != null && !prefix.isEmpty()) {
309 result = prefix + result;
310 }
311 if (result != null)
312 result = result.replaceAll("\"+", "");
313 return result;
314 }
315 }
316
317 /**
318 * Get the dot directory <code>~/.josm</code>.
319 * @return the dot directory
320 */
321 private static File getDotDirectory() {
322 String dirName = "." + Main.pref.getJOSMDirectoryBaseName().toLowerCase(Locale.ENGLISH);
323 return new File(System.getProperty("user.home"), dirName);
324 }
325
326 /**
327 * Returns true if the dot directory should be used for storing preferences,
328 * cache and user data.
329 * Currently this is the case, if the dot directory already exists.
330 * @return true if the dot directory should be used
331 */
332 private static boolean useDotDirectory() {
333 return getDotDirectory().exists();
334 }
335
336 @Override
337 public File getDefaultCacheDirectory() {
338 if (useDotDirectory()) {
339 return new File(getDotDirectory(), "cache");
340 } else {
341 String xdgCacheDir = System.getenv("XDG_CACHE_HOME");
342 if (xdgCacheDir != null && !xdgCacheDir.isEmpty()) {
343 return new File(xdgCacheDir, Main.pref.getJOSMDirectoryBaseName());
344 } else {
345 return new File(System.getProperty("user.home") + File.separator +
346 ".cache" + File.separator + Main.pref.getJOSMDirectoryBaseName());
347 }
348 }
349 }
350
351 @Override
352 public File getDefaultPrefDirectory() {
353 if (useDotDirectory()) {
354 return getDotDirectory();
355 } else {
356 String xdgConfigDir = System.getenv("XDG_CONFIG_HOME");
357 if (xdgConfigDir != null && !xdgConfigDir.isEmpty()) {
358 return new File(xdgConfigDir, Main.pref.getJOSMDirectoryBaseName());
359 } else {
360 return new File(System.getProperty("user.home") + File.separator +
361 ".config" + File.separator + Main.pref.getJOSMDirectoryBaseName());
362 }
363 }
364 }
365
366 @Override
367 public File getDefaultUserDataDirectory() {
368 if (useDotDirectory()) {
369 return getDotDirectory();
370 } else {
371 String xdgDataDir = System.getenv("XDG_DATA_HOME");
372 if (xdgDataDir != null && !xdgDataDir.isEmpty()) {
373 return new File(xdgDataDir, Main.pref.getJOSMDirectoryBaseName());
374 } else {
375 return new File(System.getProperty("user.home") + File.separator +
376 ".local" + File.separator + "share" + File.separator + Main.pref.getJOSMDirectoryBaseName());
377 }
378 }
379 }
380
381 @Override
382 public List<File> getDefaultProj4NadshiftDirectories() {
383 return Arrays.asList(new File("/usr/local/share/proj"), new File("/usr/share/proj"));
384 }
385}
Note: See TracBrowser for help on using the repository browser.