1 /*
   2  * Copyright (c) 2011, 2018, 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.javafx.application;
  27 
  28 import java.awt.Desktop;
  29 import java.io.File;
  30 import java.net.URI;
  31 import javafx.application.Application;
  32 
  33 public abstract class HostServicesDelegate {
  34 
  35     public static HostServicesDelegate getInstance(final Application app) {
  36         return StandaloneHostService.getInstance(app);
  37     }
  38 
  39     protected HostServicesDelegate() {
  40     }
  41 
  42     public abstract String getCodeBase();
  43 
  44     public abstract String getDocumentBase();
  45 
  46     public abstract void showDocument(String uri);
  47 
  48     // StandaloneHostService implementation
  49     private static class StandaloneHostService extends HostServicesDelegate {
  50 
  51         private static HostServicesDelegate instance = null;
  52 
  53         private Class appClass = null;
  54 
  55         public static HostServicesDelegate getInstance(Application app) {
  56             synchronized (StandaloneHostService.class) {
  57                 if (instance == null) {
  58                     instance = new StandaloneHostService(app);
  59                 }
  60                 return instance;
  61             }
  62         }
  63 
  64         private StandaloneHostService(Application app) {
  65              appClass = app.getClass();
  66         }
  67 
  68         @Override
  69         public String getCodeBase() {
  70             // If the application was launched in standalone mode, this method
  71             // returns the directory containing the application jar file.
  72             // If the application is not packaged in a jar file, this method
  73             // returns the empty string.
  74             String theClassFile = appClass.getName();
  75             int idx = theClassFile.lastIndexOf(".");
  76             if (idx >= 0) {
  77                 // Strip off package name prefix in class name if exists
  78                 // getResoruce will automatically add in package name during
  79                 // lookup; see Class.getResource javadoc for more details
  80                 theClassFile = theClassFile.substring(idx + 1);
  81             }
  82             theClassFile = theClassFile + ".class";
  83 
  84             String classUrlString = appClass.getResource(theClassFile).toString();
  85             if (!classUrlString.startsWith("jar:file:") ||
  86                     classUrlString.indexOf("!") == -1) {
  87                 return "";
  88             }
  89             // Strip out the "jar:" and everything after and including the "!"
  90             String urlString = classUrlString.substring(4,
  91                     classUrlString.lastIndexOf("!"));
  92             File jarFile = null;
  93             try {
  94                 jarFile = new File(new URI(urlString).getPath());
  95             } catch (Exception e) {
  96                 // should not happen
  97             }
  98             if (jarFile != null) {
  99                 String codebase = jarFile.getParent();
 100                 if (codebase != null) {
 101                     return toURIString(codebase);
 102                 }
 103             }
 104 
 105             return "";
 106         }
 107 
 108         private String toURIString(String filePath) {
 109             try {
 110                 return new File(filePath).toURI().toString();
 111             } catch (Exception e) {
 112                 // should not happen
 113                 // dump stack for debug purpose
 114                 e.printStackTrace();
 115             }
 116             return "";
 117         }
 118 
 119         @Override public String getDocumentBase() {
 120             // If the application was launched in standalone mode,
 121             // this method returns the URI of the current directory.
 122             return toURIString(System.getProperty("user.dir"));
 123         }
 124 
 125         static final String[] browsers = {"google-chrome", "firefox", "opera",
 126             "konqueror", "mozilla"};
 127 
 128         @Override
 129         public void showDocument(final String uri) {
 130             String osName = System.getProperty("os.name");
 131             try {
 132                 if (osName.startsWith("Mac OS")) {
 133                     Desktop.getDesktop().browse(URI.create(uri));
 134                 } else if (osName.startsWith("Windows")) {
 135                     Runtime.getRuntime().exec(
 136                             "rundll32 url.dll,FileProtocolHandler " + uri);
 137                 } else { //assume Unix or Linux
 138                     String browser = null;
 139                     for (String b : browsers) {
 140                         if (browser == null && Runtime.getRuntime().exec(
 141                                 new String[]{"which", b}).getInputStream().read() != -1) {
 142                             Runtime.getRuntime().exec(new String[]{browser = b, uri});
 143                         }
 144                     }
 145                     if (browser == null) {
 146                         throw new Exception("No web browser found");
 147                     }
 148                 }
 149             } catch (Exception e) {
 150                 // should not happen
 151                 // dump stack for debug purpose
 152                 e.printStackTrace();
 153             }
 154         }
 155     }
 156 }