changeset 175:5a06b662ad9b

Alternate means of finding browsers This patch makes netx look for the users' default browser in this order: xdg-open, $BROWSER, and finally prompting the user for the command. To remain backwards compatible, this is only done if the user has not previously specified a default browser. 2011-03-24 Omair Majid <omajid@redhat.com> * netx/net/sourceforge/jnlp/resources/Messages.properties: Add RBrowserLocationPromptTitle, RBrowserLocationPromptMessage and RBrowserLocationPromptMessageWithReason. * netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java (isWindows): New method. Moved from XBasicService. (isUnix): New method. * netx/net/sourceforge/jnlp/services/XBasicService (initialize): Call initializeBrowserCommand. (initializeBrowserCommand): New method. (posixCommandExists): New method. (isWindows): Moved to JNLPRuntime.
author Omair Majid <omajid@redhat.com>
date Thu, 24 Mar 2011 08:54:12 -0400
parents 24da5acc49a2
children 92486f15be36
files ChangeLog NEWS netx/net/sourceforge/jnlp/resources/Messages.properties netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java netx/net/sourceforge/jnlp/services/XBasicService.java
diffstat 5 files changed, 110 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Mar 23 16:38:33 2011 -0400
+++ b/ChangeLog	Thu Mar 24 08:54:12 2011 -0400
@@ -1,3 +1,17 @@
+2011-03-24  Omair Majid  <omajid@redhat.com>
+
+	* netx/net/sourceforge/jnlp/resources/Messages.properties: Add
+	RBrowserLocationPromptTitle, RBrowserLocationPromptMessage and
+	RBrowserLocationPromptMessageWithReason.
+	* netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java
+	(isWindows): New method. Moved from XBasicService.
+	(isUnix): New method.
+	* netx/net/sourceforge/jnlp/services/XBasicService
+	(initialize): Call initializeBrowserCommand.
+	(initializeBrowserCommand): New method.
+	(posixCommandExists): New method.
+	(isWindows): Moved to JNLPRuntime.
+
 2011-03-23  Denis Lila  <dlila@redhat.com>
 
 	* netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java
--- a/NEWS	Wed Mar 23 16:38:33 2011 -0400
+++ b/NEWS	Thu Mar 24 08:54:12 2011 -0400
@@ -22,6 +22,7 @@
   - RH677772: NoSuchAlgorithmException using SSL/TLS in javaws
 * NetX
   - Use Firefox's proxy settings if possible
+  - The user's default browser (determined from xdg-open or $BROWSER) is used
   - RH669942: javaws fails to download version/packed files (missing support for jnlp.packEnabled and jnlp.versionEnabled)
   - PR658: now jnlp.packEnabled works with applets.
 * Plugin
--- a/netx/net/sourceforge/jnlp/resources/Messages.properties	Wed Mar 23 16:38:33 2011 -0400
+++ b/netx/net/sourceforge/jnlp/resources/Messages.properties	Thu Mar 24 08:54:12 2011 -0400
@@ -154,6 +154,9 @@
 RPRoxyPacNotSupported=Using Proxy Auto Config (PAC) files is not supported.
 RProxyFirefoxNotFound=Unable to use Firefox's proxy settings. Using "DIRECT" as proxy type.
 RProxyFirefoxOptionNotImplemented=Browser proxy option "{0}" ({1}) not supported yet.
+RBrowserLocationPromptTitle=Browser Location
+RBrowserLocationPromptMessage=Specify Browser Location
+RBrowserLocationPromptMessageWithReason=Specify Browser Location (the browser command "{0}" is invalid).
 
 # Boot options, message should be shorter than this ---------------->
 BOUsage=javaws [-run-options] <jnlp file>
--- a/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java	Wed Mar 23 16:38:33 2011 -0400
+++ b/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java	Thu Mar 24 08:54:12 2011 -0400
@@ -620,6 +620,23 @@
         }
     }
 
+    /**
+     * @return true if running on Windows
+     */
+    public static boolean isWindows() {
+        String os = System.getProperty("os.name");
+        return (os != null && os.startsWith("Windows"));
+    }
+
+    /**
+     * @return true if running on a Unix or Unix-like system (including Linux
+     * and *BSD)
+     */
+    public static boolean isUnix() {
+        String sep = System.getProperty("file.separator");
+        return (sep != null && sep.equals("/"));
+    }
+
     public static void setInitialArgments(List<String> args) {
         checkInitialized();
         SecurityManager securityManager = System.getSecurityManager();
--- a/netx/net/sourceforge/jnlp/services/XBasicService.java	Wed Mar 23 16:38:33 2011 -0400
+++ b/netx/net/sourceforge/jnlp/services/XBasicService.java	Thu Mar 24 08:54:12 2011 -0400
@@ -16,6 +16,8 @@
 
 package net.sourceforge.jnlp.services;
 
+import static net.sourceforge.jnlp.runtime.Translator.R;
+
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -190,9 +192,47 @@
         if (initialized)
             return;
         initialized = true;
+        initializeBrowserCommand();
+        if (JNLPRuntime.isDebug()) {
+            System.out.println("browser is " + command);
+        }
+    }
 
-        if (isWindows()) {
+    /**
+     * Initializes {@link #command} to launch a browser
+     */
+    private void initializeBrowserCommand() {
+        if (JNLPRuntime.isWindows()) {
             command = "rundll32 url.dll,FileProtocolHandler ";
+        } else if (JNLPRuntime.isUnix()) {
+            DeploymentConfiguration config = JNLPRuntime.getConfiguration();
+            command = config.getProperty(DeploymentConfiguration.KEY_BROWSER_PATH);
+            if (command != null) {
+                return;
+            }
+
+            if (posixCommandExists("xdg-open")) {
+                command = "xdg-open";
+                return;
+            }
+
+            if (posixCommandExists(System.getenv("BROWSER"))) {
+                command = System.getenv("BROWSER");
+                return;
+            }
+
+            while (true) {
+                command = promptForCommand(command);
+                if (command != null && posixCommandExists(command)) {
+                    config.setProperty(DeploymentConfiguration.KEY_BROWSER_PATH, command);
+                    try {
+                        config.save();
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                    break;
+                }
+            }
         } else {
             DeploymentConfiguration config = JNLPRuntime.getConfiguration();
             command = config.getProperty(DeploymentConfiguration.KEY_BROWSER_PATH);
@@ -212,18 +252,44 @@
         }
     }
 
-    private boolean isWindows() {
-        String os = System.getProperty("os.name");
-        if (os != null && os.startsWith("Windows"))
-            return true;
-        else
+    /**
+     * Check that a command exists on a posix-like system
+     * @param command the command to check
+     * @return true if the command exists
+     */
+    private boolean posixCommandExists(String command) {
+        if (command == null || command.trim().length() == 0) {
+            return false;
+        }
+
+        command = command.trim();
+        if (command.contains("\n") || command.contains("\r")) {
             return false;
+        }
+
+        try {
+            Process p = Runtime.getRuntime().exec(new String[] { "bash", "-c", "type " + command });
+            p.waitFor();
+            return (p.exitValue() == 0);
+        } catch (IOException e) {
+            e.printStackTrace();
+            return false;
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+            return false;
+        }
     }
 
-    private String promptForCommand(String cmd) {
+    private String promptForCommand(String previousCommand) {
+        String message = null;
+        if (previousCommand == null) {
+            message = R("RBrowserLocationPromptMessage");
+        } else {
+            message = R("RBrowserLocationPromptMessageWithReason", previousCommand);
+        }
         return JOptionPane.showInputDialog(new JPanel(),
-                                           "Browser Location:",
-                                           "Specify Browser Location",
+                                           R("RBrowserLocationPromptTitle"),
+                                           message,
                                            JOptionPane.PLAIN_MESSAGE
                                           );
     }