View Javadoc
1   /*
2    * #%L
3    * Nuiton Utils
4    * %%
5    * Copyright (C) 2004 - 2012 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  package org.nuiton.util;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  import java.awt.Desktop;
28  import java.io.File;
29  import java.io.IOException;
30  import java.net.URI;
31  
32  /**
33   * Utility class for methods to interact with Desktop Environment
34   *
35   * @author Jean Couteau - couteau@codelutin.com
36   * @since 2.4.3
37   */
38  public class DesktopUtil {
39  
40      /** Logger. */
41      private static final Log log = LogFactory.getLog(DesktopUtil.class);
42  
43      /**
44       * Method to open an URI in the user default web browser. It uses the Java
45       * Desktop API on Windows and Gnome environment and xdg-open on other
46       * platforms (non-gnome linux distribution for example).
47       *
48       * A Bug report have been opened in 2006 so that java.awt.Desktop can
49       * support all environments but it is not fixed yet :
50       * http://bugs.sun.com/view_bug.do?bug_id=6486393 this utility method should
51       * be removed when the bug is fixed.
52       *
53       * @param uri the URI to open
54       */
55      public static void browse(URI uri) {
56          boolean useXdg = true;
57          try {
58              if (Desktop.isDesktopSupported()) {
59                  Desktop desktop = Desktop.getDesktop();
60                  if (desktop.isSupported(Desktop.Action.BROWSE)) {
61                      useXdg = false;
62                      desktop.browse(uri);
63                  }
64              }
65          } catch (IOException ioe) {
66              // ignore
67          }
68          
69          if (useXdg) {
70              if (log.isDebugEnabled()) {
71                  log.debug("Desktop API not supported, launching xdg-open");
72              }
73              ProcessBuilder pb = new ProcessBuilder("xdg-open", uri.toString());
74              try {
75                  pb.start();
76              } catch (IOException e) {
77                  if (log.isDebugEnabled()) {
78                      log.debug("Could not launch browser, there is maybe no " +
79                                "default browser configured on the system", e);
80                  }
81              }
82          }
83      }
84  
85      /**
86       * Method to open an URI in the user default open client. It uses the Java
87       * Desktop API on Windows and Gnome environment and xdg-open on other
88       * platforms (non-gnome linux distribution for example).
89       *
90       * A Bug report have been opened in 2006 so that java.awt.Desktop can
91       * support all environments but it is not fixed yet :
92       * http://bugs.sun.com/view_bug.do?bug_id=6486393 this utility method should
93       * be removed when the bug is fixed.
94       *
95       * @param file the file to open
96       */
97      public static void open(File file) {
98          boolean useXdg = true;
99          try {
100             if (Desktop.isDesktopSupported()) {
101                 Desktop desktop = Desktop.getDesktop();
102                 if (desktop.isSupported(Desktop.Action.OPEN)) {
103                     useXdg = false;
104                     desktop.open(file);
105                 }
106             }
107         } catch (IOException ioe) {
108         
109         }
110         
111         if (useXdg) {
112             if (log.isDebugEnabled()) {
113                 log.debug("Desktop API not supported, launching xdg-open");
114             }
115 
116             ProcessBuilder pb = new ProcessBuilder("xdg-open", file.toURI().toString());
117             try {
118                 pb.start();
119             } catch (IOException e) {
120                 if (log.isDebugEnabled()) {
121                     log.debug("Could not open file, there is maybe no " +
122                               "default viewer configured on the system", e);
123                 }
124             }
125 
126         }
127 
128     }
129     
130     /**
131      * Method to open an URI in the user default mail client. It uses the Java
132      * Desktop API on Windows and Gnome environment and xdg-email on other
133      * platforms (non-gnome linux distribution for example).
134      *
135      * A Bug report have been opened in 2006 so that java.awt.Desktop can
136      * support all environments but it is not fixed yet :
137      * http://bugs.sun.com/view_bug.do?bug_id=6486393 this utility method should
138      * be removed when the bug is fixed.
139      *
140      * @param uri the uri to open
141      */
142     public static void mail(URI uri) {
143         boolean useXdg = true;
144         try {
145             if (Desktop.isDesktopSupported()) {
146                 Desktop desktop = Desktop.getDesktop();
147                 if (desktop.isSupported(Desktop.Action.MAIL)) {
148                     useXdg = false;
149                     desktop.mail(uri);
150                 }
151             }
152         } catch (IOException ioe) {
153         
154         }
155         
156         if (useXdg) {
157             if (log.isDebugEnabled()) {
158                 log.debug("Desktop API not supported, launching xdg-open");
159             }
160 
161             ProcessBuilder pb = new ProcessBuilder("xdg-email", uri.toString());
162             try {
163                 pb.start();
164             } catch (IOException e) {
165                 if (log.isDebugEnabled()) {
166                     log.debug("Could not open file, there is maybe no " +
167                               "default viewer configured on the system", e);
168                 }
169             }
170 
171         }
172 
173     }
174 }