1 /*
   2  * Copyright (c) 2014, 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.oracle.bundlers;
  27 
  28 import com.oracle.bundlers.windows.WindowsBundlerParam;
  29 import com.sun.javafx.tools.packager.Log;
  30 import com.sun.javafx.tools.packager.bundlers.ConfigException;
  31 import com.sun.javafx.tools.packager.bundlers.IOUtils;
  32 import com.sun.javafx.tools.packager.bundlers.RelativeFileSet;
  33 
  34 import java.io.ByteArrayOutputStream;
  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.io.InputStream;
  38 import java.net.URL;
  39 import java.text.MessageFormat;
  40 import java.util.*;
  41 
  42 import static com.oracle.bundlers.StandardBundlerParam.*;
  43 
  44 public abstract class AbstractBundler implements Bundler {
  45 
  46     private static final ResourceBundle I18N =
  47             ResourceBundle.getBundle("com.oracle.bundlers.AbstractBundler");
  48 
  49     public static final BundlerParamInfo<File> IMAGES_ROOT = new WindowsBundlerParam<>(
  50             I18N.getString("param.images-root.name"),
  51             I18N.getString("param.images-root.description"),
  52             "imagesRoot",
  53             File.class,
  54             params -> new File(BUILD_ROOT.fetchFrom(params), "images"),
  55             (s, p) -> null);
  56 
  57     //do not use file separator -
  58     // we use it for classpath lookup and there / are not platform specific
  59     public final static String BUNDLER_PREFIX = "package/";
  60 
  61     protected static final String JAVAFX_LAUNCHER_CLASS = "com.javafx.main.Main";
  62 
  63     protected Class baseResourceLoader = null;
  64     
  65     //helper method to test if required files are present in the runtime
  66     public void testRuntime(Map<String, ? super Object> p, String[] file) throws ConfigException {
  67         RelativeFileSet runtime = RUNTIME.fetchFrom(p);
  68         if (runtime == null) {
  69             return; //null runtime is ok (request to use system)
  70         }
  71         Set<String> rfiles = runtime.getIncludedFiles();
  72         for (String aFile : file) {
  73             if (rfiles.contains(aFile)) {
  74                 return;
  75             }
  76         }
  77         throw new ConfigException(
  78                 MessageFormat.format(I18N.getString("error.jre-missing-file"), Arrays.toString(file)),
  79                 I18N.getString("error.jre-missing-file.advice"));
  80     }
  81 
  82     protected void fetchResource(
  83             String publicName, String category,
  84             String defaultName, File result, boolean verbose)
  85             throws IOException {
  86         URL u = locateResource(publicName, category, defaultName, verbose);
  87         if (u != null) {
  88             IOUtils.copyFromURL(u, result);
  89         } else {
  90             if (verbose) {
  91                 Log.info(MessageFormat.format(I18N.getString("message.using-default-resource"), category == null ? "" : "[" + category + "] ", publicName));
  92             }
  93         }
  94     }
  95 
  96     protected void fetchResource(
  97             String publicName, String category,
  98             File defaultFile, File result, boolean verbose)
  99             throws IOException {
 100         URL u = locateResource(publicName, category, null, verbose);
 101         if (u != null) {
 102             IOUtils.copyFromURL(u, result);
 103         } else {
 104             IOUtils.copyFile(defaultFile, result);
 105             if (verbose) {
 106                 Log.info(MessageFormat.format(I18N.getString("message.using-default-resource-from-file"), category == null ? "" : "[" + category + "] ", defaultFile.getAbsoluteFile()));
 107             }
 108         }
 109     }
 110 
 111     private URL locateResource(String publicName, String category,
 112                                String defaultName, boolean verbose) throws IOException {
 113         URL u = null;
 114         boolean custom = false;
 115         if (publicName != null) {
 116             u = baseResourceLoader.getClassLoader().getResource(publicName);
 117             custom = (u != null);
 118         }
 119         if (u == null && defaultName != null) {
 120             u = baseResourceLoader.getResource(defaultName);
 121         }
 122         String msg = null;
 123         if (custom) {
 124             msg = MessageFormat.format(I18N.getString("message.using-custom-resource-from-classpath"), category == null ? "" : "[" + category + "] ", publicName);
 125         } else if (u != null) {
 126             msg = MessageFormat.format(I18N.getString("message.using-default-resource-from-classpath"), category == null ? "" : "[" + category + "] ", publicName);
 127         }
 128         if (verbose && u != null) {
 129             Log.info(msg);
 130         }
 131         return u;
 132     }
 133 
 134     protected String preprocessTextResource(String publicName, String category,
 135                                             String defaultName, Map<String, String> pairs,
 136                                             boolean verbose) throws IOException {
 137         URL u = locateResource(publicName, category, defaultName, verbose);
 138         InputStream inp = u.openStream();
 139         if (inp == null) {
 140             throw new RuntimeException("Jar corrupt? No "+defaultName+" resource!");
 141         }
 142 
 143         //read fully into memory
 144         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 145         byte[] buffer = new byte[1024];
 146         int length;
 147         while ((length = inp.read(buffer)) != -1) {
 148             baos.write(buffer, 0, length);
 149         }
 150 
 151         //substitute
 152         String result = new String(baos.toByteArray());
 153         for (Map.Entry<String, String> e : pairs.entrySet()) {
 154             if (e.getValue() != null) {
 155                 result = result.replace(e.getKey(), e.getValue());
 156             }
 157         }
 158         return result;
 159     }
 160 
 161     @Override
 162     public String toString() {
 163         return getName();
 164     }
 165 }