jdk/src/share/classes/com/sun/servicetag/BrowserSupport.java
changeset 1327 912486b03832
child 5506 202f599c92aa
equal deleted inserted replaced
1324:f1f7222489a4 1327:912486b03832
       
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any 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                         InternalError x =
       
    81                             new InternalError("Desktop.getDesktop() method not found");
       
    82                         x.initCause(e);
       
    83                     } catch (InvocationTargetException e) {
       
    84                         // browser not supported
       
    85                         if (Util.isVerbose()) {
       
    86                             e.printStackTrace();
       
    87                         }
       
    88                     }
       
    89                 }
       
    90             };
       
    91             // set it to daemon, so that the vm will exit.
       
    92             xthread.setDaemon(true);
       
    93             xthread.start();
       
    94             try {
       
    95                 xthread.join(5 * 1000);
       
    96             } catch (InterruptedException ie) {
       
    97                 // ignore the exception
       
    98             }
       
    99             if (result.booleanValue()) {
       
   100                 desktopObj = getDesktopM.invoke(null);
       
   101                 result = (Boolean) isSupportedMethod.invoke(desktopObj, browseField.get(null));
       
   102                 supported = result.booleanValue();
       
   103             }
       
   104         } catch (ClassNotFoundException e) {
       
   105             // browser not supported
       
   106             if (Util.isVerbose()) {
       
   107                 e.printStackTrace();
       
   108             }
       
   109         } catch (NoSuchMethodException e) {
       
   110             // browser not supported
       
   111             if (Util.isVerbose()) {
       
   112                 e.printStackTrace();
       
   113             }
       
   114         } catch (NoSuchFieldException e) {
       
   115             // browser not supported
       
   116             if (Util.isVerbose()) {
       
   117                 e.printStackTrace();
       
   118             }
       
   119         } catch (IllegalAccessException e) {
       
   120             // should never reach here
       
   121             InternalError x =
       
   122                     new InternalError("Desktop.getDesktop() method not found");
       
   123             x.initCause(e);
       
   124             throw x;
       
   125         } catch (InvocationTargetException e) {
       
   126             // browser not supported
       
   127             if (Util.isVerbose()) {
       
   128                 e.printStackTrace();
       
   129             }
       
   130         }
       
   131         isBrowseSupported = supported;
       
   132         browseMethod = browseM;
       
   133         desktop = desktopObj;
       
   134     }
       
   135 
       
   136     static boolean isSupported() {
       
   137         initX();
       
   138         return isBrowseSupported;
       
   139     }
       
   140 
       
   141     /**
       
   142      * Launches the default browser to display a {@code URI}.
       
   143      * If the default browser is not able to handle the specified
       
   144      * {@code URI}, the application registered for handling
       
   145      * {@code URIs} of the specified type is invoked. The application
       
   146      * is determined from the protocol and path of the {@code URI}, as
       
   147      * defined by the {@code URI} class.
       
   148      * <p>
       
   149      * This method calls the Desktop.getDesktop().browse() method.
       
   150      * <p>
       
   151      * @param uri the URI to be displayed in the user default browser
       
   152      *
       
   153      * @throws NullPointerException if {@code uri} is {@code null}
       
   154      * @throws UnsupportedOperationException if the current platform
       
   155      * does not support the {@link Desktop.Action#BROWSE} action
       
   156      * @throws IOException if the user default browser is not found,
       
   157      * or it fails to be launched, or the default handler application
       
   158      * failed to be launched
       
   159      * @throws IllegalArgumentException if the necessary permissions
       
   160      * are not available and the URI can not be converted to a {@code URL}
       
   161      */
       
   162     static void browse(URI uri) throws IOException {
       
   163         if (uri == null) {
       
   164             throw new NullPointerException("null uri");
       
   165         }
       
   166         if (!isSupported()) {
       
   167             throw new UnsupportedOperationException("Browse operation is not supported");
       
   168         }
       
   169 
       
   170         // Call Desktop.browse() method
       
   171         try {
       
   172             if (Util.isVerbose()) {
       
   173                 System.out.println("desktop: " + desktop + ":browsing..." + uri);
       
   174             }
       
   175             browseMethod.invoke(desktop, uri);
       
   176         } catch (IllegalAccessException e) {
       
   177             // should never reach here
       
   178             InternalError x =
       
   179                 new InternalError("Desktop.getDesktop() method not found");
       
   180             x.initCause(e);
       
   181                 throw x;
       
   182         } catch (InvocationTargetException e) {
       
   183             Throwable x = e.getCause();
       
   184             if (x != null) {
       
   185                 if (x instanceof UnsupportedOperationException) {
       
   186                     throw (UnsupportedOperationException) x;
       
   187                 } else if (x instanceof IllegalArgumentException) {
       
   188                     throw (IllegalArgumentException) x;
       
   189                 } else if (x instanceof IOException) {
       
   190                     throw (IOException) x;
       
   191                 } else if (x instanceof SecurityException) {
       
   192                     throw (SecurityException) x;
       
   193                 } else {
       
   194                     // ignore
       
   195                 }
       
   196             }
       
   197         }
       
   198     }
       
   199 }