1 /*
   2  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.servicetag;
  27 
  28 import java.lang.reflect.Field;
  29 import java.lang.reflect.Method;
  30 import java.lang.reflect.InvocationTargetException;
  31 import java.io.IOException;
  32 import java.net.URI;
  33 
  34 /**
  35  * BrowserSupport class.
  36  *
  37  * The implementation of the com.sun.servicetag API needs to be
  38  * compiled with JDK 5 as well since the consumer of this API
  39  * may require to support JDK 5 (e.g. NetBeans).
  40  *
  41  * The Desktop.browse() method can be backported in this class
  42  * if needed.  The current implementation only supports JDK 6.
  43  */
  44 class BrowserSupport {
  45     private static boolean isBrowseSupported = false;
  46     private static Method browseMethod = null;
  47     private static Object desktop = null;
  48     private static volatile Boolean result = false;
  49 
  50 
  51     private static void initX() {
  52         if  (desktop != null) {
  53             return;
  54         }
  55         boolean supported = false;
  56         Method browseM = null;
  57         Object desktopObj = null;
  58         try {
  59             // Determine if java.awt.Desktop is supported
  60             Class<?> desktopCls = Class.forName("java.awt.Desktop", true, null);
  61             Method getDesktopM = desktopCls.getMethod("getDesktop");
  62             browseM = desktopCls.getMethod("browse", URI.class);
  63 
  64             Class<?> actionCls = Class.forName("java.awt.Desktop$Action", true, null);
  65             final Method isDesktopSupportedMethod = desktopCls.getMethod("isDesktopSupported");
  66             Method isSupportedMethod = desktopCls.getMethod("isSupported", actionCls);
  67             Field browseField = actionCls.getField("BROWSE");
  68             // isDesktopSupported calls getDefaultToolkit which can block
  69             // infinitely, see 6636099 for details, to workaround we call
  70             // in a  thread and time it out, noting that the issue is specific
  71             // to X11, it does not hurt for Windows.
  72             Thread xthread = new Thread() {
  73                 public void run() {
  74                     try {
  75                         // support only if Desktop.isDesktopSupported() and
  76                         // Desktop.isSupported(Desktop.Action.BROWSE) return true.
  77                         result = (Boolean) isDesktopSupportedMethod.invoke(null);
  78                     } catch (IllegalAccessException e) {
  79                         // should never reach here
  80                         throw new InternalError("Desktop.getDesktop() method not found", e);
  81                     } catch (InvocationTargetException e) {
  82                         // browser not supported
  83                         if (Util.isVerbose()) {
  84                             e.printStackTrace();
  85                         }
  86                     }
  87                 }
  88             };
  89             // set it to daemon, so that the vm will exit.
  90             xthread.setDaemon(true);
  91             xthread.start();
  92             try {
  93                 xthread.join(5 * 1000);
  94             } catch (InterruptedException ie) {
  95                 // ignore the exception
  96             }
  97             if (result.booleanValue()) {
  98                 desktopObj = getDesktopM.invoke(null);
  99                 result = (Boolean) isSupportedMethod.invoke(desktopObj, browseField.get(null));
 100                 supported = result.booleanValue();
 101             }
 102         } catch (IllegalAccessException e) {
 103             // should never reach here
 104             throw new InternalError("Desktop.getDesktop() method not found", e);
 105         } catch (ReflectiveOperationException e) {
 106             // browser not supported
 107             if (Util.isVerbose()) {
 108                 e.printStackTrace();
 109             }
 110         }
 111         isBrowseSupported = supported;
 112         browseMethod = browseM;
 113         desktop = desktopObj;
 114     }
 115 
 116     static boolean isSupported() {
 117         initX();
 118         return isBrowseSupported;
 119     }
 120 
 121     /**
 122      * Launches the default browser to display a {@code URI}.
 123      * If the default browser is not able to handle the specified
 124      * {@code URI}, the application registered for handling
 125      * {@code URIs} of the specified type is invoked. The application
 126      * is determined from the protocol and path of the {@code URI}, as
 127      * defined by the {@code URI} class.
 128      * <p>
 129      * This method calls the Desktop.getDesktop().browse() method.
 130      * <p>
 131      * @param uri the URI to be displayed in the user default browser
 132      *
 133      * @throws NullPointerException if {@code uri} is {@code null}
 134      * @throws UnsupportedOperationException if the current platform
 135      * does not support the {@link Desktop.Action#BROWSE} action
 136      * @throws IOException if the user default browser is not found,
 137      * or it fails to be launched, or the default handler application
 138      * failed to be launched
 139      * @throws IllegalArgumentException if the necessary permissions
 140      * are not available and the URI can not be converted to a {@code URL}
 141      */
 142     static void browse(URI uri) throws IOException {
 143         if (uri == null) {
 144             throw new NullPointerException("null uri");
 145         }
 146         if (!isSupported()) {
 147             throw new UnsupportedOperationException("Browse operation is not supported");
 148         }
 149 
 150         // Call Desktop.browse() method
 151         try {
 152             if (Util.isVerbose()) {
 153                 System.out.println("desktop: " + desktop + ":browsing..." + uri);
 154             }
 155             browseMethod.invoke(desktop, uri);
 156         } catch (IllegalAccessException e) {
 157             // should never reach here
 158             throw new InternalError("Desktop.getDesktop() method not found", e);
 159         } catch (InvocationTargetException e) {
 160             Throwable x = e.getCause();
 161             if (x != null) {
 162                 if (x instanceof UnsupportedOperationException) {
 163                     throw (UnsupportedOperationException) x;
 164                 } else if (x instanceof IllegalArgumentException) {
 165                     throw (IllegalArgumentException) x;
 166                 } else if (x instanceof IOException) {
 167                     throw (IOException) x;
 168                 } else if (x instanceof SecurityException) {
 169                     throw (SecurityException) x;
 170                 } else {
 171                     // ignore
 172                 }
 173             }
 174         }
 175     }
 176 }