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