1 /*
   2  * Copyright (c) 2014, 2019, 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 jdk.jpackage.internal;
  27 
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.File;
  30 import java.io.FileInputStream;
  31 import java.io.BufferedInputStream;
  32 import java.io.IOException;
  33 import java.io.InputStream;
  34 import java.nio.file.StandardCopyOption;
  35 import java.nio.file.Files;
  36 import java.text.MessageFormat;
  37 import java.util.Map;
  38 import java.util.ResourceBundle;
  39 
  40 import jdk.jpackage.internal.resources.ResourceLocator;
  41 
  42 /**
  43  * AbstractBundler
  44  *
  45  * This is the base class all Bundlers extend from.
  46  * It contains methods and parameters common to all Bundlers.
  47  * The concrete implementations are in the platform specific Bundlers.
  48  */
  49 public abstract class AbstractBundler implements Bundler {
  50 
  51     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  52             "jdk.jpackage.internal.resources.MainResources");
  53 
  54     public static final BundlerParamInfo<File> IMAGES_ROOT =
  55             new StandardBundlerParam<>(
  56             "imagesRoot",
  57             File.class,
  58             params -> new File(
  59                 StandardBundlerParam.TEMP_ROOT.fetchFrom(params), "images"),
  60             (s, p) -> null);
  61 
  62     public InputStream getResourceAsStream(String name) {
  63         return ResourceLocator.class.getResourceAsStream(name);
  64     }
  65 
  66     protected void fetchResource(String publicName, String category,
  67             String defaultName, File result, boolean verbose, File publicRoot)
  68             throws IOException {
  69 
  70         InputStream is = streamResource(publicName, category,
  71                 defaultName, verbose, publicRoot);
  72         if (is != null) {
  73             try {
  74                 Files.copy(is, result.toPath(),
  75                         StandardCopyOption.REPLACE_EXISTING);
  76             } finally {
  77                 is.close();
  78             }
  79         } else {
  80             if (verbose) {
  81                 Log.verbose(MessageFormat.format(I18N.getString(
  82                         "message.no-default-resource"),
  83                         defaultName == null ? "" : defaultName,
  84                         category == null ? "" : "[" + category + "] ",
  85                         publicName));
  86             }
  87         }
  88     }
  89 
  90     protected void fetchResource(String publicName, String category,
  91             File defaultFile, File result, boolean verbose, File publicRoot)
  92             throws IOException {
  93 
  94         InputStream is = streamResource(publicName, category,
  95                 null, verbose, publicRoot);
  96         if (is != null) {
  97             try {
  98                 Files.copy(is, result.toPath());
  99             } finally {
 100                 is.close();
 101             }
 102         } else {
 103             IOUtils.copyFile(defaultFile, result);
 104             if (verbose) {
 105                 Log.verbose(MessageFormat.format(I18N.getString(
 106                         "message.using-custom-resource-from-file"),
 107                         category == null ? "" : "[" + category + "] ",
 108                         defaultFile.getAbsoluteFile()));
 109             }
 110         }
 111     }
 112 
 113     private InputStream streamResource(String publicName, String category,
 114             String defaultName, boolean verbose, File publicRoot)
 115             throws IOException {
 116         boolean custom = false;
 117         InputStream is = null;
 118         if (publicName != null) {
 119             if (publicRoot != null) {
 120                 File publicResource = new File(publicRoot, publicName);
 121                 if (publicResource.exists() && publicResource.isFile()) {
 122                     is = new BufferedInputStream(
 123                             new FileInputStream(publicResource));
 124                 }
 125             } else {
 126                 is = getResourceAsStream(publicName);
 127             }
 128             custom = (is != null);
 129         }
 130         if (is == null && defaultName != null) {
 131             is = getResourceAsStream(defaultName);
 132         }
 133         if (verbose && is != null) {
 134             String msg = null;
 135             if (custom) {
 136                 msg = MessageFormat.format(I18N.getString(
 137                         "message.using-custom-resource"),
 138                         category == null ?
 139                         "" : "[" + category + "] ", publicName);
 140             } else {
 141                 msg = MessageFormat.format(I18N.getString(
 142                         "message.using-default-resource"),
 143                         defaultName == null ? "" : defaultName,
 144                         category == null ? "" : "[" + category + "] ",
 145                         publicName);
 146             }
 147             Log.verbose(msg);
 148         }
 149         return is;
 150     }
 151 
 152     protected String preprocessTextResource(String publicName, String category,
 153             String defaultName, Map<String, String> pairs,
 154             boolean verbose, File publicRoot) throws IOException {
 155         InputStream inp = streamResource(
 156                 publicName, category, defaultName, verbose, publicRoot);
 157         if (inp == null) {
 158             throw new RuntimeException(
 159                     "Jar corrupt? No " + defaultName + " resource!");
 160         }
 161 
 162         // read fully into memory
 163         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 164         byte[] buffer = new byte[1024];
 165         int length;
 166         while ((length = inp.read(buffer)) != -1) {
 167             baos.write(buffer, 0, length);
 168         }
 169 
 170         // substitute
 171         String result = new String(baos.toByteArray());
 172         for (Map.Entry<String, String> e : pairs.entrySet()) {
 173             if (e.getValue() != null) {
 174                 result = result.replace(e.getKey(), e.getValue());
 175             }
 176         }
 177         return result;
 178     }
 179 
 180     @Override
 181     public String toString() {
 182         return getName();
 183     }
 184 
 185     @Override
 186     public void cleanup(Map<String, ? super Object> params) {
 187         try {
 188             IOUtils.deleteRecursive(
 189                     StandardBundlerParam.TEMP_ROOT.fetchFrom(params));
 190         } catch (IOException e) {
 191             Log.debug(e.getMessage());
 192         }
 193     }
 194 }