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

Last change on this file was 19263, checked in by stoecker, 18 months ago

indicate possible shortcuts for parser

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