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

Last change on this file since 14253 was 14177, checked in by Don-vip, 6 years ago

see #16666 - add workaround against ATK wrapper failure on Debian, Ubuntu and Mint

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