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

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

blind attempt to support Gentoo in Java package info retrieval

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