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

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

fix some sonar issues recently introduced

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