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